2021年5月3日星期一

object reference not set to an instance of an object in winforms c# [duplicate]

I use this code to update table in database, but there is error

object reference not set to an instance of an object

con = new SqlConnection(con_str);    try  {      if (company != "")      {          con.Open();                   cmd = new SqlCommand("update representatives set  payment_alarm=@payment_alarm where represent_name='" + rep + "'", con);            cmd.Parameters.AddWithValue("@payment_alarm", paymentAlarm_txt.Text);                cmd.ExecuteNonQuery();            MessageBox.Show("added successfully");            con.Close();      }        else      {          MessageBox.Show("please choose company");      }  }    catch (Exception ex)  {      MessageBox.Show(ex.Message);  }  

Note "rep" is a a string data type which value is referenced with combobox

https://stackoverflow.com/questions/67378203/object-reference-not-set-to-an-instance-of-an-object-in-winforms-c-sharp May 04, 2021 at 10:55AM

没有评论:

发表评论