2012-10-22 112 views
0

我使用下面的代码将数据插入到数据库中。它既不会给出错误,也不会将数据添加到数据库中。我错过了什么吗?在asp.net中插入数据到SQL Server数据库c#

的列有univ_regnoemail具有数据类型nvarchar(50)分别

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); 
    } 
} 
+1

这应该不会影响您的特定问题,但是您没有正确关闭连接。 –

+0

'chkaccept.Checked'是真的吗? –

+0

ya其真正Mr @BigM –

回答

1

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

这是否将值添加到参数?它调用Parameters.Add(字符串名称,SqlDbType型)过载

参考以下链接:

Difference between Parameters.Add and Parameters.AddWithValue

尝试增加参数

cmd.Parameters.Add("@univ_regno", SqlDbType.VarChar).Value = txtuniv.Text; 
+0

它是如何超载的? –

+0

看到编辑答案 – Karthik

+0

你能告诉我,有强制性的顺序声明标量变量吗? –

相关问题