Hi all,
I use gridview to display the data in Item table.
The edit and delete button works fine in the gridview initially.
When I search for particular records and display in the grid the edit and delete button does not work.

<asp:GridView ID="GridView1" 
                         runat="server" 
                         AutoGenerateColumns="False" 
                         DataSourceID="SqlDataSource2" 
                         AutoGenerateDeleteButton="True" 
                         AutoGenerateEditButton="True" 
                         DataKeyNames="iid" 
                         >
                    <Columns>
<asp:BoundField DataField="iid" HeaderText="I.D" ReadOnly="True" SortExpression="iid"/>
<asp:BoundField DataField="iname" HeaderText="Name"/>
<asp:BoundField DataField="iprice" HeaderText="ItemPrice"/>
<asp:BoundField DataField="qtty" HeaderText="Quantity"/>
                    </Columns>
             </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [item]"
DeleteCommand="Delete from item where iid=@iid;"
UpdateCommand="Update item set iname=@inameiprice=@ipriceqtty=@qtty where iid=@iid;"></asp:SqlDataSource>

and the vb code is:

Dim con As New SqlClient.SqlConnection
con.ConnectionString = "..."
con.Open()
Dim cd As New SqlClient.SqlCommand
Dim r As SqlClient.SqlDataReader
cd.Connection = con
If choice.Text = "ItemID" Then
cd = New SqlClient.SqlCommand("select * from item where iid='" & val.Text & "'", con)
r = cd.ExecuteReader
GridView1.DataSourceID = String.Empty
GridView1.DataSource = r
GridView1.DataBind()
ElseIf choice.Text = "ItemName" Then
                cd = New SqlClient.SqlCommand("select * from item where iname='" & val.Text & "'", con)
                r = cd.ExecuteReader
                GridView1.DataSourceID = String.Empty
                GridView1.DataSource = r
                GridView1.DataBind()
            ElseIf choice.Text = "Price" Then
                cd = New SqlClient.SqlCommand("select * from item where iprice='" & val.Text & "'", con)
                r = cd.ExecuteReader
                GridView1.DataSourceID = String.Empty
                GridView1.DataSource = r
                GridView1.DataBind()
            ElseIf choice.Text = "qtty" Then
                cd = New SqlClient.SqlCommand("select * from item where qtty='" & val.Text & "'", con)
                r = cd.ExecuteReader
                GridView1.DataSourceID = String.Empty
                GridView1.DataSource = r
                GridView1.DataBind()
            End If

How to add specific update and delete commands for each choice.

I dono much abt asp.net..
pls help me..
thanks in advance..

You are working with SqlDataSource, so, you do not have to write any statement for updation or deletion.

You are working with SqlDataSource, so, you do not have to write any statement for updation or deletion.

Thank you for taking time to reply for my query..
Since I change the datasource in the gridview while I search.. The update and delete commands which I've set already doesnt work with the search results..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.