Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends

I want to retrieve data from the sql database when selecting a date and itemtype but data not retrieve. without date condition its working fine. please tell me how to solve it.

query = "select sum(txtPurity) from EntryTable where txtDate='" + this.txtDate.Value.Date + "' AND cmbItemType='" + cmbItemType.Text + "'";
Posted
Comments
Pradeep Shukla 5-Oct-13 22:09pm    
The datetime field in db is of datetime type. Convert the data you are passing in datetime for comparison with the field.

1 solution

This could be a formatting issue. Go through answers at compare two datetime using sql server[^].
 
Share this answer
 
Comments
indiancodinglove 6-Oct-13 1:35am    
This query is working but when I am selecting less then txtDate its not give that date data.
it gives sum of all the field not that date of data.

query = "select sum(txtPurity) from EntryTable where txtDate > convert(datetime,'06-10-2013') AND cmbItemType='" + cmbItemType.Text + "'";
indiancodinglove 6-Oct-13 2:30am    
I also try this coding but not working


string query = null;
query = "select sum(txtPurity) from EntryTable where txtDate =@Date AND cmbItemType='" + cmbItemType.Text + "'";
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@Date", txtDate.Value.Date);
conn.Open();
SqlDataReader sqlReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (sqlReader.Read())
{
txtSumOfPurity.Text = sqlReader[0].ToString();
}
conn.Close();
Azee 6-Oct-13 3:44am    
hey there, what is the DataType of column txtDate, Date or DateTime?
indiancodinglove 6-Oct-13 8:54am    
DateTime

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900