Reputation: 13
I want to get records for 1 day to 30 days the Query is working in SQL properly
SELECT * FROM Tbl_order WHERE date >= DATEADD(day, -1, GETDATE())
so what i did in C#
i use combo box
int CmboDays;
for (CmboDays = -1; CmboDays >= -30; CmboDays--)
{
comboBox1.Items.Add(CmboDays);
}
private void orderBySearchDays()
{
string c = comboBox1.Text;
int x = Convert.ToInt32(c);
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("select* from Tbl_order WHERE date >= DATEADD(day,'"+ x + "', GETDATE()) ; ", connection))
{
DataTable Tbl_order = new DataTable();
connection.Open(); //opens the connection
adapter.Fill(Tbl_order);
connection.Close(); //Closes the connection
lst_CustomerNo.DataSource = Tbl_order; //assigns a datasource
lst_CustomerNo.DisplayMember = "CustomerNo"; //assigns display
lst_CustomerNo.ValueMember = "CustomerNo";
lst_OrderName.DataSource = Tbl_order;
lst_OrderName.DisplayMember = "OrderName";
lst_OrderName.ValueMember = "OrderName";
lst_Quantity.DataSource = Tbl_order;
lst_Quantity.DisplayMember = "Quantity";
lst_Quantity.ValueMember = "Quantity";
lst_Price.DataSource = Tbl_order;
lst_Price.DisplayMember = "Price";
lst_Price.ValueMember = "Price";
lst_datetime.DataSource = Tbl_order;
lst_datetime.DisplayMember = "Date";
lst_datetime.ValueMember = "Date";
}
}
so all the data will be display in list box
i'm getting this Error
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Argument data type varchar is invalid for argument 2 of dateadd function.
Am I doing it that Wrong ? How to fix the Error ?
Upvotes: 1
Views: 300
Reputation: 13403
could you chnage x with x.ToString() and remove '
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from Tbl_order WHERE date >= DATEADD(day,"+ x.ToString() + ", GETDATE()) ; ", connection))
Upvotes: 1