Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried codes below but wont work

What I have tried:

C#
using (SqlConnection con = new SqlConnection(strConnString))
{
    string strQuery = "SELECT * FROM LoanRates where DateTime.Now between LoanAvailableFrom and LoanAvailableTo";
    SqlCommand cmd = new SqlCommand(strQuery);
    using (SqlDataAdapter sda = new SqlDataAdapter())
    {
        cmd.Connection = con;
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(da);
        GridView1.DataSource = da;
        GridView1.DataBind();

    }
}
Posted
Updated 5-Aug-16 18:56pm
v2
Comments
George Jonsson 5-Aug-16 23:19pm    
What kind of type is the variable da?
And what is not working? No data shown in the grid view?
Dave Kreskowiak 6-Aug-16 0:08am    
So what is "DateTime.Now" supposed to do in an SQL statement?

If the Application and Database is in same server or location, you could use this
C#
string strQuery = "SELECT * FROM LoanRates where getdate() between LoanAvailableFrom and LoanAvailableTo";

else
C#
string strQuery = "SELECT * FROM LoanRates where @date between LoanAvailableFrom and LoanAvailableTo";
           SqlCommand cmd = new SqlCommand(strQuery);
           string date = DateTime.Now.ToString("yyyy-MM-dd"); // check the format in your table
           cmd.Parameters.Add("@date", date);
 
Share this answer
 
Comments
Krunal Rohit 6-Aug-16 3:41am    
5!
KR
Karthik_Mahalingam 6-Aug-16 3:47am    
Thank you KR,
Try this line instead :
C#
string dt = DateTime.Now.ToString("yyyy-MM-dd");
string strQuery = "SELECT * FROM LoanRates where " + dt +" between LoanAvailableFrom and LoanAvailableTo";
 
Share this answer
 
Comments
Krunal Rohit 6-Aug-16 3:41am    
5!
KR

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