ExecuteReader

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • murugavelmsc
    New Member
    • Apr 2008
    • 54

    ExecuteReader

    Hi,

    In Visual studio 2008, i got a error "ExecuteRea der: Connection property has not been initialized" . So guide me

    [code=cpp]
    protected void Button1_Click(o bject sender, EventArgs e)
    {
    string constr=Configur ationSettings.A ppSettings["Myconn"];
    SqlConnection conn = new SqlConnection(c onstr);
    conn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from users where username='" + txtusername.Tex t + "' and user_password=' " + txtpassword.Tex t + "'";
    SqlDataReader rd = cmd.ExecuteRead er();

    if (rd.HasRows == true)
    {
    Response.Redire ct("Index.aspx" );
    }

    conn.Close();

    }[/code]
    Last edited by Frinavale; Apr 30 '08, 06:45 PM. Reason: added [code] tags
  • vee10
    New Member
    • Oct 2006
    • 141

    #2
    Hi,
    u should initialize the connection property of the sqlcommand variable

    Code:
    cmd .Connection = conn;

    Originally posted by murugavelmsc
    Hi,

    In Visual studio 2008, i got a error "ExecuteRea der: Connection property has not been initialized" . So guide me


    protected void Button1_Click(o bject sender, EventArgs e)
    {
    string constr=Configur ationSettings.A ppSettings["Myconn"];
    SqlConnection conn = new SqlConnection(c onstr);
    conn.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from users where username='" + txtusername.Tex t + "' and user_password=' " + txtpassword.Tex t + "'";
    SqlDataReader rd = cmd.ExecuteRead er();

    if (rd.HasRows == true)
    {
    Response.Redire ct("Index.aspx" );
    }

    conn.Close();

    }

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You need to associate the command object with the connection object.
      I believe it can be done in the constructor as well as setting a property.

      Comment

      Working...