passing parameter to Store Procedure in SqlDataAdapter

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 14
Reputation: Texpert is an unknown quantity at this point 
Solved Threads: 0
Texpert's Avatar
Texpert Texpert is offline Offline
Newbie Poster

passing parameter to Store Procedure in SqlDataAdapter

 
0
  #1
Feb 17th, 2006
I am working on this screen, where top part of the screen is a browse/edit/add panel for table called "Package"

And depending on the Package_ID (@@Identity) I am filling a datagrid in the bottom part of the same screen which has data from various other tables , like "Components" , "formats" etc.



I have written a store procedure to get data for the datagrid, and the store procedure gets an input parameter for Package_Id.

I am plugging this existing store procedure to the SqlDataAdapter in the page. But I am not quite sure how can I pass a parameter to the Store procedure which is in the SqlDataAdapter ? when I tried to use "Property" window of SqlDataAdapter and defined parameter value to be PKG_ID.Text (from the above part of the screen), it gives me error, parameter not passed.



Also, since the Datagrid gets data from various tables, it does not generate Insert, Update and Delete statements while configuring SqlDataAdapter



Should I go for SQLCommand class and put the SQL statements inside the code behind to fill the datagrid and not use the SqlDataAdapter ?
if I do so then how to fill a datagrid ?



Can you suggest a better approach to code to achieve what I am trying to do here ?

create store procedure script

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_SelectComponentDetails]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  2. drop procedure [dbo].[sp_SelectComponentDetails]
  3. GO
  4.  
  5. SET QUOTED_IDENTIFIER OFF
  6. GO
  7. SET ANSI_NULLS OFF
  8. GO
  9.  
  10. CREATE Procedure sp_SelectComponentDetails
  11. ( @PX_Package_Id char)
  12. AS
  13.  
  14. DECLARE @myERROR int -- Local @@ERROR
  15. , @myRowCount int -- Local @@ROWCOUNT
  16.  
  17. BEGIN
  18. SELECT Component.ComponentID, Component.FormatID, Component.PackageID, Component.Description, Component.Comp_SKU_Flag,
  19. Component.MixedOrNon, Component.MixedByWho, Format.FormatType, Package.PackageName
  20. FROM Component INNER JOIN
  21. Format ON Component.FormatID = Format.FormatID INNER JOIN
  22. Package ON Component.PackageID = Package.PackageId
  23.  
  24. WHERE (Component.PackageID = @PX_Package_Id)
  25.  
  26. SELECT @myERROR = @@ERROR, @myRowCount = @@ROWCOUNT
  27. IF @myERROR != 0 GOTO HANDLE_ERROR
  28.  
  29. END
  30. RETURN
  31.  
  32. HANDLE_ERROR:
  33. --ROLLBACK TRAN
  34. RETURN @myERROR
  35.  
  36. GO
  37. SET QUOTED_IDENTIFIER ON
  38. GO
  39. SET ANSI_NULLS ON
  40. GO
