p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Professionals (http://p2p.wrox.com/forumdisplay.php?f=352)
-   -   Pass parameters to stored procedure from GridView (http://p2p.wrox.com/showthread.php?t=72640)

priti2010 July 14th, 2011 04:09 AM

how to delete records in gridview calling a stored procedure
 
myconnection.ConnectionString = sqlConStr
If myconnection.State <> ConnectionState.Open Then myconnection.Open()
Dim mycommand As SqlCommand
Dim row As GridViewRow = DirectCast(DataGridOdin.Rows(e.RowIndex), GridViewRow)
Dim lbldeleteID As Label = DirectCast(row.FindControl("lblid"), Label)
mycommand = New SqlCommand("sp_ServiceDel", myconnection)
mycommand.CommandType = CommandType.StoredProcedure
'mycommand = New SqlCommand("delete tbl_Service where Service_ID=" + lbldeleteID.Text & "", myconnection)
MsgBox("Do you want to Delete", MsgBoxStyle.OkCancel, "Confirmation")
If (MsgBoxResult.Ok) Then
Dim a As Integer
a = mycommand.ExecuteNonQuery()
MsgBox("Record Deleted")
myconnection.Close()
Bindgrid()

Imar July 14th, 2011 05:29 AM

Ouch....

Quote:

mycommand = New SqlCommand("delete tbl_Service where Service_ID=" + lbldeleteID.Text & "", myconnection)
In case anyone sees the above post: don't copy this code as is as it's open to SQL injection. If I enter

0 OR 1=1

lin the bldeleteID control, all your records are gone. Ooops.

Also, MsgBox is a Win Forms concept, not an ASP.NET concept and it won't work as intended.

Cheers,

Imar

priti2010 July 14th, 2011 06:23 AM

not able to delete the particular selected records
 
Protected Sub DataGridOdin_RowDeleting1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles DataGridOdin.RowDeleting
Try
myconnection.ConnectionString = sqlConStr
If myconnection.State <> ConnectionState.Open Then myconnection.Open()
Dim mycommand As SqlCommand
mycommand = New SqlCommand("sp_ServiceDel", myconnection)
mycommand.CommandType = CommandType.StoredProcedure
mycommand.Parameters.Add("@Service_ID", SqlDbType.Int).Value = e.RowIndex
MsgBox("Do you want to Delete", MsgBoxStyle.OkCancel, "Confirmation")
If (MsgBoxResult.Ok) Then
Dim a As Integer
a = mycommand.ExecuteNonQuery
MsgBox("Record Deleted")
myconnection.Close()
Bindgrid()
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try

End Sub

Imar July 14th, 2011 06:45 AM

Better, but MsgBox is still a desktop thing, while this forum category and the original post deals with ASP.NET....

Imar

priti2010 July 14th, 2011 06:52 AM

Quote:

Originally Posted by Imar (Post 274302)
Better, but MsgBox is still a desktop thing, while this forum category and the original post deals with ASP.NET....

Imar

even i m working in asp.net but vb as backend and i m new to programming
if i could get some idea about it

Imar July 14th, 2011 07:18 AM

Idea about what? I thought you were answering the original post? Can you elaborate?

Imar

priti2010 July 14th, 2011 07:34 AM

Please help
 
Quote:

Originally Posted by Imar (Post 274304)
Idea about what? I thought you were answering the original post? Can you elaborate?

Imar

how i can delete or update a select record using stored procedure in gridview
i think my code is not proper so its not working properly

priti2010 July 14th, 2011 07:35 AM

Quote:

Originally Posted by Imar (Post 274304)
Idea about what? I thought you were answering the original post? Can you elaborate?

Imar

do you want to go through my full page so that you can give me an idea

Imar July 14th, 2011 07:39 AM

And what is not working? Do you get an error and if so, which one? Also, did you ditch the MsgBox stuff?

How does your stored procedure look? Is it set up to delete an item by its ID? It looks like you're not passing the record's ID, but the relative index in the GridView instead. You may want to query the GridView's DataKeys collection to get the underlying item ID.

Google knows more on this: http://www.google.com/#sclient=psy&h...w=1920&bih=959

If all this doesn't help, please post your relevant code and a clear problem description, or we're not able to help you out much.

Cheers,

Imar

Imar July 14th, 2011 07:41 AM

Quote:

do you want to go through my full page so that you can give me an idea
We probably don't want to see the full page, but a trimmed down version showing just the code necessary to explain and / or reproduce the problem.

Cheers,

Imar


All times are GMT -4. The time now is 06:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.