Problem with update and delete in gridview after search

Posted by Aradhanaa under ASP.NET on 2/23/2010 | Views : 3625 | Status : [Member] | Replies : 5
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..




Responses

Posted by: Abhi2434 on: 2/23/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
See if you are using SqlDataSource for your gridview and generate automatic buttons for Grid Editing commands, you dont need to manually reset the data. Rather add some FilterExpression to do the trick.

But I think it is always better to handle the Datasource from Codebehind. To do this you need to add TemplateFields for Add/ Delete buttons, and it will be working accordingly.
See the simplest example on this regard :
http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx

I hope this would guide you.

www.abhisheksur.com

Aradhanaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Aradhanaa on: 2/24/2010 [Member] Starter

Up
0
Down
can yo tel me what kind of filterexpression would help??

Aradhanaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/24/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Something like this :

SqlDataSource2.FilterParameters.Add("qtty", TypeCode.String, val.Text);

SqlDataSource2.FilterExpression = "qtty='{0}'";


Something like this. And after this rebind the DataSource with the Gridview.


www.abhisheksur.com

Aradhanaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 2/25/2010 [Member] Bronze

Up
0
Down
Ypu can even set the filterexpressions as follows

<asp:SqlDataSource

id="SqlDataSourceSample"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
FilterExpression="Title='{0}'">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlId="DropDownListSample" PropertyName="SelectedValue"/>
</FilterParameters>
</asp:SqlDataSource>



Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Aradhanaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 6/8/2012 [Member] Starter | Points: 25

Up
0
Down
http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Aradhanaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response