Question

Supposedly I have the ProjectCode textbox:

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

and one imagebutton:

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

and a 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>

and i"m using a stored procedure with query (i take the value from another database, notice the double dot in Master..[MS_Project]):

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

I want to create a search function, so the user type into the textbox what project code they want, then click the imagebutton, then the search result should be displayed in the gridview, is there anyway to do this? thank you.

EDIT

I add in the .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

End Class

Was it helpful?

Solution

This is what you can do:

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();
}

The idea is pretty simple. On your Search(click) event you are rebinding your GridView with a new DataSource that you retrieved with your new SELECT query.

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

Try this vb code. I have used conversion tool to get this. Maybe you might require some modifications here.

Following your comment:

Add this line on your page.

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

and change your first line like this:

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

Also you are missing

Dim ds As New DataSet()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top