I am trying to build an SQL query to compare date in my txtDate box to one in access database for my database search-page. I'm clueless as to which is the best method to do this. Obviously just comparing the textbox value doesn't work. This is my current if-function to check it:

else if((DropDownList1.SelectedValue == "") && (txtDate.Text != ""))
			{
				strSQL = "SELECT * FROM [" + DropDownList2.SelectedValue + "] WHERE DATE_START = " + txtDate.Text;
			}

And is there any better way to build different kind of searches other than lots of if and elseif functions in a row, like i have done?

Recommended Answers

All 3 Replies

Hmm seems that i was missing "'" around the date, now it works i think. How about my other question, optimizing my search so it wouldnt be lots of if's in a row?

else if((DropDownList1.SelectedValue == "") && (txtDate.Text != ""))
{
strSQL = "SELECT * FROM [" + DropDownList2.SelectedValue + "] WHERE DATE_START =#" + txtDate.Text + "#";
}

My search still isn't working properly... it shows every result in the database even though i have WHERE definition in the string.

DateTime dtDate = Convert.ToDateTime(txtDate.Text);

strSQL = "SELECT * FROM [" + DropDownList2.SelectedValue + "] WHERE DATE_START >= '" + dtDate.Year + "-" + dtDate.Month + "-" + dtDate.Day + "'";

How should i modify my SQL query so that i can get only dates from the access database that has a "newer" date than the one in my txtbox (which i converted to datetime)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.