locked
Error in session state RRS feed

  • Question

  • User1734666507 posted

    Hi

    I am trying to implement a session state and this is the code:

    protected void Page_Load(object sender, EventArgs e)
    {

    SqlConnection conn2 = new SqlConnection();
    conn2.ConnectionString = ConfigurationManager.ConnectionStrings["SungeiBulohConnectionString"].ConnectionString;

    SqlCommand cmd = new SqlCommand();

    conn2.Open();

    SqlDataReader dr = cmd.ExecuteReader();


    while (dr.Read())
    {
    string TextBox1 = dr["SessName"].ToString();
    string DropDownList1 = dr["SessClass"].ToString();

    if (TextBox1.Equals(TextBox1) && DropDownList1.Equals(DropDownList1))
    {
    Session["timeStart"] = DateTime.Now.ToString();
    Session["SessName"] = TextBox1;
    Session["SessClass"] = DropDownList1;
    }
    }
    dr.Close();
    conn2.Close();
    Error:

    ExecuteReader: Connection property has not been initialized.

    Line 26: SqlDataReader dr = cmd.ExecuteReader(); 


    I am clueless! I tried shifting conn2.Open() to different parts of the code but the error still persists! Help is much appreciated!
    Friday, August 19, 2011 4:19 AM

Answers

  • User1734666507 posted
    protected void Button1_Click(object sender, EventArgs e)
        {
            string Name = TextBox1.Text;
            string Class = DropDownList1.SelectedItem.Text;
    
            SqlConnection connIns = new SqlConnection();
            SqlCommand cmdIns = new SqlCommand();
    
            try
            {
                connIns.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
    
    
                cmdIns.CommandText = "INSERT INTO Login(Name, Class) VALUES (@Name, @Class)";
                cmdIns.Connection = connIns;
                cmdIns.Parameters.AddWithValue("@Name", Name);
                cmdIns.Parameters.AddWithValue("@Class", Class);
                connIns.Open();
    
                cmdIns.ExecuteNonQuery();
    
                Session["SessName"] = Name;
                Session["SessClass"] = Class;
    
                Response.Redirect("FirstPage.aspx");
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (connIns.State != System.Data.ConnectionState.Closed)
                {
                    connIns.Close();
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 3:08 AM

All replies

  • User905006921 posted

    Hi,

    you have missing connection initialization

       SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn2;             conn2.Open(); 
    Friday, August 19, 2011 4:25 AM
  • User-2067077449 posted
    pass the connection  name into sqlcommand
    

    SqlCommand cmd = new SqlCommand(conn2);
    Friday, August 19, 2011 4:31 AM
  • User1734666507 posted

    I changed the code to:

    SqlConnection conn2 = new SqlConnection();
    conn2.ConnectionString = ConfigurationManager.ConnectionStrings["SungeiBulohConnectionString"].ConnectionString;

    SqlCommand cmd = new SqlCommand();

    cmd.Connection = conn2;
    conn2.Open();

    SqlDataReader dr = cmd.ExecuteReader();


    while (dr.Read())
    {
    string TextBox1 = dr["SessName"].ToString();
    string DropDownList1 = dr["SessClass"].ToString();

    if (TextBox1.Equals(TextBox1) && DropDownList1.Equals(DropDownList1))
    {
    Session["timeStart"] = DateTime.Now.ToString();
    Session["SessName"] = TextBox1;
    Session["SessClass"] = DropDownList1;
    }
    }
    dr.Close();
    conn2.Close();
    
    
    
    
    and i got this error:

    ExecuteReader: CommandText property has not been initialized

    Line 27: SqlDataReader dr = cmd.ExecuteReader(); 

    Friday, August 19, 2011 4:47 AM
  • User905006921 posted

    Hi

    you cannot write the command text.

    SqlConnection conn2 = new SqlConnection();
            conn2.ConnectionString = ConfigurationManager.ConnectionStrings["SungeiBulohConnectionString"].ConnectionString;
           
            SqlCommand cmd = new SqlCommand();
    cmd.CommandText="Select * from tablename ";

    cmd.Connection = conn2;
            conn2.Open();

            SqlDataReader dr = cmd.ExecuteReader();


            while (dr.Read())
            {
                string TextBox1 = dr["SessName"].ToString();
                string DropDownList1 = dr["SessClass"].ToString();

                if (TextBox1.Equals(TextBox1) && DropDownList1.Equals(DropDownList1))
                {
                    Session["timeStart"] = DateTime.Now.ToString();
                    Session["SessName"] = TextBox1;
                    Session["SessClass"] = DropDownList1;
                }
            }
            dr.Close();
            conn2.Close();

    Friday, August 19, 2011 4:51 AM
  • User-2067077449 posted

    intilize the sqldatareader and sqlcommand before the conn2.open() like

    SqlDataReader dr;

    sqlcommand cmd;

    conn2.open();

    cmd = new SqlCommand(conn2);

    dr = cmd.ExecuteReader();

    Friday, August 19, 2011 4:56 AM
  • User1734666507 posted

    Hi

    but i am not selecting any data from any table, i just need to store the data temporarily and retrieve it in the same session. AHH

    ====================================================================================================

    ====================================================================================================

    Ok, I realised that session must be place in a button for it to initialise and not on a page load. Will edit my codes and update you guys again!

    Friday, August 19, 2011 5:17 AM
  • User905006921 posted

    if you are not recive data from sql server then why u using sqlconnection,command..

    Friday, August 19, 2011 6:11 AM
  • User-126497635 posted
    protected void Page_Load(object sender, EventArgs e)
    {

    if(!IsPostBack)
    {
    SqlConnection conn2 = new SqlConnection();
    conn2.ConnectionString = ConfigurationManager.ConnectionStrings["SungeiBulohConnectionString"].ConnectionString;

    SqlCommand c = new SqlCommand("Your Query", conn2);

    conn2.Open();

    SqlDataReader dr = cmd.ExecuteReader();


    if(dr.Read())
    {
    string TextBox1 = dr["SessName"].ToString();
    string DropDownList1 = dr["SessClass"].ToString();

    if (TextBox1.Equals(TextBox1) && DropDownList1.Equals(DropDownList1))
    {
    Session["timeStart"] = DateTime.Now.ToString();
    Session["SessName"] = TextBox1;
    Session["SessClass"] = DropDownList1;
    }
    }
    dr.Close();
    conn2.Close();
    }
    }
    Friday, August 19, 2011 8:04 AM
  • User1734666507 posted
    protected void Button1_Click(object sender, EventArgs e)
        {
            string Name = TextBox1.Text;
            string Class = DropDownList1.SelectedItem.Text;
    
            SqlConnection connIns = new SqlConnection();
            SqlCommand cmdIns = new SqlCommand();
    
            try
            {
                connIns.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
    
    
                cmdIns.CommandText = "INSERT INTO Login(Name, Class) VALUES (@Name, @Class)";
                cmdIns.Connection = connIns;
                cmdIns.Parameters.AddWithValue("@Name", Name);
                cmdIns.Parameters.AddWithValue("@Class", Class);
                connIns.Open();
    
                cmdIns.ExecuteNonQuery();
    
                Session["SessName"] = Name;
                Session["SessClass"] = Class;
    
                Response.Redirect("FirstPage.aspx");
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (connIns.State != System.Data.ConnectionState.Closed)
                {
                    connIns.Close();
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 3:08 AM