In my WinForms app, i have this below code which fetches the lot number from MSSQL table when the button is clicked ,
the function was working fine till yesterday , but from today it is throwing below exception and stopping the application even though try block is present.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
i don't know why is this happening , but i was adding another form called "formedit" to edit the existing record , which is very similar to "FornewEntry" , but with prefilled values of selected datagridview row , maybe i have messed up something while doing the same as this is first application as i am learning C# below is the link to my solution kindly help me .
What I have tried:
private void getlotnumbers()
{
try
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd;
con.Open();
string s = "select LotNumber from Lot_Numbers where CoilNumber = @p1";
cmd = new SqlCommand(s, con);
cmd.Parameters.AddWithValue("@p1", coilNoTextBox.Text);
cmd.CommandType = CommandType.Text;
int i = cmd.ExecuteNonQuery();
lotNoTextBox.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show("Lot Number Not Found:" + ex.Message.ToString(), "Lot Number",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}