|
-
Jun 20th, 2007, 09:59 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] DataGridView Binding
I'm having a major brainfart today and can't think straight.
I'm binding my DataGridView to a BindingSource, and then binding the BindingSource to a DataTable like this:
Code:
Private Sub(ByVal part As String)
' SET CONNECTION STRING
Me.PartReqsCon.ConnectionString = DataBaseTools.GetConnectionString("PartReqs")
' SET COMMAND STRING
Dim cmdString As String = "SELECT * FROM PartReqs_tbl WHERE PartKey = " & part
' RENEW DATA ADAPTER
Me.ReqsDataAdapter = New OleDbDataAdapter
Me.ReqsDataAdapter.SelectCommand = New OleDbCommand(cmdString, Me.PartReqsCon)
' CREATE COMMAND BUILDER FOR DATA ADAPTER
Dim commandBuilder As New OleDbCommandBuilder(Me.ReqsDataAdapter)
' CREATE DATA TABLE AND SET LOCALE
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
' FILL DATA TABLE
Me.ReqsDataAdapter.Fill(table)
' BIND DATA TABLE
Me.ReqsBindingSource.DataSource = table
' DATAGRIDVIEW IS BOUND FROM DESIGNER
End Sub
I'm getting an error on the Fill method:
"No value given for one or more required parameters."
I created a breakpoint and it looks like all of the requirements are there. What am I missing?
Last edited by circuits2; Jun 20th, 2007 at 10:07 AM.
-
Jun 20th, 2007, 10:06 AM
#2
Hyperactive Member
Re: [2005] DataGridView Binding
If you copy and pasted that there is a space between the
Code:
"ReqsDataAdapter .SelectCommand"
Fixed it..
-
Jun 20th, 2007, 10:08 AM
#3
Thread Starter
Frenzied Member
Re: [2005] DataGridView Binding
Just a thread typo. Actual code is correct.
-
Jun 20th, 2007, 10:10 AM
#4
Hyperactive Member
Re: [2005] DataGridView Binding
I would bind it at run time, if you have not tried that already. based on this it is binding to an empty bindingsource because it hasn't been filled yet.
-
Jun 20th, 2007, 10:13 AM
#5
Thread Starter
Frenzied Member
Re: [2005] DataGridView Binding
 Originally Posted by NPassero
I would bind it at run time, if you have not tried that already. based on this it is binding to an empty bindingsource because it hasn't been filled yet.
Thanks for the suggestion, but that would not cause the error at the Fill method that I am trying to resolve.
-
Jun 20th, 2007, 10:15 AM
#6
Re: [2005] DataGridView Binding
What data type is the PartKey column? If it's not a number then that query can't work because you haven't enclosed the value in single quotes or any other type delimiters. Regardless of the type, do NOT use string concatenation to build an SQL statement. I'd be a very rich man if I had a dollar for every time I'd posted that. Always use parameters.
http://www.vbforums.com/showthread.php?t=469872
By the way, your thread title is inaccurate because this issue is completely unrelated to data-binding and the DataGridView.
Note also that if you've bound the grid to the BindingSource before binding the BindingSource to the DataTable then no columns will be created in your grid. If you haven't added the columns to the grid in the designer then your grid will be empty. If you want the grid to create the columns itself then the DataTable must be bound to the BindingSource first. That way the grid will find the columns when it is bound to the BindingSource, which is when it creates its own columns.
-
Jun 20th, 2007, 10:32 AM
#7
Thread Starter
Frenzied Member
Re: [2005] DataGridView Binding
 Originally Posted by jmcilhinney
What data type is the PartKey column? If it's not a number then that query can't work because you haven't enclosed the value in single quotes or any other type delimiters. Regardless of the type, do NOT use string concatenation to build an SQL statement. I'd be a very rich man if I had a dollar for every time I'd posted that. Always use parameters.
http://www.vbforums.com/showthread.php?t=469872
By the way, your thread title is inaccurate because this issue is completely unrelated to data-binding and the DataGridView.
Note also that if you've bound the grid to the BindingSource before binding the BindingSource to the DataTable then no columns will be created in your grid. If you haven't added the columns to the grid in the designer then your grid will be empty. If you want the grid to create the columns itself then the DataTable must be bound to the BindingSource first. That way the grid will find the columns when it is bound to the BindingSource, which is when it creates its own columns.
Thanks John! I realized how poorly I had written that code (What was I thinking?!?! ). I revised the code to my normal writing methods and the issue was resolved.
New code:
Code:
Private Sub BindReqs(ByVal part As String)
' CREATE CONNECTION
Dim reqsCon As OleDbConnection = New OleDbConnection(DataBaseTools.GetconnectionString("PartReqs"))
' CREATE COMMAND AND SET PARAMETERS
Dim reqsCmd As OleDbCommand = New OleDbCommand("SELECT * FROM PartReqs_tbl WHERE PartKey = @part", reqsCon)
reqsCmd.Parameters.AddWithValue("@part", part)
' CREATE DATA ADAPTER
Dim reqsDA As OleDbDataAdapter = New OleDbDataAdapter(reqsCmd)
' CREATE COMMANDBUILDER
Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(reqsDA)
' CREATE DATATABLE
Dim table As New DataTable()
' FILL DATATABLE
reqsDA.Fill(table)
' BIND TO BINDINGSOURCE
Me.ReqsBindingSource.DataSource = table
' BIND TO DATAGRIDVIEW
Me.ReqsDataGridView.DataSource = Me.ReqsBindingSource
End Sub
Last edited by circuits2; Jun 20th, 2007 at 10:58 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|