0
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=MJ-PC;Initial Catalog=Test;Integrated Security=True ");
        con.Open();
    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        //SqlCommand cmd = con.CreateCommand();
        SqlCommand cmd = new SqlCommand("select password from TestDemo where userName'" + txtusername .Text+ "'", con);

        //cmd.Connection = con;

        SqlDataReader da;
        da = cmd.ExecuteReader();
        if (!da.Read())
        {
            Response.Write("Wrong Details");
        }
        else
        {
            if(da[0].ToString()==txtusername.Text)
                 Response.Redirect("WebForm1.aspx");
            else
                Response.Write("Wrong Password");
        }
    }
1
  • Where's the error? At con.Open() or cmd.ExecuteReader()? Commented Nov 2, 2013 at 6:25

4 Answers 4

1

where username **=** forgot equality sign

Also, the conenction you open and the connection you use are different

Sign up to request clarification or add additional context in comments.

7 Comments

Heh, good catch. But still gotta figure out where he got an error. It could be a typo here.
does con.open() work fine? Also, assign resulting query to a string , debug, copy it tobuffer and run directly on sql via sql studio
Nah, I am very sure it won't work fine. See my answer for 'why'
Not sure i underatsnd you. Anyway, to build a proper connection string use VS integrated datasource wizard - just build it and test until it works. To make correct query try it on direct sql (like SSMS). If both work then do: 1) create and open connection 2) run query on it. Obviuosly :))
Well, if you see, he is trying to use con object in btnsubmit_Click handler, but according to C# rules, you CANNOT access a local object defined in one method, and use it in another method
|
0

The way I see it, you open a connection to the SQL server in Page_Load handler. But you don't close it.

If you try to open another one, or try to execute on a closed SqlConnection object, you might get an error.

A good way to do this is do something like this:

using (SqlConnection connection = new SqlConnection(connectionString)) 
{        
  try    
   {
            connection.Open();
          //do something here

   } 
   catch (Exception) 
   { 
      /*Handle error*/ 
   }

}

Comments

0
     SqlConnection con;    
    protected void Page_Load(object sender, EventArgs e)
            {
    try
    {            
    con = new SqlConnection("Data Source=MJ-PC;Initial Catalog=Test;Integrated Security=True");
                con.Open();
    }
    catch
    {
    //Handles exceptions here
    }
            }

            protected void btnsubmit_Click(object sender, EventArgs e)
            {
              try
              {
                //SqlCommand cmd = con.CreateCommand();
                SqlCommand cmd = new SqlCommand("select password from TestDemo where userName='" + txtusername .Text+ "'", con);

                //cmd.Connection = con;

                SqlDataReader da;
                da = cmd.ExecuteReader();
                if (!da.Read())
                {
                    Response.Write("Wrong Details");
                }
                else
                {
                    if(da[0].ToString()==txtusername.Text)
                         Response.Redirect("WebForm1.aspx");
                    else
                        Response.Write("Wrong Password");
                }
              }
              finally
              {
              con.Close();
              }
            }

3 Comments

How do you reference con object in btnsubmit_Click()?
@Ankit : Please check it now.
If it were upto me, I would give it another -1, you do OPEN the connection, but you don't close it. When you will try to open it again ever in the future, you will get a yellow-red screen of death on asp.net
0

If you code for login, then here a neat version code, Depend on your flagset you can redirect or display wrong password msg

    bool flagset=false;
    SqlDataReader dr;
    using (SqlConnection con = new SqlConnection(cn.ConnectionString))
    {
     using (SqlCommand cmd = new SqlCommand())
       {
        cmd.CommandText = "select password from TestDemo where userName=@uName";
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@uName", txtusername.Text); 
        cmd.Connection = con;
        con.Open();
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
         if (dr.HasRows){
           while (dr.Read())
            {
                if(dr[0].ToString()==txtusername.Text)
                 {     flagset=true;   }
            }
            }dr.Close();
             con.Close();
      }
}return flagset;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.