i have a form with datetimepicker,button,datagridview. in SQL i have a table with feeid,feeno,classid,feedate,total named fee.
when we click the button, I want to select all the data from sql according to Selected date
(datetimepicker) and it will displayed in datagridview.
Loading
Riddhi ValechaPosted Apr 17, 2013, 6:11 AM
Try this code. It works-
DateTime dt;
SQLConnection con;
SQLCommand cmd;
SQLDataAdapter;
Datatable table;
private void DateTimePicker1_ValueChanged(Object sender, EventArgs e)
{
dt = DateTimePicker1.Selecteddate.value;
}
private void button1_click(object sender, EventArgs e)
{
try
{
con = new SQLConnection(@"connection string");
con.open();
string s = "select * from tablename where Date='"+dt+"'";
cmd = new SQLCommand(con, s);
adp = new SQLDataAdapter();
adp.SelectCommand = cmd;
table = new DataTable();
adp.Fill(table);
datagridview1.dataSource = table;
datagridview1.bind();
}
catch(Exception err){err.Message.ToString();}
finally
{
if(con.State == ConnectionState.open)
{
con.Close();
}
}
}
------------
If you still have issues, and do not mind in sharing the project, do share it.
Hope this helps.
sheetalPosted Apr 11, 2011, 5:24 AM
Please check the datatype of your feedate in sql data table. Change it to varchar if it is datetime or smalldatetime.
on button click event u can write
sql command
comm=new sqlcommand("ur query where feedate='"+dtp.value+"' ");
fill the adapter
set datasource property of grid.
Thanks,
Sheetal
DevMaster IndiaPosted Apr 11, 2011, 3:43 AM
The problem u are facing is associated with TimeStamp with datetime type of columns, so just remove the time from both side (Database column & variable both) & compare the dates. Whats the issue in this?
The date from front end must be sent in the format you choose in the datetime picker control. (DD/MM or MM/DD)
Example:
SELECT * FROM
WHERE Convert(varchar(20),
Note: The '101' (also 103) can be used to format the datetime fields into 'DD/MM/YYYY' or 'MM/DD/YYYY' formats.
Mayur GujrathiPosted Apr 11, 2011, 3:11 AM
so you need to convert your datetime picker value into string and get into strin variable, you can get it easily from net and then do
cmd.CommandText = "select * from fee where feedate='" + DateTime.Parse(variable name) + "'";
Karthikeyan AnbarasanPosted Apr 11, 2011, 2:33 AM
Shyja AbrahamPosted Apr 11, 2011, 2:31 AM
Mayur GujrathiPosted Apr 11, 2011, 1:57 AM
and then convert date to string
Shyja AbrahamPosted Apr 11, 2011, 1:45 AM
Mayur GujrathiPosted Apr 11, 2011, 1:15 AM
Shyja AbrahamPosted Apr 11, 2011, 12:32 AM
Sam HobbsPosted Apr 10, 2011, 2:40 PM