VB.NET Updating database table using dataset - sql

I am trying to update my physical database using a dataset. There is no error that appears but the Table in my Database doesn't updating at all.
I am using VS 2012, VB.NET
This is my code for updating my database table name "ProdRec"
grid()
Dim da As New OleDbDataAdapter
Dim cb As New OleDb.OleDbCommandBuilder(da)
da.Update(ds, "ProdRec")
ds.AcceptChanges()
I declare my dataset named "ds" in this Public sub
Public Sub grid()
connect()
MD = "Select * From ProdRec"
com = New OleDbCommand(MD, conn)
adaptr = New OleDbDataAdapter(com)
ds = New DataSet
adaptr.Fill(ds, "ProdRec")
With DataGridView1()
.DataSource = ds
.DataMember = "ProdRec"
End With
End Sub

Try to add this before da.Update command: da.UpdateCommand = cb.GetUpdateCommand()

Related

How do i show my data from a table with specific position to a combobox? example is this

How do i show my data from a table in database with specific position to a combobox? example is this
Example
Only with the President Position candidates will appear on combobox1 from table1
Only with the VPresident Position candidates will appear on combobox2 from table1
and so on...
im stuck with this on combobox1 which is called prescombo
code
Private Sub prescombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles prescombo.SelectedIndexChanged
Try
Dim query As String = "SELECT * FROM Candidates WHERE Position = President"
Using con As SqlConnection = New SqlConnection("Data Source=EXPOOKY;Initial Catalog=SchoolDB;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand(query, con)
Dim datapter As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim datable As DataTable = New DataTable()
datapter.Fill(datable)
prescombo.DisplayMember = "CandidateName"
prescombo.ValueMember = "CandidateID"
If datable.Rows.Count > 0 Then
If datable.Rows(3)("Position") = "President" Then
presparty.Text = datable.Rows(3).Item("Position").ToString
End If
End If
End Using
End Using
Catch ex As Exception
End Try
End Sub
This is what i tried based on my logic and its not working
If you're filtering the data using a WHERE clause in the query then there's no need to check the records in the result set. Just populate the DataTable and bind it.
Dim query = "SELECT * FROM Candidates WHERE Position = 'President'"
Dim connectionString = "Data Source=EXPOOKY;Initial Catalog=SchoolDB;Integrated Security=True"
Using adapter As New SqlDataAdapter(query, connectionString)
Dim table As New DataTable
adapter.Fill(table)
With prescombo
.DisplayMember = "CandidateName"
.ValueMember = "CandidateID"
.DataSource = table
Endf With
End Using

OLEDB - select Count not working

Find below what I've done so far, but unfortunately it's not working.
Private BS as New BindingSource
Dim ds As New DataSet
' Make the Connection
Using con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Database1.mdb")
con.Open()
Dim Sql = "SELECT COUNT ([Eventname]) FROM Eventproposal"
Dim da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Eventproposal")
' Set the Binding Source
bs.DataSource = ds.Tables("Eventproposal")
con.Close()
End Using
TextBox1.DataBindings.Add("Text", bs, "")
Couple things, you should end all SQL Commands to MS Access with ;
Dim Sql = "SELECT COUNT ([Eventname]) FROM Eventproposal;"
And you did not name your column which will give you an error when you attempt to access it by name.
Dim Sql = "SELECT COUNT ([Eventname]) AS Eventname FROM Eventproposal;"
I believe it will give it a name but not what your thinking. Lastly, when you do your binding, you will have to reference the name of the field in the table.
TextBox1.DataBindings.Add("Text", bs, "Eventname")

Populating and editing datagrid from a SQL Server table

I need to populate my datagrid from my a table in SQL Server called Pastel_Companies and then if any changes are made to the datagrid it has to update it to the database.
I am using a default view to populate my datagrid.
Is there another way where I link each column separately so I can resize my columns as they are fixed to what SQL has?
Here is my code:
Dim cn As New SqlClient.SqlConnection(SQL_Loader("", My.Settings.SQL_Win_Auth, My.Settings.SQL_Username, My.Settings.SQL_Password, My.Settings.SQL_Server_Name, My.Settings.SQL_DB_Name))
Dim Cmd As New SqlClient.SqlCommand
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New SqlClient.SqlDataAdapter
Cmd.Connection = cn
cn.Open()
da = New SqlClient.SqlDataAdapter("Select Company_ID, Prefix, DSN, File_Path From Pastel_Companies", cn)
da.Fill(dt)
'DataGridView1.Columns.Add("Company_ID", Prefix.ToString)
DataGridView1.DataSource = dt.DefaultView
cn.Close()
From: http://social.msdn.microsoft.com/Forums/windows/en-US/e444ca84-3319-4dfa-aa31-46f310dd0c13/datagridview-autosize-rowcolumn
'for the rows
DataGridView1.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader)
'and for columns
DataGridView1.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
or you can use for each loop
DataGridView1.Columns(i).Width = x

