public partial class Home_AlumniLogin : System.Web.UI.Page
{
    Buildconnection c = new Buildconnection();
    public string conn = ConfigurationSettings.AppSettings["dsn"];
    public string str = "Data Source=S-90E1F156B1BF4\\SQLEXPRESS;uid=a;database=college";
    public SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

          con = new SqlConnection(conn); 
          string str = "select ID1 from student where ID1='" + TextBox1.Text + "' and password='" + TextBox2.Text + "'";
          con.Open();  
          SqlCommand cmd = new SqlCommand(str, con);
          string a = Convert.ToString(cmd.ExecuteScalar());

        if (a == "")
        {
            Message.Text = "User Not Found";
        }
        else 
        {
            Session["ID1"] = a;
            Response.Redirect("../Student/AlumniHome.aspx");

        }

       
    }
}

I got an error at con.Open();
please help me....it's very urgent....

Dani AI

Generated

is right — the actual exception text is needed. ’s pointer about the connection string is also on target. Common reasons for an error at con.Open() are: a missing/incorrect connection string coming from config, wrong instance name or SQL service not running, incorrect authentication (missing password or wrong auth mode), or network/permission issues. The next steps are diagnostic and quick to try.

Quick checklist to diagnose and fix

  • Capture the full exception and stack trace (wrap Open() in a try/catch and log ex.Message and ex.StackTrace). That tells what to fix.
  • Verify the runtime value of the connection string (set a breakpoint or log it). If the config key is missing you’ll get null/empty and Open() will fail.
  • Put the connection string in <connectionStrings> and read it with ConfigurationManager.ConnectionStrings[...] rather than appSettings. See the ADO.NET connection-string recommendations: ADO.NET connection strings and the config API: ConfigurationManager.

Example best-practice notes

  • Test the same credentials/instance in SQL Server Management Studio to confirm the server, instance name (for SQLEXPRESS use .\SQLEXPRESS), and credentials work.
  • Ensure SQL Server service is running and that TCP/IP or the SQL Browser is enabled for named instances.
  • Use using blocks to dispose connections, check ExecuteScalar() for null instead of assuming an empty string, and use parameterized queries to avoid SQL injection.

A short, safe pattern and the SqlConnection.Open behavior are documented here: SqlConnection.Open. If the exact exception text is posted, a more specific fix can be suggested. — please post the error text when available.

Recommended Answers

All 3 Replies

Could we at least be told what the error is?

Without knowing the details of your error, I would say that your connection string (conn) is incomplete. Here is a great reference site for constructing connection strings, but I would say that you may need to provide a Password or Integrated Security setting?

commented: Agree! :) +15

give details of an ERROR

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.