0

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..

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

Attachments item.jpg 21.99 KB
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Aradhanaa
0

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

0

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..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.