0

I am inserting data into database using the following code. Neither it gives error nor it adds data to database. Am i missing anything in it?

The columns are univ_regno and email have datatype nvarchar(50) respectively

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (chkaccept.Checked)
    {
        try
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL Connection String"].ConnectionString);
            con.Open();
            com = new SqlCommand("INSERT INTO stdtable ([univ_regno],[email]) values(@univ_regno,@email)", con);

            com.Parameters.Add("@univ_regno", txtuniv.Text);
            com.Parameters.Add("@email", txtemail.Text);

            com.ExecuteNonQuery();
            con.Close();

            /*show javascript message */
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            String cstext = "alert('Record Added Successfully');";
            cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
        }
        catch (System.Exception err)
        {
           Type cstype = this.GetType();
           ClientScriptManager cs = Page.ClientScript;
           String cstext = "alert('"+  err.Message.ToString() +" ');";
           cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
        }
   }
   else
   {
       Type cstype = this.GetType();
       ClientScriptManager cs = Page.ClientScript;
       String cstext = "alert('Not checked ');";
       cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
   }
}
8
  • 1
    This shouldn't effect your specific problem, but you aren't closing your connections correctly. Commented Oct 22, 2012 at 17:48
  • What result does ExecuteNonQuery() return? Commented Oct 22, 2012 at 17:50
  • i am using con.Close(); is it not sufficient? @JoelCoehoorn Commented Oct 22, 2012 at 17:50
  • 1
    My guess is that there is an error in your javascript reporting. Set a break point in the catch to see what is actually happening with the sql call... Commented Oct 22, 2012 at 17:50
  • The connection must always be closed in a finally block, or you can end up creating a denial of service situation on your database. Commented Oct 22, 2012 at 17:51

1 Answer 1

1

enter image description here com.Parameters.Add("@univ_regno", txtuniv.Text);

does this add the value to the parameter? it calls the Parameters.Add(string name, SqlDbType type) overload

refer this link:

Difference between Parameters.Add and Parameters.AddWithValue

Try adding parameters as

cmd.Parameters.Add("@univ_regno", SqlDbType.VarChar).Value = txtuniv.Text;
Sign up to request clarification or add additional context in comments.

4 Comments

can u tell me that is there compulsary to declare scalar variable in sequence?
as i m adding data to univ_regno and email and adding parameters seqeuntially. can i email first and then univ_regno?
i think that wont be a problem.did you try changing the way you add parameters?use a break point and debug it line by line it may help

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.