开发者

user contol textbox value to datagrid

开发者 https://www.devze.com 2023-03-01 12:11 出处:网络
there 开发者_JS百科are two usercontrol in my form as label textbox datepicker 1st label=From date

there 开发者_JS百科are two usercontrol in my form as label textbox datepicker 1st label=From date 2nd label=TO date there is also one data grid and one button now i wnt data between two dates in datagrid as from two textboxes in user control i m trying this code but it is not working:

SqlConnection cs = new SqlConnection("Data Source=IRIS-CSG-174;Initial Catalog=library_system;Integrated Security=True");
            cs.Open();
            SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between'" + userControl11+ "'" + "and'" + userControl12.Text + "'", cs);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            da.Fill(ds, "lib_issue_details");
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.DataBindings.Add(new Binding("text", ds, "lib_issue_details"));

            cs.Close();


Assuming the labels have right date format, you are missing .Text on userControl11

Try

SqlCommand cmd = new SqlCommand("select * from dbo.lib_issue_details where book_issue_on between'" + userControl11.Text + "'" + "and'" + userControl12.Text + "'", cs);

And couple things not related your question. Look into parametrized queries rather than concatenating values into the query string. Also a better practice is to use using for IDisposable entities like this

using (SqlConnection cs = new SqlConnection(...) )
{
  ...
}


You have to correct your command text:

SqlCommand cmd = new SqlCommand(@"select * from dbo.lib_issue_details 
    where book_issue_on between'" + userControl11.Text+ "' and '" + 
    userControl12.Text + "'", cs);
0

精彩评论

暂无评论...
验证码 换一张
取 消