skip to Main Content

This Is My Codding i have face this type of error in my code

(Exception thrown: ‘System.Data.SqlClient.SqlException’ in
System.Data.dll

Additional information: Error converting data type varchar to bigint

Update Query:___________________________________

private void btnUpdate_Click(object sender, EventArgs e)
{
    query = ("update items set name='" + txtName.Text + "',category='" + txtCategory.Text + "',price='" + txtPrice.Text + "where iid =" + id + "'");
    fn.setData(query);
    loadData();
    txtName.Clear();
    txtCategory.Clear();
    txtPrice.Clear();
}

Set Query_______________

public void setData(String query)
{
    SqlConnection con = getConnection();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    con.Open();
    cmd.CommandText = query;
    cmd.ExecuteNonQuery();
    con.Close();


    MessageBox.Show("Data Processed Successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

2

Answers


  1. Change your code as highlighted below, it should solve your problem.

    {    
        query = ("update items set name='" + txtName.Text + "',category='" + txtCategory.Text + "',price= **" +int.Parse(txtPrice.Text) +** "where iid =" + id + "'");
    
        fn.setData(query);
        loadData();
        txtName.Clear();
        txtCategory.Clear();
        txtPrice.Clear();
    }
    
    Login or Signup to reply.
  2. Always try to use Parameterized Query or Stored Procedure, rather than injecting values.

    btnUpdate_Click

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        query = ("update items set name = @name, category = @category, price = @price where iid = @id");
        fn.setData(query,long.Parse(id),txtName.Text, txtCategory.Text, long.Parse(txtPrice.Text));
        loadData();
        txtName.Clear();
        txtCategory.Clear();
        txtPrice.Clear();
    }
    

    setData function

    public void setData(String query, long id,string name, string category, long price)
    {
        SqlConnection con = getConnection();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = query;
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@category", category);
        cmd.Parameters.AddWithValue("@price", price);
        cmd.Parameters.Add(new SqlParameter()
        {
            DbType = System.Data.DbType.Int64, //For big int
            Direction = System.Data.ParameterDirection.Input,
            ParameterName = "@id",
            Value = id
        });
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Data Processed Successfully.", "Success",MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch(Exception ex)
        {
            // catch exception here
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search