part of vb code
  1. Public Sub datasave(ByVal DataSet As DataSet)
  2. If DataExists() Then
  3. ViewState.Item("_Data") = DataSet
  4. Else
  5. ViewState.Add("_Data", DataSet)
  6. End If
  7. End Sub
  8. Public Function DataRetrieve() As DataSet
  9. Return CType(ViewState.Item("_Data"), DataSet)
  10. End Function
  11. Public Function DataExists() As Boolean
  12. If Not ViewState.Item("_Data") Is Nothing Then Return True
  13. End Function
  1. Private Sub Add_Comp_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add_Comp_btn.Click
  2. If DataExists() = False Then Exit Sub
  3. DsComponents1 = DataRetrieve()
  4. Dim rowNew As System.Data.DataRow = DsComponents1.Tables(0).NewRow
  5. DsComponents1.Tables(0).Rows.Add(rowNew)
  6. CompDataGrid.EditItemIndex = CompDataGrid.Items.Count
  7. CompDataGrid.DataSource = DsComponents1
  8. CompDataGrid.DataBind()
  9. datasave(DsComponents1)
  10. End Sub
  11.  
  12. Private Sub Delete_Comp_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete_Comp_Btn.Click
  13.  
  14. Dim objItem As DataGridItem
  15. Delete_Comp_Btn.Attributes("onclick") = "return getconfirm();"
  16.  
  17. If DataExists() = False Then Exit Sub
  18. If CompDataGrid.SelectedIndex = -1 Then Exit Sub
  19. DsComponents1 = DataRetrieve()
  20. DsComponents1.Tables(0).Rows(CompDataGrid.SelectedIndex).Delete()
  21. CompDataGrid.EditItemIndex = -1
  22. CompDataGrid.SelectedIndex = -1
  23. CompDataGrid.DataSource = DsComponents1
  24. CompDataGrid.DataBind()
  25. datasave(DsComponents1)
  26.  
  27. End Sub
  28.  
  29. Private Sub Edit_Comp_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Edit_Comp_Btn.Click
  30. If DataExists() = False Then Exit Sub
  31. If CompDataGrid.SelectedIndex = -1 Then Exit Sub
  32. Dim DsComponents1 As DataSet = DataRetrieve()
  33. CompDataGrid.DataSource = DsComponents1
  34. CompDataGrid.EditItemIndex = CompDataGrid.SelectedIndex
  35. CompDataGrid.DataBind()
  36. End Sub
  37.  
  38. Private Sub Cancel_Comp_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Comp_Btn.Click
  39. If DataExists() = False Then Exit Sub
  40. CompDataGrid.SelectedIndex = -1
  41. CompDataGrid.EditItemIndex = -1
  42. CompDataGrid.DataSource = DataRetrieve()
  43. CompDataGrid.DataBind()
  44. End Sub
  45.  
  46. Private Sub OK_Comp_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Comp_Btn.Click
  47. If DataExists() = False Then Exit Sub
  48. If CompDataGrid.EditItemIndex = -1 Then Exit Sub
  49. Dim intCount As Integer
  50. Dim dscomponents As DataSet = DataRetrieve()
  51. With CompDataGrid
  52. For intCount = 1 To .Items(.EditItemIndex).Cells.Count
  53. If intCount = .Items(.EditItemIndex).Cells.Count Then Exit For
  54. If .Items(.EditItemIndex).Cells(intCount).Controls.Count Then
  55. If TypeOf (.Items(.EditItemIndex).Cells(intCount). _
  56. Controls(0)) Is TextBox Then
  57. Dim strValue As String = CType(.Items(.EditItemIndex). _
  58. Cells(intCount).Controls(0), TextBox).Text
  59. If strValue <> "" Then
  60. dscomponents.Tables(0).Rows(.EditItemIndex).Item( _
  61. intCount - 1) = strValue
  62. Else
  63. dscomponents.Tables(0).Rows(.EditItemIndex).Item( _
  64. intCount - 1) = System.DBNull.Value
  65. End If
  66. End If
  67. End If
  68. Next
  69. .SelectedIndex = -1
  70. .EditItemIndex = -1
  71. datasave(dscomponents)
  72. .DataSource = dscomponents
  73. .DataBind()
  74. End With
  75. SqlDataAdapter1.Update(DataRetrieve)
  76. CompDataGrid.DataSource = DataRetrieve()
  77. CompDataGrid.DataBind()
  78. End Sub
  79.  
  80. Private Sub Add_Components_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add_Components.Click
  81. 'If Not IsPostBack Then
  82. SqlDataAdapter1.Fill(DsComponents1)
  83. CompDataGrid.DataSource = DsComponents1
  84. CompDataGrid.DataBind()
  85. datasave(DsComponents1)
  86. 'End If
  87. End Sub

thanks in advance.
Tejoo.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC