Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

       }

   }
Posted
Updated 8-Oct-10 6:24am
v3
Comments
Hiren solanki 8-Oct-10 7:36am    
question is not clear.
Goutam Patra 8-Oct-10 7:38am    
Update your question to show what coding have you done for that so that some one can help you to find out what wrong with your code.

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.
 
Share this answer
 
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.
C#
if(!string.IsNullOrEmpty(lblProduct_Id.Text))
{
"UPDATE Logic"
}
else
{
"Insert Logic"
}



If ID is autogenerated then remove below line.

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


If this helped you then mark it as answer and please Vote.
 
Share this answer
 
Just Convert Parameters as SqlDB Parameters like Int 32.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900