1
votes

I'm trying to do an app without adding some details about a car delivery. I wrote the code in C# and SQL, but when I add the data to textbox, radiobutton, labels, etc. I get this error:

System.Data.SqlClient.SqlException: 'The parameterized query '(@a int,@b nvarchar(7),@c int,@d nvarchar(12),@e nvarchar(10),@f' expects the parameter '@f', which was not supplied.'

But I tried by debug to see if they take values and all have values less @f The length of the columns in the database is 50 or 100

if (materialRadioButton5.Checked) 
{
    choose = "Excelent"; 
}
else if (materialRadioButton8.Checked) 
{
    choose = "Foarte bună"; 
}
else if (materialRadioButton7.Checked) 
{
    choose = "Bună"; 
}
else if (materialRadioButton6.Checked) 
{  
    choose = "Uzată"; 
}


if (materialRadioButton4.Checked)  
{
     chooser = "Mulţumit"; 
}
else if (materialRadioButton1.Checked)
{
     chooser = "Nemulţumit"; 
}

SqlConnection con = new SqlConnection(stringcon);
SqlCommand cmd = new SqlCommand();

con.Open();
cmd.Connection = con;

cmd.Parameters.Clear();
cmd.CommandText = "insert into returncar(id_client,fullname_client,id_team,fullname_team,rendition,condition,team_mention,customers_plesed,exp_felt,client_mention) values(@a,@b,@c,@d,@e,@f,@g,@h,@i,@j)";

cmd.Parameters.AddWithValue("@a", Convert.ToInt32(label65.Text));
cmd.Parameters.AddWithValue("@b", label67.Text);
cmd.Parameters.AddWithValue("@c", Convert.ToInt32(label66.Text));
cmd.Parameters.AddWithValue("@d", label68.Text);
cmd.Parameters.AddWithValue("@e", metroDateTime1.Text);          
cmd.Parameters.AddWithValue("@f", choose);
cmd.Parameters.AddWithValue("@g", firstname_textbox.Text);
cmd.Parameters.AddWithValue("@h", chooser);
cmd.Parameters.AddWithValue("@i", role_dropbox.selectedValue);
cmd.Parameters.AddWithValue("@j", materialSingleLineTextField1.Text);

cmd.ExecuteNonQuery();

SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con;

cmd2.Parameters.Clear();
cmd2.CommandText = "update rentcar set inchiriat=0 where id=@id";
cmd2.Parameters.AddWithValue("@id", Form2.idddloan);

cmd2.ExecuteNonQuery();

con.Close();

panel2.Visible = false;
bunifuFlatButton7.Visible = false;

How radiobutton4 and radiobutton1 works and the others do not?...

 public string choose, chooser;
1
Debug your code to verify you have values for all those paramaters. - Brad
You're passing choose as @f, not chooser. If materialRadioButton1 and 4 aren't checked, choose is probably null - 15ee8f99-57ff-4f92-890c-b56153
I debug my code and the @f dosn't take values.. @EdPlunkett because i have to strings one choose and chooser.. - Rasti
Choose is for that 5/8/7/6 button and chooser is for 4/1 button .. - Rasti
This would be pretty brutal to debug... if you name your variables properly, it would likely be easier. - Louis Ingenthron

1 Answers

1
votes

If the value is null then the parameter is not added and you'll get the exception you mentioned. In these cases make sure to check for null and instead pass

choose ?? Value.DBNull