2012-12-27 58 views
0

我有3個文本框 - 名稱,FromDate和StartDate.When我在第一個文本框中輸入名稱並單擊按鈕時,它應顯示記錄cooresponding到該記錄。如果我從日期和日期,它應該顯示具有該輸入值範圍內的出生日期的記錄。 名稱搜索正在working.But搜索基於日期不工作..我想在asp.net中的代碼。 請幫助...根據fromdate和enddate在gridbox中顯示記錄

ALTER PROCEDURE [dbo].[usp_GetSearchResume]       
@Name varchar(50)=''  
,@FromDate varchar(15)='',           
@ToDate varchar (15)=''           
AS       
BEGIN       
DECLARE @SQL AS VARCHAR(8000) 
SET @SQL = 'SELECT Name,Address,Gender,Marital_Status,Email_Id,Country,State,District,Current_Salary,Total_Salary,Experience,Current_Position,      
convert(varchar(15),DOB,101) as DOB from Resume' 
if(@Name <> '')       
BEGIN       
SET @SQL = @SQL + ' where Name like ''%'+ @Name + '%'''       
END 

IF(@FromDate<>'' and @ToDate<>'')           
begin           
set @[email protected]+'or DOB between '''+ CONVERT(varchar(12),@FromDate,101) +''' and ''+ CONVERT(varchar(12),@ToDate,101) +''' '            
End      
print @SQL   
--select (@SQL)       
exec (@SQL)       
END 

如果我不輸入名字,我從日期輸入到今天爲止,它應該基於該日期顯示記錄......請幫助

回答

0

更改您的存儲過程像下面...... 在存儲過程中,如果名稱爲空,那麼「去哪兒」是miising的日期字段。

Alter PROCEDURE [dbo].[usp_GetSearchResume]   
@Name varchar(50)='', 
@FromDate varchar(15)='', 
@ToDate varchar (15)=''  
AS BEGIN 
DECLARE @SQL AS VARCHAR(8000) 
SET @SQL = 'SELECT Name,Address,Gender,Marital_Status,Email_Id,Country,State,District,Current_Salary,Total_Salary,Experience,Current_Position, convert(varchar(15),DOB,101) as DOB from Resume' 
if(@Name <> '')  
BEGIN    
      SET @SQL = @SQL + ' where Name like ''%'+ @Name + '%'''       END 

IF(@FromDate<>'' and @ToDate<>'')         
begin   
    if(@Name <> '')  
    BEGIN   
     set @[email protected]+'or convert(varchar(15),DOB,101) between '''+ @FromDate 
+''' and ''' + @ToDate +''' ' 
    END  
    ELSE 
    BEGIN 
     set @[email protected]+' Where convert(varchar(15),DOB,101) between '''+ @FromDate +''' and ''' + @ToDate +''' ' 
    END  
End       
print @SQL   
--select (@SQL)   
exec (@SQL)     
END 
+0

你的代碼總是顯示前兩個記錄,不管日期輸入如何 – Mizbella

+0

以哪種格式將日期傳遞給存儲過程? 您需要通過這樣的 EXEC usp_GetSearchResume','12/20/2012' ,‘2012年12月27日’ – andy

+0

雅想過去日期以mm/dd/yyyy格式 – Mizbella

0

請通過下面的方法來減少動態字符串查詢問題。

它會找到近10天的記錄aspx頁面上

declare @fromDate datetime = GETDATE()-10 
declare @ToDate datetime = GETDATE() 

where 1= case when @fromDate IS NOT NULL AND @ToDate IS NULL AND CreatedOn > 
@fromDate THEN 1 
      when @fromDate IS NULL AND @ToDate IS NOT NULL AND CreatedOn < @ToDate THEN 1 
      when CreatedOn between @fromDate AND @ToDate THEN 1 end 
0

代碼如下:

</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox> 
    <asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox> 
    <asp:TextBox ID="txtToDate" runat="server"></asp:TextBox> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"> 
    <SelectParameters> 
    <asp:ControlParameter ControlID="txtName" DefaultValue="%" Name="Name" PropertyName="Text" /> 
    <asp:ControlParameter ControlID="txtFromDate " DefaultValue="%" Name="FromDate" PropertyName="Text"/> 
    <asp:ControlParameter ControlID="txtToDate" DefaultValue="%" Name="txtToDate" PropertyName="Text"/> 
    </SelectParameters> 
    </asp:SqlDataSource> 
    <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"> 
    </asp:GridView> 
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
</div> 
</form> 

和按鈕單擊事件代碼如下所示:

protected void Button1_Click(object sender, EventArgs e) 
    { 
    string name = txtName.Text; 
    string fromdate = txtFromDate.Text; 
    string todate = txtToDate.Text; 

    SqlDataSource1.SelectParameters.Clear(); 
    SqlDataSource1.SelectParameters.Add("@Name", name); 
    SqlDataSource1.SelectParameters.Add("@FromDate" , fromdate); 
    SqlDataSource1.SelectParameters.Add("@ToDate", todate); 
    GridView1.Visible = true; 
} 
0

使用以下修改的存儲過程並僅在文本框不爲空時添加參數。

ALTER PROCEDURE [dbo].[usp_GetSearchResume]       
@Name varchar(50)=NULL  
,@FromDate datetime=NULL,           
@ToDate datetime =NULL           
AS       
BEGIN       


SELECT Name,Address,Gender,Marital_Status,Email_Id,Country,State, 
     District,Current_Salary,Total_Salary,Experience,Current_Position,      
     convert(varchar(15),DOB,101) as DOB from Resume 
where Name like '%'+ISNULL(@Name,Name)+'%' 
or DOB between ISNULL(@FromDate,DOB) and ISNULL(@ToDate,DOB) 

END 

的.cs

SqlConnection con = new SqlConnection(path); 
      SqlCommand cmd = new SqlCommand("usp_GetSearchResume", con); 

     cmd.CommandType = CommandType.StoredProcedure; 


     if (!string.IsNullOrWhiteSpace(txtName.Text)) 
     { 
      cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtNames.Text; 
     } 
     if (!string.IsNullOrWhiteSpace(txtFrom.Text)) 
     { 
      DateTime str1 = Convert.ToDateTime(txtFrom.Text); 
      cmd.Parameters.Add("@FromDate", SqlDbType.DateTime).Value = str1; 
     } 
     if (!string.IsNullOrWhiteSpace(txtTo.Text)) 
     { 
      DateTime str2 = Convert.ToDateTime(txtTo.Text);   
      cmd.Parameters.Add("@ToDate", SqlDbType.DateTime).Value = str2; 
     } 
+0

請參閱最新的代碼。確保通過將日期轉換爲日期時間格式來傳遞日期。 – Arshad

+0

您的意思是'mm/dd/yyyy'格式 – Mizbella

+0

Convert.ToDateTime(txtboxID.Text); objSqlCommand.Parameters.Add(「@ yourparameter」,SqlDbType.DateTime).Value = Converteddatetimevariable – Arshad