Search code examples
sql-servervb.netdata-bindingdatasetbindingsource

How to properly access bindingSource to SQL Server data?


I am new to vb and to SQL Server. I would like to know the exact procedure how to bind from start and only by code, BindingSource to SQL Server data. Any help would be appreciated.

Dim connectionString = _conn_Test

dataAdapter = New SqlDataAdapter(selectCommand,           connectionString)

Dim commandBuilder = New SqlCommandBuilder(dataAdapter)

Dim table = New DataTable
table.Columns.Add("cie_code", GetType(System.String))
table.Columns.Add("cie_nom", GetType(System.String))

For i = 0 To table.Rows.Count - 1
    table.Rows.Add()
Next

dataAdapter.Fill(table)

bindingSource1.DataSource = table

Solution

  • You are mixing up some Lines that not fit properly.

    'Get data from a sqlserver and fill a datatable with those data
    Dim connectionString = _conn_Test
    Dim table = New DataTable
    dataAdapter = New SqlDataAdapter(selectCommand,connectionString)
    dataAdapter.Fill(table)
    

    So now you have a table filled with all data from your Selectcommand query.

    This table you can use as datasource for Listboxes datagridview or everything else with can have a datasource. Another way tio create a datasource is

    Dim bindingSource1 As BindingSource
    bindingSource1.DataSource = table
    

    This you can use also to populate Datagridviews or listboxes etc.

    'Bild a new table as datasource
    Dim table = New DataTable
    'Add new Colmuns to the Table
    table.Columns.Add("cie_code", GetType(System.String))
    table.Columns.Add("cie_nom", GetType(System.String))
     'Add 1 new row can be encklosed in a for loop
      Dim newrow as As DataRo = table.NewRow()
      newrow ("cie_code") = "Youtext1"
      newrow ("cie_nom") = "Yourtext2"
      table.Rows.Add(newrow)
      'Change 1 Column of of all rows
      For i as Integer = 0 To table.Rows.Count - 1
        table.Rows(i).Item("cie_nom") = "newtext"
      Next
      'Define new elements to demonstrate what to do with datasouurces
      Dim ListBox1 As ListBox
      Dim bindingSource2 As BindingSource
      This would sow a Listbox with one element newtext
      bindingSource2.DataSource = table
      listBox1.DataSource = bindingSource2
      listBox1.DisplayMember = "cie_nom"
    

    The last thing in the pot is

    Dim commandBuilder = New SqlCommandBuilder(dataAdapter)
    

    You can change the data in your sql server by doing the sql commands like in the link https://learn.microsoft.com/de-de/dotnet/api/system.data.sqlclient.sqldataadapter.insertcommand?view=netframework-4.8

    Where you build you own update, insert and /or delete command to manipulate the data in the sql server. Or you manipulate the data in your filled table, and let dot net do the work. By doing following line

    Dim commandBuilder1 = New SqlCommandBuilder(dataAdapter)
    'this next works with commandbuilder or your own sql ,commands
    dataAdapter.Update(table)
    

    For the future try to keep your question focussed on one problem. When you want to do something enter in a search engine you plain text question with vb net at the start and see where it takes you. usually here or to microsoft docs and some other pages blogs etc there you might find inspiration to try it yourself. The shown examples are often very easy, so that it can be explained properly. complex you have to figure out yourself or you are lucky and someone else has tried it and succeeded. I hoped this helps a little