public partial class Form3 : Form
{
string var;
int ID1;
private void button1_Click(object sender, EventArgs e)
{
ID1 = int.Parse(textBox1.Text);
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\asp\Desktop\DatabasesPractice\DatabasesPractice\soccer.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand dataCommand = new SqlCommand("SELECT Name FROM team WHERE ID = @ID1", cn);
cn.Open();
var = Convert.ToString(dataCommand.ExecuteScalar());
label3.Text = var;
}
}
It gives me an error saying I must declare the scalar variable @ID, I’m searching everywhere and can’t get a solution for my specific case.
Thanks for the help people! your solutions worked 😀
}
See above, I have added
dataCommand.Parameters.AddWithValue("@ID1", ID1);. What that does is defines your parameter for your SQL query and passes it the value. I assumed that you’d want the variableID1to be the value of your SQL parameter@ID1.