[RESOLVED]How to Query Data from GridView

Dear All,

I had link  DB with Gridview, May i know how to query it when i text in data in textbox or select in dropdownlist ?

asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
        <AlternatingItemTemplate>
            <tr style="background-color:white;">
                <td>
                    <asp:Label ID="DATELabel" runat="server" Text='<%# Eval("DATE") %>' />
                </td>
                <td>
                    <asp:Label ID="ITEMLabel" runat="server" Text='<%# Eval("ITEM") %>' />
                </td>
                <td>
                    <asp:Label ID="ITEM_TYPELabel" runat="server" Text='<%# Eval("ITEM_TYPE") %>' />
                </td>
                <td>
                    <asp:Label ID="DESCRIPTIONLabel" runat="server"
                        Text='<%# Eval("DESCRIPTION") %>' />
                </td>
                <td>
                    <asp:Label ID="MATERIALLabel" runat="server" Text='<%# Eval("MATERIAL") %>' />
                </td>
                <td>
                    <asp:Label ID="NET_WEIGHTLabel" runat="server"
                        Text='<%# Eval("NET_WEIGHT") %>' />
                </td>
                <td>
                    <asp:Label ID="VENDOR_NAMELabel" runat="server"
                        Text='<%# Eval("VENDOR_NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="REASONCODELabel" runat="server"
                        Text='<%# Eval("REASONCODE") %>' />
                </td>
                <td>
                    <asp:Label ID="LOCATORLabel" runat="server" Text='<%# Eval("LOCATOR") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_SOURCELabel" runat="server"
                        Text='<%# Eval("CONTAINER_SOURCE") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_TYPELabel" runat="server"
                        Text='<%# Eval("CONTAINER_TYPE") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_QTYLabel" runat="server"
                        Text='<%# Eval("CONTAINER_QTY") %>' />
                </td>
                <td>
                    <asp:Label ID="STATUSLabel" runat="server" Text='<%# Eval("STATUS") %>' />
                </td>
                <td>
                    <asp:Label ID="DOCNUMLabel" runat="server" Text='<%# Eval("DOCNUM") %>' />
                </td>
                <td>
                    <asp:Label ID="REMARKLabel" runat="server" Text='<%# Eval("REMARK") %>' />
                </td>
                <td>
                    <asp:Label ID="CREATEUSERLabel" runat="server"
                        Text='<%# Eval("CREATEUSER") %>' />
                </td>
                <td>
                    <asp:Label ID="CREATEDATELabel" runat="server"
                        Text='<%# Eval("CREATEDATE") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="background-color:White;color: #284775;">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
                        Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                        Text="Cancel" />
                </td>
                <td>
                    <asp:TextBox ID="DATETextBox" runat="server" Text='<%# Bind("DATE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ITEMTextBox" runat="server" Text='<%# Bind("ITEM") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ITEM_TYPETextBox" runat="server"
                        Text='<%# Bind("ITEM_TYPE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="DESCRIPTIONTextBox" runat="server"
                        Text='<%# Bind("DESCRIPTION") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MATERIALTextBox" runat="server"
                        Text='<%# Bind("MATERIAL") %>' />
                </td>
                <td>
                    <asp:TextBox ID="NET_WEIGHTTextBox" runat="server"
                        Text='<%# Bind("NET_WEIGHT") %>' />
                </td>
                <td>
                    <asp:TextBox ID="VENDOR_NAMETextBox" runat="server"
                        Text='<%# Bind("VENDOR_NAME") %>' />
                </td>
                <td>
                    <asp:TextBox ID="REASONCODETextBox" runat="server"
                        Text='<%# Bind("REASONCODE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="LOCATORTextBox" runat="server" Text='<%# Bind("LOCATOR") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CONTAINER_SOURCETextBox" runat="server"
                        Text='<%# Bind("CONTAINER_SOURCE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CONTAINER_TYPETextBox" runat="server"
                        Text='<%# Bind("CONTAINER_TYPE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CONTAINER_QTYTextBox" runat="server"
                        Text='<%# Bind("CONTAINER_QTY") %>' />
                </td>
                <td>
                    <asp:TextBox ID="STATUSTextBox" runat="server" Text='<%# Bind("STATUS") %>' />
                </td>
                <td>
                    <asp:TextBox ID="DOCNUMTextBox" runat="server" Text='<%# Bind("DOCNUM") %>' />
                </td>
                <td>
                    <asp:TextBox ID="REMARKTextBox" runat="server" Text='<%# Bind("REMARK") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CREATEUSERTextBox" runat="server"
                        Text='<%# Bind("CREATEUSER") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CREATEDATETextBox" runat="server"
                        Text='<%# Bind("CREATEDATE") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server"
                style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                <tr>
                    <td>
                        No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                        Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                        Text="Clear" />
                </td>
                <td>
                    <asp:TextBox ID="DATETextBox" runat="server" Text='<%# Bind("DATE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ITEMTextBox" runat="server" Text='<%# Bind("ITEM") %>' />
                </td>
                <td>
                    <asp:TextBox ID="ITEM_TYPETextBox" runat="server"
                        Text='<%# Bind("ITEM_TYPE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="DESCRIPTIONTextBox" runat="server"
                        Text='<%# Bind("DESCRIPTION") %>' />
                </td>
                <td>
                    <asp:TextBox ID="MATERIALTextBox" runat="server"
                        Text='<%# Bind("MATERIAL") %>' />
                </td>
                <td>
                    <asp:TextBox ID="NET_WEIGHTTextBox" runat="server"
                        Text='<%# Bind("NET_WEIGHT") %>' />
                </td>
                <td>
                    <asp:TextBox ID="VENDOR_NAMETextBox" runat="server"
                        Text='<%# Bind("VENDOR_NAME") %>' />
                </td>
                <td>
                    <asp:TextBox ID="REASONCODETextBox" runat="server"
                        Text='<%# Bind("REASONCODE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="LOCATORTextBox" runat="server" Text='<%# Bind("LOCATOR") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CONTAINER_SOURCETextBox" runat="server"
                        Text='<%# Bind("CONTAINER_SOURCE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CONTAINER_TYPETextBox" runat="server"
                        Text='<%# Bind("CONTAINER_TYPE") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CONTAINER_QTYTextBox" runat="server"
                        Text='<%# Bind("CONTAINER_QTY") %>' />
                </td>
                <td>
                    <asp:TextBox ID="STATUSTextBox" runat="server" Text='<%# Bind("STATUS") %>' />
                </td>
                <td>
                    <asp:TextBox ID="DOCNUMTextBox" runat="server" Text='<%# Bind("DOCNUM") %>' />
                </td>
                <td>
                    <asp:TextBox ID="REMARKTextBox" runat="server" Text='<%# Bind("REMARK") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CREATEUSERTextBox" runat="server"
                        Text='<%# Bind("CREATEUSER") %>' />
                </td>
                <td>
                    <asp:TextBox ID="CREATEDATETextBox" runat="server"
                        Text='<%# Bind("CREATEDATE") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr style="background-color:#DCDCDC;color: #000000;">
                <td>
                    <asp:Label ID="DATELabel" runat="server" Text='<%# Eval("DATE") %>' />
                </td>
                <td>
                    <asp:Label ID="ITEMLabel" runat="server" Text='<%# Eval("ITEM") %>' />
                </td>
                <td>
                    <asp:Label ID="ITEM_TYPELabel" runat="server" Text='<%# Eval("ITEM_TYPE") %>' />
                </td>
                <td>
                    <asp:Label ID="DESCRIPTIONLabel" runat="server"
                        Text='<%# Eval("DESCRIPTION") %>' />
                </td>
                <td>
                    <asp:Label ID="MATERIALLabel" runat="server" Text='<%# Eval("MATERIAL") %>' />
                </td>
                <td>
                    <asp:Label ID="NET_WEIGHTLabel" runat="server"
                        Text='<%# Eval("NET_WEIGHT") %>' />
                </td>
                <td>
                    <asp:Label ID="VENDOR_NAMELabel" runat="server"
                        Text='<%# Eval("VENDOR_NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="REASONCODELabel" runat="server"
                        Text='<%# Eval("REASONCODE") %>' />
                </td>
                <td>
                    <asp:Label ID="LOCATORLabel" runat="server" Text='<%# Eval("LOCATOR") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_SOURCELabel" runat="server"
                        Text='<%# Eval("CONTAINER_SOURCE") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_TYPELabel" runat="server"
                        Text='<%# Eval("CONTAINER_TYPE") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_QTYLabel" runat="server"
                        Text='<%# Eval("CONTAINER_QTY") %>' />
                </td>
                <td>
                    <asp:Label ID="STATUSLabel" runat="server" Text='<%# Eval("STATUS") %>' />
                </td>
                <td>
                    <asp:Label ID="DOCNUMLabel" runat="server" Text='<%# Eval("DOCNUM") %>' />
                </td>
                <td>
                    <asp:Label ID="REMARKLabel" runat="server" Text='<%# Eval("REMARK") %>' />
                </td>
                <td>
                    <asp:Label ID="CREATEUSERLabel" runat="server"
                        Text='<%# Eval("CREATEUSER") %>' />
                </td>
                <td>
                    <asp:Label ID="CREATEDATELabel" runat="server"
                        Text='<%# Eval("CREATEDATE") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="1"
                            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                            <tr runat="server" style="background-color:#DCDCDC;color: #000000;">
                                <th runat="server">
                                    DATE</th>
                                <th runat="server">
                                    ITEM</th>
                                <th runat="server">
                                    ITEM_TYPE</th>
                                <th runat="server">
                                    DESCRIPTION</th>
                                <th runat="server">
                                    MATERIAL</th>
                                <th runat="server">
                                    NET_WEIGHT</th>
                                <th runat="server">
                                    VENDOR_NAME</th>
                                <th runat="server">
                                    REASONCODE</th>
                                <th runat="server">
                                    LOCATOR</th>
                                <th runat="server">
                                    CONTAINER_SOURCE</th>
                                <th runat="server">
                                    CONTAINER_TYPE</th>
                                <th runat="server">
                                    CONTAINER_QTY</th>
                                <th runat="server">
                                    STATUS</th>
                                <th runat="server">
                                    DOCNUM</th>
                                <th runat="server">
                                    REMARK</th>
                                <th runat="server">
                                    CREATEUSER</th>
                                <th runat="server">
                                    CREATEDATE</th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server"
                        style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                        <asp:DataPager ID="DataPager1" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                                    ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
                <td>
                    <asp:Label ID="DATELabel" runat="server" Text='<%# Eval("DATE") %>' />
                </td>
                <td>
                    <asp:Label ID="ITEMLabel" runat="server" Text='<%# Eval("ITEM") %>' />
                </td>
                <td>
                    <asp:Label ID="ITEM_TYPELabel" runat="server" Text='<%# Eval("ITEM_TYPE") %>' />
                </td>
                <td>
                    <asp:Label ID="DESCRIPTIONLabel" runat="server"
                        Text='<%# Eval("DESCRIPTION") %>' />
                </td>
                <td>
                    <asp:Label ID="MATERIALLabel" runat="server" Text='<%# Eval("MATERIAL") %>' />
                </td>
                <td>
                    <asp:Label ID="NET_WEIGHTLabel" runat="server"
                        Text='<%# Eval("NET_WEIGHT") %>' />
                </td>
                <td>
                    <asp:Label ID="VENDOR_NAMELabel" runat="server"
                        Text='<%# Eval("VENDOR_NAME") %>' />
                </td>
                <td>
                    <asp:Label ID="REASONCODELabel" runat="server"
                        Text='<%# Eval("REASONCODE") %>' />
                </td>
                <td>
                    <asp:Label ID="LOCATORLabel" runat="server" Text='<%# Eval("LOCATOR") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_SOURCELabel" runat="server"
                        Text='<%# Eval("CONTAINER_SOURCE") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_TYPELabel" runat="server"
                        Text='<%# Eval("CONTAINER_TYPE") %>' />
                </td>
                <td>
                    <asp:Label ID="CONTAINER_QTYLabel" runat="server"
                        Text='<%# Eval("CONTAINER_QTY") %>' />
                </td>
                <td>
                    <asp:Label ID="STATUSLabel" runat="server" Text='<%# Eval("STATUS") %>' />
                </td>
                <td>
                    <asp:Label ID="DOCNUMLabel" runat="server" Text='<%# Eval("DOCNUM") %>' />
                </td>
                <td>
                    <asp:Label ID="REMARKLabel" runat="server" Text='<%# Eval("REMARK") %>' />
                </td>
                <td>
                    <asp:Label ID="CREATEUSERLabel" runat="server"
                        Text='<%# Eval("CREATEUSER") %>' />
                </td>
                <td>
                    <asp:Label ID="CREATEDATELabel" runat="server"
                        Text='<%# Eval("CREATEDATE") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:CIMProRPT01ConnectionString %>"
        SelectCommand="SELECT [DATE], [ITEM], [ITEM_TYPE], [DESCRIPTION], [MATERIAL], [NET_WEIGHT], [VENDOR_NAME], [REASONCODE], [LOCATOR], [CONTAINER_SOURCE], [CONTAINER_TYPE], [CONTAINER_QTY], [STATUS], [DOCNUM], [REMARK], [CREATEUSER], [CREATEDATE] FROM [WM_TRANS_HISTORY]">
    </asp:SqlDataSource>
    <br />
    <br />
    <br />

</asp:Content>

Hi caulson,

From my understanding, you would like to query data to bind GridView by SqlDataSource.

If so, please try to refer to the following code:

In the aspx file:

<asp:TextBox runat="server" ID="txtSearch" AutoPostBack="true"></asp:TextBox>
        <asp:Button runat="server" Text="Click" />
        <asp:GridView runat="server" DataSourceID="SqlDataSource1"></asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:Demo01ConnectionString %>"
            DeleteCommand="DELETE FROM [Student] WHERE [Id] = @Id"
            InsertCommand="INSERT INTO [Student] ([Id], [name], [sex]) VALUES (@Id, @name, @sex)"
            SelectCommand="SELECT * FROM [Student] where name=@name" UpdateCommand="UPDATE [Student] SET [name] = @name, [sex] = @sex WHERE [Id] = @Id">
            <SelectParameters>
                <asp:ControlParameter ControlID="txtSearch" DbType="String" Name="name" PropertyName="Text"  />
            </SelectParameters>
        </asp:SqlDataSource>

Hope it can help you, if there have anything unclear, please let me know.

Best Regards,
Terry Guo

Leave a Reply