while inserting record in sql server database. every time inserting 2 records, i think event firing two times. how to stop inserting records twice in ado.net.
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
CompanyName = txtCompany.Text.Trim();
InsertData();
}
catch (Exception)
{
throw;
}
}
private void InsertData()
{
if (Page.IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kernelCS"].ConnectionString);
SqlCommand cmd = new SqlCommand(@"INSERT INTO [tbl_Leads]([CompanyName])VALUES(@CompanyName)", con);
cmd.Parameters.AddWithValue("@CompanyName", txtCompany.Text.ToString());
con.Open();
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
...
}
}
您正在通过cmd.ExecuteNonQuery()
和da.Fill(ds)
语句在代码中两次触发查询。
Try This :
private void InsertData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["kernelCS"].ConnectionString);
SqlCommand cmd = new SqlCommand(@"INSERT INTO [tbl_Leads]
([CompanyName]) VALUES (@CompanyName)", con);
cmd.Parameters.AddWithValue("@CompanyName", txtCompany.Text.ToString());
con.Open();
cmd.ExecuteNonQuery();
}`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.