2013-05-13 93 views
0

據說我有ProjectCode文本框:創建一個搜索功能,使用存儲過程

<td align="left" width="200px"> 
    <asp:TextBox ID="TbProjectCode" runat="server" Width="194px"></asp:TextBox> 
</td> 

和一個ImageButton的:

<asp:ImageButton ID="BtnSearch" runat="server" ImageUrl="../Support/Image/MagnifierGlass.png" Width="75%" Height="75%" OnClientClick="openNewWin();return false;" /> 

和一個GridView:

<asp:Panel ID="PanelDGV" runat="server" Height="100%" ScrollBars="None" Width="100%"> 
    <asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" PageSize="2" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"> 
     <Columns> 
    <asp:BoundField DataField="ProjectCode" HeaderText="Project Code" /> 
    <asp:BoundField DataField="ProjectName" HeaderText="Project Name" /> 
    <asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit"> 
    <ItemStyle HorizontalAlign="Center"></ItemStyle> 
     </asp:ButtonField> 
      </Columns> 
      <PagerStyle CssClass="pgr"></PagerStyle> 
       <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
        </asp:GridView> 
         </asp:Panel> 

和我」 m使用帶查詢的存儲過程(我從另一個數據庫獲取值,注意Master中的雙點.. [MS_Project]):

SELECT [projectCode],[projectName] 
    FROM Master..[MS_Project] 
    WHERE [projectCode] like '%' + @ProjectCode + '%' 
    ORDER BY [projectCode] ASC 

我想創建一個搜索功能,使用戶類型到文本框中他們想要什麼項目代碼,然後單擊ImageButton的,那麼搜索結果應該顯示在GridView,反正是有這樣做?謝謝。

編輯

我在的.vb添加:

Protected Sub BtnSearch_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnSearch.Click 
    Dim ds As New DataSet() 

    Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString()) 
     Using command As New SqlCommand() 
      command.CommandType = CommandType.StoredProcedure 
      command.CommandText = "msProject_Select" 
      command.Connection = connection 

      command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text) 

      connection.Open() 
      Dim a As New SqlDataAdapter(command) 
      a.Fill(ds) 
     End Using 
    End Using 

    DGV2.DataSource = ds 
    DGV2.DataBind() 
End Sub 

末級

+0

什麼ü[R面臨的問題...標記和查詢看上去很完美?你想要.cs代碼嗎? – 2013-05-13 07:13:21

+0

@PraveenNambiar也許我需要背後的代碼或任何需要的,你能幫忙嗎?順便說一下,我使用VB。 – 2013-05-13 07:15:31

+0

我會給你的.cs代碼。使用轉換工具轉換它至少你會得到一個想法。 – 2013-05-13 07:17:25

回答

0

這是你可以做什麼:

protected void BtnSearch_Click(object sender, ImageClickEventArgs e) 
{ 
    DataSet ds = new DataSet(); 

    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ToString())) 
     { 
      using (SqlCommand command = new SqlCommand()) 
      { 
       command.CommandType = CommandType.StoredProcedure; 
       command.CommandText = "youProcedureName"; 
       command.Connection = connection; 

       command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text); 

       connection.Open(); 
       SqlDataAdapter a = new SqlDataAdapter(command); 
       a.Fill(ds); 
      } 
     } 

    DGV.DataSource = ds; 
    DGV.DataBind(); 
} 

我們的想法很簡單。在您的搜索(單擊)事件中,您正在使用新的DataSource重新綁定GridView,並使用新的SELECT查詢檢索到該新的DataSource

VB.NET

Protected Sub BtnSearch_Click(sender As Object, e As ImageClickEventArgs) 
Dim ds As New DataSet() 

Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("YourConnectionString").ToString()) 
    Using command As New SqlCommand() 
     command.CommandType = CommandType.StoredProcedure 
     command.CommandText = "youProcedureName" 
     command.Connection = connection 

     command.Parameters.AddWithValue("@ProjectCode", TbProjectCode.Text) 

     connection.Open() 
     Dim a As New SqlDataAdapter(command) 
     a.Fill(ds) 
    End Using 
End Using 

DGV.DataSource = ds 
DGV.DataBind() 
End Sub 

試試這個vb代碼。我已經使用轉換工具來獲得這個。也許你可能在這裏需要一些修改。

以下您的評論:

將此行添加到您的頁面上。

public string CfgConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("CfgConnectionString‌​").ConnectionString; 

,改變你的第一行是這樣的:

Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("CfgConnectionString").ToString()) 

而且你缺少

Dim ds As New DataSet() 
+0

我編輯數據集到我的數據集:DsPlaygroundProject並將連接字符串更改爲CF.CfgConnectionString,過程名稱爲msProject_Select,它仍然在工作 – 2013-05-13 08:05:27

+0

張貼代碼你在問題中所做的一切......在某個地方肯定是一個小小的錯誤。 – 2013-05-13 08:06:37

+0

我編輯它,請看看 – 2013-05-13 08:10:18