0

Really Im getting very much confustion. I searched in net to get solution for the following msg.
There is already an open DataReader associated with this Command which must be closed first.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.

Source Error:

Line 94:             cmd.Parameters.AddWithValue("@status", "0");
Line 95:             Label1.Text = " Record added successully";
Line 96:             cmd.ExecuteNonQuery();            
Line 97:         }
Line 98:         con.Close();

somebody asked to insert to connection string, ultipleActiveResultSets=True; this code

con = new SqlConnection("MultipleActiveResultSets=True; Server=.; database=nkm; user id=sa; pwd=karthavya");

but still it is not working pls help.....

public partial class registration : System.Web.UI.UserControl
{
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    string gender;

    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection("MultipleActiveResultSets=True; Server=.; database=nkm; user id=sa; pwd=karthavya");
        con.Open();
        cmd = new SqlCommand();
        cmd.Connection = con;

        int years = DateTime.Now.Year;

        int i = 1;
        while (i < 32)
        {
            ListItem L = new ListItem(i.ToString(), i.ToString());
            days.Items.Add(L);
            i++;
        }

        for (i = 1; i < 13; i++)
        {
            ListItem L = new ListItem(i.ToString(), i.ToString());
            month.Items.Add(L);

        }

        for (i = 1950; i <= years; i++)
        {
            ListItem L = new ListItem(i.ToString(), i.ToString());
            year.Items.Add(L);
        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(RadioButton1.Checked)
            gender="Male";
        else if(RadioButton2.Checked)
            gender="Female";

        cmd.CommandText = string.Format("SELECT uid FROM member WHERE uid='{0}'", txtuserid.Text);
        SqlDataReader buffer = cmd.ExecuteReader();
        if (buffer.Read())
        {
            Response.Write("<script> alert('User Id already exist " + buffer.GetValue(1).ToString() + " ...') </script>");
            buffer.Close();         
        }
        else
        {
            cmd.CommandText = "member_login";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@uid", txtuserid.Text);
            cmd.Parameters.AddWithValue("@password", txtpassword.Text);
            cmd.Parameters.AddWithValue("@name", txtname.Text);
            cmd.Parameters.AddWithValue("@fname", txtfname.Text);
            cmd.Parameters.AddWithValue("@gender", gender);
            cmd.Parameters.AddWithValue("@dob", string.Concat(month.SelectedItem.Value, "/", days.SelectedItem.Value, "/", year.SelectedItem.Value));
            cmd.Parameters.AddWithValue("@bgroup", bgroup.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@address1", txtaddress1.Text);
            cmd.Parameters.AddWithValue("@address2", txtaddress2.Text);
            cmd.Parameters.AddWithValue("@city", txtcity.Text);
            cmd.Parameters.AddWithValue("@district", txtdistrict.Text);
            cmd.Parameters.AddWithValue("@state", txtstate.Text);
            cmd.Parameters.AddWithValue("@pincode", txtpincode.Text);
            cmd.Parameters.AddWithValue("@email", txtemail.Text);
            cmd.Parameters.AddWithValue("@phone", txtphone.Text);
            cmd.Parameters.AddWithValue("@mobile", txtmobile.Text);
            cmd.Parameters.AddWithValue("@nominee_name", txtnomineename.Text);
            cmd.Parameters.AddWithValue("@nominee_address", txtnomineeaddress.Text);
            cmd.Parameters.AddWithValue("@nominee_relationship", txtnomineerealtionship.Text);
            cmd.Parameters.AddWithValue("@date", DateTime.Now.ToString("MM/dd/yyyy"));
            cmd.Parameters.AddWithValue("@session", "Null");
            cmd.Parameters.AddWithValue("@flag", 0);
            cmd.Parameters.AddWithValue("@status", "0");
            Label1.Text = " Record added successully";
            cmd.ExecuteNonQuery();            
        }
        con.Close();
    }

}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Lusiphur
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.