Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server error: ExecuteNonQuery: Connection property has not been initialized
    text
    copied!<p>I am trying to develop a sample registration page using ASP.Net and C#. I am calling a stored procedure to insert the data to database. My database is SQL Server 2008. </p> <p>This is my code:</p> <pre><code>public partial class Sample : System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()); string str; protected void Page_Load(object sender, EventArgs e) { rbt_Male.Checked = true; } protected void btn_Submit_Click(object sender, EventArgs e) { string @Name = txtbx_Name.Text; string @Gender_male = rbt_Male.Text; string @Gender_Female = rbt_Female.Text; string @Email = txtbx_Email.Text; DateTime @Dob = Convert.ToDateTime(txt_Dob.Text); submitdata(); } protected void submitdata() { try { SqlCommand cmd = new SqlCommand(); cmd.Parameters.Clear(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "insertdata"; if (rbt_Male.Checked) { cmd.Parameters.AddWithValue("@Name", txtbx_Name.Text); cmd.Parameters.AddWithValue("@Gender_Male", rbt_Male.Text); cmd.Parameters.AddWithValue("@Email", txtbx_Email.Text); cmd.Parameters.AddWithValue("@Dob", Convert.ToDateTime(txt_Dob.Text)); } else if (rbt_Female.Checked) { cmd.Parameters.AddWithValue("@Name", txtbx_Name.Text); cmd.Parameters.AddWithValue("@Gender_Female", rbt_Male.Text); cmd.Parameters.AddWithValue("@Email", txtbx_Email.Text); cmd.Parameters.AddWithValue("@Dob", Convert.ToDateTime(txt_Dob.Text)); } if (con.State == ConnectionState.Closed) con.Open(); cmd.ExecuteNonQuery(); lbl_Errormsg.Visible = true; lbl_Errormsg.Text = "Record Inserted Successfully"; con.Close(); } catch (Exception ex) { lbl_Errormsg.Visible = true; lbl_Errormsg.Text = ex.Message; } </code></pre> <p>I am getting the error message </p> <blockquote> <p><em>ExecuteNonQuery: Connection property has not been initialized.</em> </p> </blockquote> <p>I am getting this error at <code>cmd.ExecuteNonQuery();</code> </p> <p>Please help me.</p> <p>My stored procedure is </p> <pre><code>ALTER Procedure insertdata ( @Name Varchar(20), @Gender Varchar(6), @Email Varchar(20), @Dob date ) As Begin Insert into samplelogintable (Name, Gender, Email, Dob) Values(@Name, @Gender, @Email, @Dob) End </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload