473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to get a DataGrid that uses a Stored Procedure to initially populate to refresh

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.



Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4.  
  5.  
  6. Public Class WholeAppleInsertion
  7.  
  8.     ' This form is for entering Apple Names with their corresponding
  9.     ' Type. I'm presently trying to get it to work where after submitting
  10.     ' the new name that the data grid will reset itself via calling upon the 
  11.     ' stored procedure originally used to populate the grid so that it can 
  12.     ' repopulate it, giving an updated view to the user
  13.  
  14.  
  15.  
  16.     Private Sub WholeAppleInsertion_Load( _
  17.         ByVal sender As System.Object, _
  18.         ByVal e As System.EventArgs) _
  19.         Handles MyBase.Load
  20.         'TODO: This line of code loads data into the 'VaedaAlefDataSet1.spSelectallWholeApple' _
  21.         ' table. You can move, or remove it, as needed.
  22.         Me.spSelectallWholeAppleTableAdapter1.Fill(Me.VaedaAlefDataSet1.spSelectallWholeApple)
  23.  
  24.         Me.spSelectAppleTypeTableAdapter.Fill(Me.VaedaAlefDataSet.spSelectAppleTypeTableAdapterType)
  25.  
  26.         Me.spSelectallWholeAppleTableAdapter.Fill( _
  27.         Me.VaedaAlefDataSet.spSelectallWholeApple)
  28.  
  29.     End Sub
  30.  
  31.  
  32.  
  33.     ' This is the data grid that holds the list of Apple names with their types 
  34.     ' that are found in the database. THIS is the data grid that I want to get to
  35.     ' update, to refresh, or however you'd say it, whenever the button is pushed.
  36.  
  37.     Private Sub DataGridView1_CellContentClick( _
  38.         ByVal sender As System.Object, _
  39.         ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
  40.         Handles DataGridView1.CellContentClick
  41.  
  42.     End Sub
  43.  
  44.  
  45.  
  46. '########THIS IS THE BUTTON CLICK EVENT I WANT TO 
  47. '########TRIGGER THE REFRESHING OF THE DATAGRID
  48.  
  49.     Public Function EnterApple_Click( _
  50.         ByVal sender As System.Object, _
  51.         ByVal e As System.EventArgs) As SqlDataReader _
  52.         Handles EnterApple.Click
  53.  
  54.  
  55.         Using sqlconn As New SqlConnection
  56.             Dim retval As Integer
  57.  
  58.             sqlconn.ConnectionString = "Data Source=SQUASHERZ-PC\SQLEXPRESS;Initial Catalog=VaedaAlef;Integrated Security=True"
  59.             sqlconn.Open()
  60.  
  61.             Dim sqlComm As New SqlCommand
  62.             sqlComm.Connection = sqlconn
  63.             sqlComm.CommandType = CommandType.StoredProcedure
  64.             sqlComm.CommandText = "spInserlectWholeApple"
  65.  
  66.             Try
  67.                 sqlComm.Parameters.Add("@Name", SqlDbType.VarChar)
  68.                 sqlComm.Parameters.Add("@Type", SqlDbType.SmallInt)
  69.                 sqlComm.Parameters("@Name").Direction = ParameterDirection.Input
  70.                 sqlComm.Parameters("@Name").Value = TxtAppleName.Text
  71.                 sqlComm.Parameters("@Type").Direction = ParameterDirection.Input
  72.                 sqlComm.Parameters("@Type").Value = ParameterDirection.Input = ComboBox1.SelectedValue
  73.  
  74.                 retval = sqlComm.ExecuteNonQuery()
  75.             Catch Sqlex As SqlException
  76.                 MessageBox.Show(Sqlex.Message)
  77.             End Try
  78.  
  79.  
  80.             TxtAppleName.Text = ""
  81.  
  82.  
  83.             If retval >= 1 Then
  84.                 MsgBox("something happened")
  85.  
  86.  
  87.             End If
  88.  
  89.  
  90. ' //Here below are my attempts at getting this to refresh
  91. ' //It likely makes it clear that I don't know what I'm doing
  92. ' PLEASE HELP!
  93.  
  94.             Me.WholeAppleInsertion_ResetBindings()
  95.  
  96.             Me.spSelectallWholeAppleBindingSource1.ResetItem(0)
  97.  
  98.             Me.DataGridView1.Refresh()
  99.  
  100.             sqlconn.Close()
  101.  
  102.         End Using
  103.  
  104.  
  105.     End Function
  106.  
  107.  
  108.  
  109.     Private Sub TextBox1_TextChanged( _
  110.         ByVal sender As System.Object, _
  111.         ByVal e As System.EventArgs) _
  112.         Handles TxtAppleName.TextChanged
  113.  
  114.     End Sub
  115.  
  116.     Private Sub ComboBox1_SelectedIndexChanged( _
  117.         ByVal sender As System.Object, _
  118.         ByVal e As System.EventArgs)
  119.  
  120.  
  121.     End Sub
  122.  
  123.     Private Sub ComboBox1_SelectedIndexChanged_1( _
  124.         ByVal sender As System.Object, _
  125.         ByVal e As System.EventArgs) _
  126.         Handles ComboBox1.SelectedIndexChanged
  127.  
  128.     End Sub
  129.  
  130.  
  131.     Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
  132.             ByVal e As System.ComponentModel.DoWorkEventArgs) _
  133.             Handles BackgroundWorker1.DoWork
  134.  
  135.     End Sub
  136.  
  137.     Private Sub spSelectallWholeAppleBindingSource1_CurrentChanged _
  138.         (ByVal sender As System.Object, _
  139.          ByVal e As System.EventArgs) _
  140.          Handles spSelectallWholeAppleBindingSource1.CurrentChanged
  141.  
  142.     End Sub
  143.     'This sub is the binding source that retrieves the data for the data grid
  144.     'This is what I'm trying to figure out how to get it to refresh so that the data
  145.     'grid displays the new information whenever a new name is submitted via the 'submit
  146.     'new button' button.
  147.     Private Sub spSelectallWholeAppleBindingSource_CurrentChanged _
  148.         (ByVal sender As System.Object, _
  149.          ByVal e As System.EventArgs) _
  150.          Handles spSelectallWholeAppleBindingSource.CurrentChanged
  151.  
  152.  
  153.     End Sub
  154.  
  155.  
  156. End Class
Mar 3 '10 #1
3 4435
semomaniz
210 Expert 100+
define a data set then assign the retrieved data to the data set then bind the data to the datagrid.

Me.DataGridView1.DataSource = Place the dataset here
Me.DataGridView1.DataBind()
Mar 4 '10 #2
semomaniz,

Well part of what you gave me is doing something and the other part I'm not figuring it out. Heck I may be doing it all wrong. I'm so new I'm not even sure. By placing the first one, the

Me.DataGridView1.DataSource = Place the dataset here

That makes it so that when I press the button something happens to the DataGridView, it goes empty. Which makes me unsure if I'm following you on this or if I'm doing it right. If there's any way, and I know this is asking more of your time and showing my noobness, but could you please give an example in code as to what you're describing would look like?

Oh, and another problem, and perchance this would vanish if I was doing things correctly, but when I go to do the

Me.DataGridView1.DataBind()

Visual Studio only wants to do a

Me.DataGridView1.DataBindings()

which it then underlines with the blue line thingy and claims (and I'll be honest and say I'm not quite understanding what it's asking for) that "Property access must assign to the property or use its value" and that one just has me even more befuddled.

I'm tickled pink that I was able to get the datagridtable to at least do something. I hope you, or someone, can help me with this.

Thank you again for your time.
Mar 11 '10 #3
Frinavale
9,735 Expert Mod 8TB
I didn't think that you could bind a DataGridView to a DataSet...

A DataSet contains multiple tables but a DataGridView really should only display one table.

You should set the DataGridView.DataSource a specific table in the DataSet...not to the DataSet itself.

If you require data to be pulled from multiple tables then you should use the tables within the DataSet to return a View or a new table with the columns you need to display.

Once you have changed the DataSource the DataGridView should display the new source. If not, call the DataBind method...if this still doesn't display the refreshed data also call the DataGridView.Update() method to repaint the DataGridView.

-Frinny
Mar 12 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: harborboy76 | last post by:
Hi, I have a stored procedure that does a lot of INSERT/UDATE to 3 tables. And When I call the stored procedure, I get a Transaction Log Full error. When I want to do is turning off the...
2
by: Max | last post by:
Is it possible or more effecient to use a stored procedure to populate a datagrid when using datagrid or custom paging? Is it (ADO.NET?) pulling the entire table into the dataset or is it just...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
11
by: Fred Nelson | last post by:
I have an application in which it would be VERY beneficial if I could obtain the names of the colums in a datagrid. For example dim datagrid1 as new datagrid datagrid1.datasource = (stored...
1
by: chreo | last post by:
Hello. Does anybody had problem with VB application with MS SQL Database? I have DATAGRID which shows TABLE from DATABASE. Next I add new ROW to TABLE with SQLcommand (stored procedure) ...
1
by: Sharon | last post by:
Hello All, Is it possible to update Sql Table through DataGrid. I have a DataGrid which is being populated through a stored procedure, all i wanted to do is to update one field...
3
by: boyindie86 | last post by:
How can i pull images out of an mysql table and populate the below datagrid. I have all my images stored in an external store, and the addresses are stored in my DB, i am not sure how to pull these...
11
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
7
by: Mark B | last post by:
Can someone write some VB.Net example code for me that does this: 1) Creates a gridview control with the results of a SQL stored procedure that has 1 parameter (text) 2) Adds an extra column...
3
by: FrustratedNoob | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.