1
votes
private void button3_Click(object sender, EventArgs e)
    {
        string ssr;
        SqlConnection scr = new SqlConnection(@"Data Source=USER-PC\MSSQL;Initial Catalog=Highscore;Integrated Security=True");
        scr.Open();
        ssr = "Select Nume,Scor,DataInitiala,DataRecenta FROM Users where DataInitiala between @Param and @Param1 ";
        SqlCommand cmd2 = new SqlCommand(ssr, scr);
        cmd2.Parameters.AddWithValue("@Param", from.Text);
        cmd2.Parameters.AddWithValue("@Param1", to.Text);
        SqlDataAdapter adapter1 = new SqlDataAdapter(ssr, scr);
        DataSet ds1 = new DataSet();
        adapter1.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.Refresh();  


    }

What am I missing here?I have the error Must Declare scalar variable @Param

2
what is from.Text's value? - Tzah Mama
the value is entered by the user in the app(it is a date) - RashReigns
If you're dealing with date/time/timestamps, you don't want to use BETWEEN. Also, you should be converting the parameters to the proper type before inclusion in the query anyways, don't just assume the text values are correct. This doesn't necessarily solve your problem, though... - Clockwork-Muse

2 Answers

2
votes

Actually you declaring Parameters variable in cmd2 but you are calling ssr in adapter1.

Try this

private void button3_Click(object sender, EventArgs e)
    {
        string ssr;
        SqlConnection scr = new SqlConnection(@"Data Source=USER-PC\MSSQL;Initial Catalog=Highscore;Integrated Security=True");
        scr.Open();
        ssr = "Select Nume,Scor,DataInitiala,DataRecenta FROM Users where DataInitiala between @Param and @Param1 ";
        SqlCommand cmd2 = new SqlCommand(ssr, scr);
        cmd2.Parameters.AddWithValue("@Param", from.Text);
        cmd2.Parameters.AddWithValue("@Param1", to.Text);
        SqlDataAdapter adapter1 = new SqlDataAdapter();
        adapter1.SelectCommand = cmd2;
        DataSet ds1 = new DataSet();
        adapter1.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.Refresh();         
    }
0
votes

please see the below query:

private void button3_Click(object sender, EventArgs e)
    {
        string ssr;
        SqlConnection scr = new SqlConnection(@"Data Source=USER-PC\MSSQL;Initial Catalog=Highscore;Integrated Security=True");
        scr.Open();
        ssr = "Select Nume,Scor,DataInitiala,DataRecenta FROM Users where DataInitiala between @Param and @Param1 ";
        SqlCommand cmd2 = new SqlCommand(ssr, scr);
        cmd2.Parameters.AddWithValue("@Param", from.Text);
        cmd2.Parameters.AddWithValue("@Param1", to.Text);
        SqlDataAdapter adapter1 = new SqlDataAdapter();
        adapter1.SelectCommand = cmd2;
        DataSet ds1 = new DataSet();
        adapter1.Fill(ds1);
        dataGridView1.DataSource = ds1.Tables[0];
        dataGridView1.Refresh();         
    }