Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys. When I search record between two dates it works ok success but you must enter date from and dateto first to to make search

I will show what i need from this example

I need to search dynamic by 4 textbox

1-datefrom

2-dateto

3-EmployeeNo

4-EmployeeName

but search i need must be dynamic meaning

if i enter employee no only give me employee no found in database

if i enter employee name give me employees found with this name using like

if i enter all 4 text box null and enter button search get all data

but i have proplem in this query when i need to search by click search button

i must write date from and date to firstly then write employee no or employee name if i need to search

so that i need to search by employee no alone or employee name alone without using date from and date to

And if i search without using datefrom and dateto it give me message error 'string wasnot recognized as valid datetime"

my stored procedure and code as following :
SQL
ALTER proc [dbo].[CollectsearchData]
@StartDate datetime, 
@EndDate datetime,
@EmployeeID NVARCHAR(50),
@EmployeeName nvarchar(50)
as
Begin
Declare @SQLQuery as nvarchar(2000)
SET @SQLQuery ='SELECT * from ViewEmployeeTest Where (1=1)' 

If (@StartDate is not NULL) 
Set @SQLQuery = @SQLQuery + ' And (joindate >= '''+ Cast(@StartDate as varchar(100))+''')' 
If (@EndDate is not NULL) 
Set @SQLQuery = @SQLQuery + ' And (joindate <= '''+ Cast(@EndDate as varchar(100))+''')' 
If @EmployeeID <>''
Set @SQLQuery = @SQLQuery + 'And (EmployeeID = '+ @EmployeeID+') '
If @EmployeeName Is Not Null 
Set @SQLQuery = @SQLQuery + ' AND (DriverName LIKE ''%'+@EmployeeName+'%'') '
Print @sqlQuery
Exec (@SQLQuery) 
End


Function using
C#
public DataTable SearchDataA(string ConnectionString,string EmployeeNo,string EmployeeName, DateTime StartDate, DateTime EndDate)
{
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "CollectsearchData";//work
cmd.Parameters.Add("@StartDate", SqlDbType.DateTime);
cmd.Parameters.Add("@EndDate", SqlDbType.DateTime);
cmd.Parameters.Add("@EmployeeID", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@EmployeeName", SqlDbType.NVarChar, 50);
cmd.Parameters["@StartDate"].Value = StartDate;
cmd.Parameters["@EndDate"].Value = EndDate;
cmd.Parameters["@EmployeeID"].Value = EmployeeNo;
cmd.Parameters["@EmployeeName"].Value = EmployeeName;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
return dt;
}

interface button search
C#
try
{

CultureInfo ukCulture = new CultureInfo("en-GB"); 
FleetManagment.Fleet fleet = new FleetManagment.Fleet();
DataTable Table = fleet.SearchDataA("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "",textBox3.Text,textBox4.Text, DateTime.Parse(textBox1.Text, ukCulture.DateTimeFormat), Convert.ToDateTime(textBox2.Text, ukCulture.DateTimeFormat));
dataGridView1.DataSource = Table;
dataGridView1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex + "error");
}
Posted
Updated 3-Sep-14 9:41am
v2
Comments
PhilLenoir 3-Sep-14 14:18pm    
Why are you casting your dates as varchars? What is the data type of the database fields? Comparisions between datestrings are tricky, abstract date/time types are far more reliable.

Are you sure that you are not passing blank strings for the dates? You also have potential issues with Month/Day ordering for date validity.
RAHUL(10217975) 3-Sep-14 14:36pm    
If you are casting @StartDate to varchar then you have to cast joindate to varchar also then only this query will work
Richard Deeming 3-Sep-14 14:57pm    
Why have you reposted the same question?
http://www.codeproject.com/Questions/814285/How-to-make-search-between-two-dates-accept-null-n[^]

If you want to update your question, use the "Improve question" link instead of reposting it.

All you need to do is choose a value if one of the dates is null. For example you can do

SQL
COALESCE(@EndDate, GetDate())


which will use today's date if @EndDate is null.
 
Share this answer
 
change ur stored procedure to something like this,it may work
SQL
ALTER proc [dbo].[CollectsearchData]
@StartDate datetime, 
@EndDate datetime,
@EmployeeID NVARCHAR(50),
@EmployeeName nvarchar(50)
as
Begin
Declare @SQLQuery as nvarchar(2000)
SET @SQLQuery ='SELECT * from ViewEmployeeTest Where (1=1)' 
Set @SQLQuery = @SQLQuery + 'And ('+ @EmployeeID+'IS NULL OR  EmployeeID = '+ @EmployeeID+') '
Set @SQLQuery = @SQLQuery + 'And ('+ @EmployeeName+'IS NULL OR  DriverName LIKE ''%'+ @EmployeeName+'%'') '
Set @SQLQuery = @SQLQuery + 'And ('+ @StartDate+'IS NULL OR  joindate >= '+ @StartDate+') '
Set @SQLQuery = @SQLQuery + 'And ('+ @EndDate+'IS NULL OR  joindate <= '+ @EndDate+') '
Print @sqlQuery
Exec (@SQLQuery) 
End


;-)
 
Share this answer
 
When sending parameters to a database make sure they are DBNull.Value when they are NULL. Otherwise the parameter is not send to the database.
OR
Change your Sp parms to:
SQL
ALTER proc [dbo].[CollectsearchData]
@StartDate datetime = null, 
@EndDate datetime = null,
@EmployeeID NVARCHAR(50),
@EmployeeName nvarchar(50)


In that case you do not have to send the date parameters to the sp
 
Share this answer
 
v2

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