Issue with Comobox on DataGridView - sql

I am using VB.NET to pull some data from an SQL database into a Datagridview.
I then want the user to be able to modify the information and save it back to the database, which I have working fine.
What I need to be able to do now is to restrict the values, but way of a combobox for the field Tarrif.
I have configured a DataSource called Tarrif1 and I am using the below code.
I have a couple of issues/questions.
Firstly the dropdown shows a single value of "System.Data.DataViewManagerListItemTypeDescriptor" not the Tarrif1 values.
Secondly, I now have 2 columns on my datatable called Tarrif, the original database one and the one I have added - How can I get the ComboBox to right back to the appropriate Tarrif database field.
Here is my code:
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT * FROM Customers"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
'comboBoxCol.DisplayMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'comboBoxCol.ValueMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
Hope that makes sense. Any help greatly appreciated.

A few things in this code:
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
First, I'm not sure what happens whenever the code attempts to fill a DataSet that already has data in it. In my code, I never assume, so I always reinitialize it each time unless I need it:
ds = new DataSet()
Next, you are setting the DataSource for comboBoxCol as Tarrifs1, but I do not see that defined anywhere. You might want to look into that.
Finally, about the SQL: Dim sql As String = "SELECT * FROM Customers" I don't know what columns are in your Customers table. Is Tarrif an actual column?

