Israel

Israel

  • 703
  • 1.3k
  • 215.6k

Name of dbconnection correct. But still wrong connection...

Jul 23 2013 8:47 AM

Hello!

Just a little bug but can make a funny crash (this is all the code)
After to write my codes when "ctrl+f5" its crash and showing me this error message from the .aspx.:
The connection name 'Mfwamba' was not found in the application configuration or the connection string is empty

But the real name of my dbconnection is really 'Mfwamba'. Why this message then? Thanx.



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowDetails.aspx.cs" Inherits="ShowDetails" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="height: 74px">
                    Enter a Employee ID To be Searched
                </td>
                <td style="height: 74px">
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
                        DataTextField="EmpId" DataValueField="EmpId"></asp:DropDownList>

                         <!--the crash it's here-->
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Mfwamba %>"
                        SelectCommand="SELECT EmpId, EmpName, EmpAdd, imPhoto FROM Emp"></asp:SqlDataSource>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" />
                </td>
            </tr>
            <tr>
                <td>
                    Employee Image :
                </td>
                <td valign="top">
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/Handler.ashx?ID=" + Eval("EmpID")%>' /></td>
            </tr>
            <tr>
                <td>
                    Employee Name :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td>
                    Employee Address :
                </td>
                <td>
                    &nbsp;<asp:Label ID="Label2" runat="server"></asp:Label></td>
            </tr>
        </table>
       
    </div>
    </form>
</body>
</html>

////////////////////////////

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class ShowDetails : System.Web.UI.Page
{
    string strcon = "Data Source=localhost\\sqlexpress;Integrated Security=true;Initial Catalog=Mfwamba";
    SqlConnection con;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(strcon);
        con.Open();
        cmd = new SqlCommand("Select * from Emp where Empid=@id", con);
        cmd.Parameters.AddWithValue("@id", DropDownList1.Text);
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        if (dr.HasRows)
        {
            Label1.Text = dr[1].ToString();
            Label2.Text = dr[2].ToString();
            Image1.ImageUrl = "~/Handler.ashx?id=" + DropDownList1.Text;
        }
        con.Close();
    }
}


Answers (2)