hangs on datagridview .fill large amount of data

Hello I am trying to fill a datagridview with items from a datafile. The code stops on the .fill . I tested the query in excel (Microsoft query) it works fine there. I also tested it (in my vb code) by adding a WHERE to only receive on specific entry, this works fine. Any ideas how to get this to work in my application? Code is below thanks for the help!
Dim dataAdapter As New Odbc.OdbcDataAdapter()
Private Sub ChangeSelect()
Dim selectCommand As String = "SELECT CWIPH.CUST_NO, CWIPH.JOB_NO, CWIPH.JOB_NAME, CJCMS.JOB_DESC FROM { OJ CWIPH CWIPH INNER JOIN CJCMS CJCMS ON CWIPH.JOB_NO = CJCMS.JOB_NO }"
accessDB(selectCommand)
End Sub
Private Sub accessDB(ByVal selectCommand As String)
Dim JobConnectionString As String = "Dsn=Jake; provider=System.Data.Odbc"
Dim cn As OdbcConnection = New OdbcConnection(JobConnectionString)
Dim table As New DataTable()
cn.Open()
Dim cmd As New Odbc.OdbcCommand(selectCommand, cn)
Me.dataAdapter = New Odbc.OdbcDataAdapter(cmd)
With dgvWorkOrder
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
End With
Dim commandBuilder As New Odbc.OdbcCommandBuilder(Me.dataAdapter)
Me.dataAdapter.Fill(table)
Me.dgvWorkOrder.DataSource = table
cn.Close()
End Sub
This article helped a lot. What's better: DataSet or DataReader?
So I ended up changing my code to use data reader and so far so good.

Saving record with dataset

I want to save a record in the database using a dataset, but my data is not committing into my database.
My code can be viewed below:
Dim mydataset1 As New MyDataSet
Dim row As DataRow = mydataset1.Tables("testtable").NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
mydataset1.Tables("testtable").Rows.Add(row)
Any help will be appreciated
A DataSet/DataTable is a offline/in-memory representation of your database. If you want to update the database, you need to use a DataAdapter.
For example (assuming you're using MS-Sql-Server):
Public Function UpdateDataSet(dataSet As DataSet) As Int32
Using con = New SqlConnection(My.Settings.SqlConnection)
Dim sql = "INSERT INTO TUser(Name,Address)VALUES(#Name,#Address)"
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
cmd.Parameters.Add(New SqlParameter("#Address", SqlDbType.VarChar))
Using da = New SqlDataAdapter()
da.InsertCommand = cmd
con.Open()
Dim rowCount = da.Update(dataSet)
Return rowCount
End Using
End Using
End Using
End Function
I could be rusty here since its a long time since I wrote any VB.NET or used data adapters/datasets/datatables but I think if you decide to take that route you would need code like this:
Dim connection As New SqlConnection("#####YourConnectionString#####")
connection.Open()
Dim adapter As New SqlDataAdapter("SELECT * FROM testtable", connection)
' For the line below to work, you must have a primary key field in "testtable"
Dim builder As New SqlCommandBuilder(adapter)
Dim testtable As New DataTable("testtable")
adapter.Fill(testtable)
Dim row As DataRow = testtable.NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
testtable.Rows.Add(row)
adapter.Update(testtable)
connection.Close()