How do I show a selected date data from a SQL Server database in a DataGridView
?
I tried but that shows all data from database.
Here is the code which triggers when I click the button to show
SqlDataAdapter da = new SqlDataAdapter("select currDate,WtrNm,Type,No from WtrTblAllot", con);
DataSet ds = new DataSet();
da.Fill(ds);
dgvWtrAllot.DataSource = ds.Tables[0];
Since you don't have a WHERE
clause in your query its very obvious that it will fetch all data from the database.
SqlDataAdapter da = new SqlDataAdapter("select currDate,WtrNm,Type,No
from WtrTblAllot", con);
Add WHERE clause to your query if you want to fetch only specific records form the database table.
Use your selected date in WHERE clause as @Coder says...
Date selectedDate = Date.Now;
String query = "SELECT currDate,WtrNm,Type,No FROM WtrTblAllot WHERE currDate = @SelectedDate";
SqlComman command = New SqlCommand(query, con);
command.Parameters.AddWithValue("@SelectedDate", selectedDate);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
dgvWtrAllot.DataSource = ds.Tables[0];
When adding some values inside of query, then better practice is using of parameters...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.