通过提交按钮插入和更新记录 [英] Insert and update records by submit button

查看:51
本文介绍了通过提交按钮插入和更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在尝试更新记录时正在尝试插入新记录并通过单个按钮进行更新,但是当尝试在该时间插入记录时错误是
输入的字符串格式不正确.

实际上,我不是为此记录插入ID,因为它是为插入记录自动生成的.
要更新,我使用id作为参考.

以下是我在调试时首先检查更新条件是否正确但对于插入它不会进行其他阻止的代码,如果再次阻止则是什么原因?

Hi,

I am trying to insert new record and update by single button while i am updating record that is working but when try to insert record on that time error is
input string was not in correct format.

actually i am not insert id for this record because it is auto generated for insert record.
for update i am using id for reference.

following is my coding when i debug first check if condition for update is right but for insert it is not going to else block it is again if block what is the reason?

protected void btnSave_Click(object sender, EventArgs e)
   {
       connection = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
      if(lblProduct_Id.Text==@lblProduct_Id.Text)

          {
              connection.Open();
              string strUpdate = "Update product_detail set product_name=@product_name,product_quantity=@product_quantity,product_rate=@product_rate where Product_Id=@Product_Id";
              SqlCommand cmd = new SqlCommand(strUpdate, connection);
              cmd.CommandType = CommandType.Text;
              cmd.Parameters.AddWithValue("@product_name", SqlDbType.NVarChar).Value = txtProduct_Name.Text;
              cmd.Parameters.AddWithValue("@product_quantity", SqlDbType.Int).Value = Convert.ToInt32(txtProduct_Quantity.Text);
              cmd.Parameters.AddWithValue("@product_rate", SqlDbType.Int).Value = Convert.ToInt32(txtProduct_Rate.Text);
              cmd.Parameters.AddWithValue("@product_id", SqlDbType.Int).Value = Convert.ToInt32(lblProduct_Id.Text);
              cmd.ExecuteNonQuery();
              connection.Close();
              bindgrid();
      }
        else
       {

           connection.Open();
           string strSave = "insert into product_detail (product_name,product_quantity,product_rate) values(@product_name,@product_quantity,@product_rate)";
           SqlCommand cmd = new SqlCommand(strSave, connection);
           cmd.CommandType = CommandType.Text;
           cmd.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = txtProduct_Name.Text;
           cmd.Parameters.Add("@product_quantity", SqlDbType.Int).Value = txtProduct_Quantity.Text.ToString();
           cmd.Parameters.Add("@product_rate", SqlDbType.Int).Value = txtProduct_Rate.Text.ToString();
           cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = lblProduct_Id.Text.ToString();
           cmd.ExecuteNonQuery();
           radGrid1.Visible = true;
           connection.Close();
           bindgrid();

       }

   }

推荐答案

我会弯腰,猜测您看到的错误是在C#代码中.这意味着您正在调用的方法需要一个字符串,并且您的字符串没有正确实例化/创建/格式化.

尝试在调试器下运行.
I''m gonna go out on a limb and guess that the error you''re seeing is in your C# code. It means that the method you''re calling is expecting a string, and your string is not correctly instantiated/created/formatted.

Try running under the debugger.


当时插入记录时,应禁用product_id字段.添加条件,例如product_id为空白,然后转到其他部分.
像下面提到的代码.
When you insert the record at that time you should disable the product_id field. Add the condition like if product_id is blank then go to else part.
like below mentioned code.
if(!string.IsNullOrEmpty(lblProduct_Id.Text))
{
"UPDATE Logic"
}
else
{
"Insert Logic"
}




如果ID是自动生成的,则删除下面的行.




If ID is autogenerated then remove below line.

cmd.Parameters.Add("@product_id", SqlDbType.Int).Value = lblProduct_Id.Text.ToString();



如果这对您有帮助,则将其标记为答案,请进行投票.



If this helped you then mark it as answer and please Vote.


只需将参数转换为SqlDB参数(如Int 32)即可.
Just Convert Parameters as SqlDB Parameters like Int 32.


这篇关于通过提交按钮插入和更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