I've got a database that I've been working on for over a year and a half but I've just recently finished the back end to the point of feeling comfortable with starting on a front end.
So while I've been able to pick up a great deal just stumbling through on my own with the several apress books I've gotten a hold of I've figured out a great deal and have some half decent forms for data entry but for the life of me I can't figure out how to get the databinding to do whatever it does again after having entered data into the database.
My problem placed more concisely
My form's data grid populates from a stored procedure via a binding source.
My form's button then uses another stored procedure to enter in the data into the database.
What I want to do is have the datagrid recall the stored procedure and refresh the grid with the newly modified data included.
This is what I've been racking my brain over for some time, and scouring the internet and my various apress books for, is figuiring out why none of my attempts at doing this are working.
I'm just finding most all of the stuff out there about datagrids and the such are not at all tailored towards a person trying to do all their interactions via stored procedures. I know I can do the basics, and the forms work and I know that if I just close the form and access it manually again that the datagrid reloads with the newly input data, but I'm really trying to get it so that I'm making a UI that is as easy to use as I can get it.
I'd appreciate it if you, or anyone, could point out what I'm doing wrong, or not doing right, or both.
Sorry the code is so weird here. I accidentally posted this question in the old vb land.
Here's a link if you want a prettier view of the code--
http://bytes.com/topic/visual-basic/answers/879935-how-get-datagrid-uses-stored-procedure-initially-populate-refresh#post354 0295
Expand|Select|W rap|Line Numbers
1. ===========
2.
3. Imports System
4. Imports System.Data
5. Imports System.Data.Sql Client
6.
7.
8. Public Class WholeAppleInser tion
9.
10. ' This form is for entering Apple Names with their corresponding
11. ' Type. I'm presently trying to get it to work where after submitting
12. ' the new name that the data grid will reset itself via calling upon the
13. ' stored procedure originally used to populate the grid so that it can
14. ' repopulate it, giving an updated view to the user
15.
16.
17.
18. Private Sub WholeAppleInser tion_Load( _
19. ByVal sender As System.Object, _
20. ByVal e As System.EventArg s) _
21. Handles MyBase.Load
22. 'TODO: This line of code loads data into the 'VaedaAlefDataS et1.spSelectall WholeApple' _
23. ' table. You can move, or remove it, as needed.
24. Me.spSelectallW holeAppleTableA dapter1.Fill(Me .VaedaAlefDataS et1.spSelectall WholeApple)
25.
26. Me.spSelectAppl eTypeTableAdapt er.Fill(Me.Vaed aAlefDataSet.sp SelectAppleType TableAdapterTyp e)
27.
28. Me.spSelectallW holeAppleTableA dapter.Fill( _
29. Me.VaedaAlefDat aSet.spSelectal lWholeApple)
30.
31. End Sub
32.
33.
34.
35. ' This is the data grid that holds the list of Apple names with their types
36. ' that are found in the database. THIS is the data grid that I want to get to
37. ' update, to refresh, or however you'd say it, whenever the button is pushed.
38.
39. Private Sub DataGridView1_C ellContentClick ( _
40. ByVal sender As System.Object, _
41. ByVal e As System.Windows. Forms.DataGridV iewCellEventArg s) _
42. Handles DataGridView1.C ellContentClick
43.
44. End Sub
45.
46.
47.
48. '########THIS IS THE BUTTON CLICK EVENT I WANT TO
49. '########TRIGGE R THE REFRESHING OF THE DATAGRID
50.
51. Public Function EnterApple_Clic k( _
52. ByVal sender As System.Object, _
53. ByVal e As System.EventArg s) As SqlDataReader _
54. Handles EnterApple.Clic k
55.
56.
57. Using sqlconn As New SqlConnection
58. Dim retval As Integer
59.
60. sqlconn.Connect ionString = "Data Source=SQUASHER Z-PC\SQLEXPRESS;I nitial Catalog=VaedaAl ef;Integrated Security=True"
61. sqlconn.Open()
62.
63. Dim sqlComm As New SqlCommand
64. sqlComm.Connect ion = sqlconn
65. sqlComm.Command Type = CommandType.Sto redProcedure
66. sqlComm.Command Text = "spInserlectWho leApple"
67.
68. Try
69. sqlComm.Paramet ers.Add("@Name" , SqlDbType.VarCh ar)
70. sqlComm.Paramet ers.Add("@Type" , SqlDbType.Small Int)
71. sqlComm.Paramet ers("@Name").Di rection = ParameterDirect ion.Input
72. sqlComm.Paramet ers("@Name").Va lue = TxtAppleName.Te xt
73. sqlComm.Paramet ers("@Type").Di rection = ParameterDirect ion.Input
74. sqlComm.Paramet ers("@Type").Va lue = ParameterDirect ion.Input = ComboBox1.Selec tedValue
75.
76. retval = sqlComm.Execute NonQuery()
77. Catch Sqlex As SqlException
78. MessageBox.Show (Sqlex.Message)
79. End Try
80.
81.
82. TxtAppleName.Te xt = ""
83.
84.
85. If retval >= 1 Then
86. MsgBox("somethi ng happened")
87.
88.
89. End If
90.
91.
92. ' //Here below are my attempts at getting this to refresh
93. ' //It likely makes it clear that I don't know what I'm doing
94. ' PLEASE HELP!
95.
96. Me.WholeAppleIn sertion_ResetBi ndings()
97.
98. Me.spSelectallW holeAppleBindin gSource1.ResetI tem(0)
99.
100. Me.DataGridView 1.Refresh()
101.
102. sqlconn.Close()
103.
104. End Using
105.
106.
107. End Function
108.
109.
110.
111. Private Sub TextBox1_TextCh anged( _
112. ByVal sender As System.Object, _
113. ByVal e As System.EventArg s) _
114. Handles TxtAppleName.Te xtChanged
115.
116. End Sub
117.
118. Private Sub ComboBox1_Selec tedIndexChanged ( _
119. ByVal sender As System.Object, _
120. ByVal e As System.EventArg s)
121.
122.
123. End Sub
124.
125. Private Sub ComboBox1_Selec tedIndexChanged _1( _
126. ByVal sender As System.Object, _
127. ByVal e As System.EventArg s) _
128. Handles ComboBox1.Selec tedIndexChanged
129.
130. End Sub
131.
132.
133. Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object, _
134. ByVal e As System.Componen tModel.DoWorkEv entArgs) _
135. Handles BackgroundWorke r1.DoWork
136.
137. End Sub
138.
139. Private Sub spSelectallWhol eAppleBindingSo urce1_CurrentCh anged _
140. (ByVal sender As System.Object, _
141. ByVal e As System.EventArg s) _
142. Handles spSelectallWhol eAppleBindingSo urce1.CurrentCh anged
143.
144. End Sub
145. 'This sub is the binding source that retrieves the data for the data grid
146. 'This is what I'm trying to figure out how to get it to refresh so that the data
147. 'grid displays the new information whenever a new name is submitted via the 'submit
148. 'new button' button.
149. Private Sub spSelectallWhol eAppleBindingSo urce_CurrentCha nged _
150. (ByVal sender As System.Object, _
151. ByVal e As System.EventArg s) _
152. Handles spSelectallWhol eAppleBindingSo urce.CurrentCha nged
153.
154.
155. End Sub
156.
157.
158. End Class
159.
160. ############### ############### ###
I'm using Visual Studio 08 and I've been leaning on the GUI so if there are code elements that don't make sense please let me know. I'm trying to learn this as fast as I can, I'm just not learning how to fix this particular item fast enough.
So while I've been able to pick up a great deal just stumbling through on my own with the several apress books I've gotten a hold of I've figured out a great deal and have some half decent forms for data entry but for the life of me I can't figure out how to get the databinding to do whatever it does again after having entered data into the database.
My problem placed more concisely
My form's data grid populates from a stored procedure via a binding source.
My form's button then uses another stored procedure to enter in the data into the database.
What I want to do is have the datagrid recall the stored procedure and refresh the grid with the newly modified data included.
This is what I've been racking my brain over for some time, and scouring the internet and my various apress books for, is figuiring out why none of my attempts at doing this are working.
I'm just finding most all of the stuff out there about datagrids and the such are not at all tailored towards a person trying to do all their interactions via stored procedures. I know I can do the basics, and the forms work and I know that if I just close the form and access it manually again that the datagrid reloads with the newly input data, but I'm really trying to get it so that I'm making a UI that is as easy to use as I can get it.
I'd appreciate it if you, or anyone, could point out what I'm doing wrong, or not doing right, or both.
Sorry the code is so weird here. I accidentally posted this question in the old vb land.
Here's a link if you want a prettier view of the code--
http://bytes.com/topic/visual-basic/answers/879935-how-get-datagrid-uses-stored-procedure-initially-populate-refresh#post354 0295
Expand|Select|W rap|Line Numbers
1. ===========
2.
3. Imports System
4. Imports System.Data
5. Imports System.Data.Sql Client
6.
7.
8. Public Class WholeAppleInser tion
9.
10. ' This form is for entering Apple Names with their corresponding
11. ' Type. I'm presently trying to get it to work where after submitting
12. ' the new name that the data grid will reset itself via calling upon the
13. ' stored procedure originally used to populate the grid so that it can
14. ' repopulate it, giving an updated view to the user
15.
16.
17.
18. Private Sub WholeAppleInser tion_Load( _
19. ByVal sender As System.Object, _
20. ByVal e As System.EventArg s) _
21. Handles MyBase.Load
22. 'TODO: This line of code loads data into the 'VaedaAlefDataS et1.spSelectall WholeApple' _
23. ' table. You can move, or remove it, as needed.
24. Me.spSelectallW holeAppleTableA dapter1.Fill(Me .VaedaAlefDataS et1.spSelectall WholeApple)
25.
26. Me.spSelectAppl eTypeTableAdapt er.Fill(Me.Vaed aAlefDataSet.sp SelectAppleType TableAdapterTyp e)
27.
28. Me.spSelectallW holeAppleTableA dapter.Fill( _
29. Me.VaedaAlefDat aSet.spSelectal lWholeApple)
30.
31. End Sub
32.
33.
34.
35. ' This is the data grid that holds the list of Apple names with their types
36. ' that are found in the database. THIS is the data grid that I want to get to
37. ' update, to refresh, or however you'd say it, whenever the button is pushed.
38.
39. Private Sub DataGridView1_C ellContentClick ( _
40. ByVal sender As System.Object, _
41. ByVal e As System.Windows. Forms.DataGridV iewCellEventArg s) _
42. Handles DataGridView1.C ellContentClick
43.
44. End Sub
45.
46.
47.
48. '########THIS IS THE BUTTON CLICK EVENT I WANT TO
49. '########TRIGGE R THE REFRESHING OF THE DATAGRID
50.
51. Public Function EnterApple_Clic k( _
52. ByVal sender As System.Object, _
53. ByVal e As System.EventArg s) As SqlDataReader _
54. Handles EnterApple.Clic k
55.
56.
57. Using sqlconn As New SqlConnection
58. Dim retval As Integer
59.
60. sqlconn.Connect ionString = "Data Source=SQUASHER Z-PC\SQLEXPRESS;I nitial Catalog=VaedaAl ef;Integrated Security=True"
61. sqlconn.Open()
62.
63. Dim sqlComm As New SqlCommand
64. sqlComm.Connect ion = sqlconn
65. sqlComm.Command Type = CommandType.Sto redProcedure
66. sqlComm.Command Text = "spInserlectWho leApple"
67.
68. Try
69. sqlComm.Paramet ers.Add("@Name" , SqlDbType.VarCh ar)
70. sqlComm.Paramet ers.Add("@Type" , SqlDbType.Small Int)
71. sqlComm.Paramet ers("@Name").Di rection = ParameterDirect ion.Input
72. sqlComm.Paramet ers("@Name").Va lue = TxtAppleName.Te xt
73. sqlComm.Paramet ers("@Type").Di rection = ParameterDirect ion.Input
74. sqlComm.Paramet ers("@Type").Va lue = ParameterDirect ion.Input = ComboBox1.Selec tedValue
75.
76. retval = sqlComm.Execute NonQuery()
77. Catch Sqlex As SqlException
78. MessageBox.Show (Sqlex.Message)
79. End Try
80.
81.
82. TxtAppleName.Te xt = ""
83.
84.
85. If retval >= 1 Then
86. MsgBox("somethi ng happened")
87.
88.
89. End If
90.
91.
92. ' //Here below are my attempts at getting this to refresh
93. ' //It likely makes it clear that I don't know what I'm doing
94. ' PLEASE HELP!
95.
96. Me.WholeAppleIn sertion_ResetBi ndings()
97.
98. Me.spSelectallW holeAppleBindin gSource1.ResetI tem(0)
99.
100. Me.DataGridView 1.Refresh()
101.
102. sqlconn.Close()
103.
104. End Using
105.
106.
107. End Function
108.
109.
110.
111. Private Sub TextBox1_TextCh anged( _
112. ByVal sender As System.Object, _
113. ByVal e As System.EventArg s) _
114. Handles TxtAppleName.Te xtChanged
115.
116. End Sub
117.
118. Private Sub ComboBox1_Selec tedIndexChanged ( _
119. ByVal sender As System.Object, _
120. ByVal e As System.EventArg s)
121.
122.
123. End Sub
124.
125. Private Sub ComboBox1_Selec tedIndexChanged _1( _
126. ByVal sender As System.Object, _
127. ByVal e As System.EventArg s) _
128. Handles ComboBox1.Selec tedIndexChanged
129.
130. End Sub
131.
132.
133. Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object, _
134. ByVal e As System.Componen tModel.DoWorkEv entArgs) _
135. Handles BackgroundWorke r1.DoWork
136.
137. End Sub
138.
139. Private Sub spSelectallWhol eAppleBindingSo urce1_CurrentCh anged _
140. (ByVal sender As System.Object, _
141. ByVal e As System.EventArg s) _
142. Handles spSelectallWhol eAppleBindingSo urce1.CurrentCh anged
143.
144. End Sub
145. 'This sub is the binding source that retrieves the data for the data grid
146. 'This is what I'm trying to figure out how to get it to refresh so that the data
147. 'grid displays the new information whenever a new name is submitted via the 'submit
148. 'new button' button.
149. Private Sub spSelectallWhol eAppleBindingSo urce_CurrentCha nged _
150. (ByVal sender As System.Object, _
151. ByVal e As System.EventArg s) _
152. Handles spSelectallWhol eAppleBindingSo urce.CurrentCha nged
153.
154.
155. End Sub
156.
157.
158. End Class
159.
160. ############### ############### ###
I'm using Visual Studio 08 and I've been leaning on the GUI so if there are code elements that don't make sense please let me know. I'm trying to learn this as fast as I can, I'm just not learning how to fix this particular item fast enough.