I figured half my issue out.
I configured a second data connection to pull the Tarrif drop down from the Tarrifs table and display this as part of the customer data table.
My outstanding issues now, is that I need to write the select Tarrif value back to the Customers table.
All the other updated values save, just not sure how to write the Tarrif dropdown back to my SQL table
Here is my latest code.
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT [Customer ID] ,[Customer Name] ,[Address] ,[City] ,[County] ,[Post Code] FROM [BillingReferenceData].[dbo].[Customers]"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim con As New System.Data.SqlClient.SqlConnection("server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes")
Dim strSQL As String = "SELECT * FROM Tarrifs"
Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, con)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
da.Fill(ds, "Tarrifs")
comboBoxCol.DataSource = ds.Tables("Tarrifs")
comboBoxCol.DisplayMember = "Tarrif"
comboBoxCol.ValueMember = "Tarrif"
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
dataadapter.Update(ds.Tables("Tarrifs"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class

Related

Update database with dataviewgrid on joined query

Update my SQL Server with info provided by dataviewgrid changes, mainly a checkbox / bit field.
I can populate my dataviewgrid and I can even make it update if i only use a single table, the problem is I want my query to have three tables joined, just to display information.
I've tried to replicate some tutorials and make changes of my own but it just doesn't seems to click.
It has been a while since I've coded in VB so there's also that.
What I have so far:
Imports System.Data.SqlClient
Public Class Form1
Dim myDA1 As SqlDataAdapter
Dim myDataSet1 As DataSet
Dim myDA2 As SqlDataAdapter
Dim myDataSet2 As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con2 As SqlConnection = New SqlConnection("Data Source=CONNECTION STRING WORKS FINE")
Dim cmd2 As SqlCommand = New SqlCommand("SELECT x1,y2,z1 from a inner join b where a.x1=b.x2", con2)
con2.Open()
myDA2 = New SqlDataAdapter(cmd2)
Dim builder2 As SqlCommandBuilder = New SqlCommandBuilder(myDA2)
myDataSet2 = New DataSet()
myDA2.Fill(myDataSet2, "MyTable2")
DataGridView2.DataSource = myDataSet2.Tables("MyTable2").DefaultView
con2.Close()
con2 = Nothing
End Sub
Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
Me.Validate()
Me.myDA2.Update(Me.myDataSet2.Tables("MyTable2"))
Me.myDataSet2.AcceptChanges()
End Sub
End Class
It's updating on a basic query, yes, but i need to make it update on a join query.
As I said in my comment you cannot use a CommandBuilder when you use a join. You need to supply your own Update, Insert, and Delete commands for the DataAdapter. Be sure to use the overload of the Parameters.Add method that includes the name of the source column. This provides the mapping for the DataAdapter. I used a database on my machine to show the code. You would probably want to add commands for Insert and Delete.
Private da As SqlDataAdapter
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
da = New SqlDataAdapter
Dim dt As New DataTable
Dim query = "Select Article.ArticleID, Article.Title, Author.LastName From Article Inner Join Author on Article.AuthorID = Author.AuthorID"
Using cn As New SqlConnection(My.Settings.PublishersConnection)
Using cmd As New SqlCommand(query, cn)
da.SelectCommand = cmd
da.Fill(dt)
End Using
End Using
DataGridView1.DataSource = dt
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim Uquery = "Update Article Set Title = #Title Where ArticleID = #ID;"
Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
Using cn As New SqlConnection(My.Settings.PublishersConnection)
Using UCommand As New SqlCommand(Uquery, cn)
UCommand.Parameters.Add("#Title", SqlDbType.VarChar, 50, "Title")
UCommand.Parameters.Add("#ID", SqlDbType.Int, 4, "ArticleID")
da.UpdateCommand = UCommand
da.Update(dt)
End Using
End Using
End Sub

Filtering Data within a DataGridView in VB.NET

How would I add a filtration function to my program so when I begin typing into a text box, it filters the results in the DataGridView? Just to clarify, this code loads a linked Access database into the DataGridView on form load and allows me to add, remove and update the data contained with in.
I'm relatively new to Visual Basic and I have ran out of ideas in regards to what to in order to get it to work. Thank you in advance for any help provided!
Imports System.Data.OleDb
Public Class frmDatabase
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter
Private Sub frmDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Joe\Documents\Visual Studio 2012\Projects\school database viewer\school database viewer\dbSchoolDatabase.mdb"
con.Open()
ds.Tables.Add(dt)
da = New OleDbDataAdapter("Select * from tableStudentDetails", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
da.Fill(dt)
dgvStudentDetails.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub cmdSave_Click_1(sender As Object, e As EventArgs) Handles cmdSave.Click
da.Update(dt)
End Sub

How To Populate a DropDown List From a DataSet?

I am trying to populate an ASP dropdown list in vb.net with the results from a stored procedure returned in a data set. I was wondering if anyone knew the vb.net code to populate the dropdown list?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim connString As String = "Server=MYCOMPUTER\SQLEXPRESS;Database=scales;Trusted_Connection=True"
Dim myConn As New SqlConnection(connString)
myConn.Open()
Dim da As New SqlDataAdapter("select scaleName from scales", myConn)
Dim dt As New DataTable
da.Fill(dt)
ComboBox1.DisplayMember = "scaleName"
ComboBox1.DataSource = dt
myConn.Close()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
IF Not IsPostback then
PopulateDropdown()
End IF
End Sub
Private Sub PopulateDropDown()
Dim connString As String = "Server=MYCOMPUTER\SQLEXPRESS;Database=scales;Trusted_Connection=True"
Dim myConn As New SqlConnection(connString)
myConn.Open()
Dim da As New SqlDataAdapter("select ScaleId, scaleName from scales", myConn)
Dim dt As New DataTable
da.Fill(dt)
Me.ComboBox1.DataTextField = "scaleName "
Me.ComboBox1.DataValueField = "ScaleId"
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DataSourceID = String.Empty
Me.ComboBox1.DataBind()
myConn.Close()
End Sub
In order to show the data in the DropDownList control, you can use the following Code. To use the results of a Stored Procedure, you need to create the SELECT command:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dt As New DataTable
Dim connString As String = "Server=MYCOMPUTER\SQLEXPRESS;Database=scales;Trusted_Connection=True"
Using myConn As New SqlConnection(connString)
myConn.Open()
Using cmd = myConn.CreateCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "dbo.uspMyStoredProc"
cmd.Parameters.AddWithValue("#MyInputParam", 123)
Using da As New SqlDataAdapter(cmd)
da.Fill(dt)
End Using
End Using
End Using
ComboBox1.DisplayMember = "scaleName"
ComboBox1.DataSource = dt
ComboBox1.DataBind()
End If
' ...
End Sub
I've adjusted the following things:
Usually you only need to bind the data on the initial request. Therefore, the if statement at the beginning checks the IsPostBack property.
In order to close and dispose the connection and the data datapter reliably, I've added some using statements.
In order to access the stored procedure, I've created a SqlCommand and set the CommandType to StoredProcedure. The CommandText is set to the name of the Stored Procedure. In the sample, I've also added a parameter named MyInputParam that is sent to the Stored Procedure.

vb database connection gives no information as to whether connection was successful

I am trying to connect to an access database THROUGH CODE. I capitalized that last bit because I am well aware of the visual method of doing it but can't use that method in this instance. Basically I just want to create a databinding to a combobox on a single column from a table Title that displays the column Year. To clear that up I want to populate cboYearsFillBy with the data from the column YearPublished. The thing I am finding to be the biggest pain is that vb doesn't throw any sort of fuss when the db isn't found so I don't even know if it is finding it. My code is below
Imports System.Data.OleDb
Public Class frmTitle
' Create untyped dataTables
Dim dtYears As New DataTable
' Declare a variable to represent the DataAdapter
Dim daYears As OleDbDataAdapter
' Create BindingSource objects for Employee & Dept tables:
Dim WithEvents bsYears As New BindingSource
Private Sub frmTitle_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim connStr As String = "Provider=Microsoft.ace.OLEDB.12.0;Data Source=D:\VisualStudioProjects\Biblio.accdb"
MessageBox.Show(connStr)
Dim titleSQLStr As String = "SELECT * FROM Title"
daYears = New OleDbDataAdapter(titleSQLStr, connStr)
Dim commandBuilder As New OleDb.OleDbCommandBuilder(daYears)
daYears.Fill(dtYears)
dtYears.PrimaryKey = New DataColumn() {dtYears.Columns("YearPublished")}
bsYears.DataSource = dtYears
cboYearsFillBy.DataSource = bsYears
cboYearsFillBy.DisplayMember = "YearPublished"
cboYearsFillBy.ValueMember = "YearPublished"
End Sub
End Class
I get the following output
Imports System.Data.OleDb
Public Class frmTitle
' Create untyped dataTables
Dim dtYears As New DataTable
' Declare a variable to represent the DataAdapter
Dim daYears As OleDbDataAdapter
' Create BindingSource objects for Employee & Dept tables:
Dim WithEvents bsYears As New BindingSource
Private Sub frmTitle_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim connStr As String = "Provider=Microsoft.ace.OLEDB.12.0;Data Source=D:\VisualStudioProjects\Biblio.accdb; Persist Security Info=True"
MessageBox.Show(connStr)
Dim con As New OleDbConnection
con = New OleDbConnection(connStr)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim titleSQLStr As String = "SELECT * FROM Title"
daYears = New OleDbDataAdapter(titleSQLStr, connStr)
daYears.Fill(dtYears)
cboYearsFillBy.DataSource = dtYears
cboYearsFillBy.DisplayMember = "YearPublished"
cboYearsFillBy.ValueMember = "YearPublished"
con.Close()
End Sub
End Class
I simplified your code just try now

Showing all fields in listbox from database, vb.net

I'm trying to make a list box to show all the fields of a particular table in an Access database. I have a few tables, and the idea would be to have each table loaded by a different button (and the items in the box cleared). One trick is that tables aren't all the same size. How do I get all the fields to show up for each table. What I have now is only showing one field:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;data source='C:\dummy_data.accdb';"
Dim conn As New OleDbConnection(connString)
Dim sql As String = "SELECT * FROM Customers"
Dim cmd As New OleDbCommand(sql, conn)
conn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader()
ClientList.Items.Clear()
While reader.Read()
ClientList.Items.Add(reader(0).ToString())
End While
reader.Close()
conn.Close()
End Sub
If you are not averse to using a DataGridView instead of a ListView, you could do it this way:
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source='PathToMyDatabase';"
Dim sql As String = "SELECT * FROM Tメイン;"
Dim dt As DataTable
With New OleDbDataAdapter(sql, connString)
Dim ds As New DataSet()
.Fill(ds)
dt = ds.Tables(0)
End With
Me.DataGridView1.DataSource = dt
I do not have 'ACE.OLEDB' on my computer, so had to use the JET provider instead, but it should work the same way.
I should add that you can use this method to retrieve the data from the DB, but there is no easy way as far as I understand to bind a DataTable to a ListView (see this MSDN question for example) and you would have to loop through the columns in your DataTable first and add the column headers to your ListView and then loop through the rows and add the data.
UPDATE:
To answer your questiona s to how to export data from the DataGridView I remembered that I wrote code to do that a little while back.
Private Function ExportToExcelFile(ByVal FileName As String) As Boolean
With New Excel.Application
With .Workbooks.Add()
For Each sheet As Worksheet In .Worksheets
If sheet.Index > 1 Then
Call sheet.Delete()
End If
Next
With CType(.Worksheets(1), Worksheet).Range("A1")
For Each column As DataGridViewColumn In Me.dgvData.Columns
.Offset(0, column.Index).Value = column.HeaderText
For Each row As DataGridViewRow In Me.dgvData.Rows
.Offset(row.Index + 1, column.Index).Value = row.Cells(column.Index).Value
Next
Next
End With
Call .SaveAs(FileName)
End With
Call .Quit()
End With
End Function
I hope this will help to get you started.
Using a Listview control, the following code should do the trick. For simplicity I defined only 4 customer fields - you will need to define them according to your table field defintions:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClientList.View = View.Details
ClientList.FullRowSelect = True
ClientList.Columns.Add("ID", 120)
ClientList.Columns.Add("Name", 120)
ClientList.Columns.Add("Address", 140)
ClientList.Columns.Add("Email", 100)
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\dummy_data.accdb;"
ClientList.Items.Clear()
Using conn As New Data.OleDb.OleDbConnection(connString)
conn.Open()
Dim sql As String = "SELECT * FROM Customers"
Using cmd As New Data.OleDb.OleDbCommand(sql, conn)
Dim lvi As ListViewItem
Using oRDR As Data.OleDb.OleDbDataReader = cmd.ExecuteReader
While oRDR.Read()
lvi = ClientList.Items.Add(oRDR.GetValue(0).ToString)
For i = 1 To oRDR.FieldCount - 1
lvi.SubItems.Add(oRDR.GetValue(i).ToString)
Next
End While
End Using
End Using
conn.Close()
End Using
End Sub