Reputation: 2708
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);
}
}
Upvotes: 0
Views: 2166
Reputation: 2399
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;
Upvotes: 1