Results 1 to 7 of 7

Thread: [RESOLVED] [2005] DataGridView Binding

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Resolved [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.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  2. #2
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    Re: [2005] DataGridView Binding

    If you copy and pasted that there is a space between the
    Code:
    "ReqsDataAdapter .SelectCommand"
    Fixed it..

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] DataGridView Binding

    Just a thread typo. Actual code is correct.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4
    Hyperactive Member NPassero's Avatar
    Join Date
    May 2007
    Location
    NJ
    Posts
    272

    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.

  5. #5

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] DataGridView Binding

    Quote 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.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] DataGridView Binding

    Quote 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.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width