Declare a DataSet for DataGridView CellFormatting - vb.net

Im trying to change a row color in datagridview but the code that used to work for me needs a dataset. This is the code that i use to fill my datagridview
Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, strcon)
Dim dt As New DataTable("[product info]")
adapter.Fill(dt)
DataGridView1.DataSource = dt
Dim sql1 As String
sql1 = "SELECT * FROM [product info]"
Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
Dim cmd As New OleDbCommand(sql1, strcon)
strcon.Open()
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read()
strcon.Close()
How can i declare a dataset within this line of codes?

Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, strcon)
Dim ds As New DataSet("[product info]")
adapter.Fill(ds)
DataGridView1.DataSource = ds
Dim sql1 As String
sql1 = "SELECT * FROM [product info]"
Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
Dim cmd As New OleDbCommand(sql1, strcon)
strcon.Open()
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read()
strcon.Close()

Dim ds as New Dataset("product_info")
ds.Tables.Add(dt)
Add these row after you filled data to datatable dt

Related

Crystal Report not showing data even though dataset contains data from MSAccess - what am I missing?

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim ssql As String
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= 'C:\Users\xxxx\xxxxx\xxxxxxxxe/xxxxxx.accdb'"
con.Open()
ssql = "SELECT * FROM tblTempProformaInvoice"
da = New OleDb.OleDbDataAdapter(ssql, con)
da.Fill(ds, "GetProformaInvoice")
con.Close()
MsgBox(ds.Tables("GetProformaInvoice").Rows(0).Item("spinvnr").ToString)
Dim rpt As New PFI
rpt.SetDataSource(ds)
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.Refresh()
try
rpt.SetDataSource(ds.Tables(0))

How to populate DataGridView using Select with Where Clause using parameters.addwithvalue?

Sorry for my noob question, But how can I add values in #id in a select command?
I'm using vb.net and MySQL
here is calling all the 'time' column, but I want to add a where in a query.
("Select time from dtr where id = #id") where will I put the parameters.addwithvalues command?
con = New MySqlConnection
con.ConnectionString = "server = localhost; userid = root; password=; database=mjb_payroll; SslMode=none"
Dim query As String = "Select time from dtr"
Dim cmd As New MySqlCommand(query, con)
Dim adpt As New MySqlDataAdapter(cmd)
Dim tbl As New DataTable()
adpt.Fill(tbl)
DataGridView1.DataSource = tbl
If your concern is just on adding parameters.addwithvalue you can add it after declaring your MySqlCommand:
Dim query As String = "Select time from dtr where id = #id"
Dim cmd As New MySqlCommand(query, con)
cmd.Parameters.AddWithValue("#id", id)
Dim adpt As New MySqlDataAdapter(cmd)
Dim tbl As New DataTable()

How to configure multiple dataadapters?

The following code works ok.
Public Class DaisyServicesForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim sql As String = "SELECT t.[ID],t.[Site],t.[CLI],t.[CustomerName],t.[FromDate],t.[ToDate],t.[Quantity],t.[UnitCost],t.[TotalCost],t.[Description],t.[filenameonly],t.billingmonth as [CurrentBillingMonth], [bill] FROM [DaisyServices].[dbo].[DaisyServicesIndigo] t"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
But for some reason when I add additional dataadapters, as per the below, I get the following error
"Object reference not set to an instance of an object."
Public Class DaisyServicesForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=inlt01\SQLEXPRESS; database=DaisyServices; integrated security=yes"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim sql As String = "SELECT t.[ID],t.[Site],t.[CLI],t.[CustomerName],t.[FromDate],t.[ToDate],t.[Quantity],t.[UnitCost],t.[TotalCost],t.[Description],t.[filenameonly],t.billingmonth as [CurrentBillingMonth], [bill] FROM [DaisyServices].[dbo].[DaisyServicesIndigo] t"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim sql2 As String = "SELECT i.[ID],i.[Site],i.[CLI],i.[CustomerName],i.[FromDate],i.[ToDate],i.[Quantity],i.[UnitCost],i.[TotalCost],i.[Description],i.[filenameonly],i.billingmonth as [CurrentBillingMonth], i.[bill] From [DaisyServices].[dbo].[DaisyServicesIndigo] i LEFT JOIN [DaisyServices].[dbo].[" + TextBox1.Text + "] s on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE s.[CLI] is NULL"
Dim comm2 As SqlCommand = New SqlCommand(sql2, conn)
Dim dataadapter2 As SqlDataAdapter = New SqlDataAdapter(comm2)
Dim sql3 As String = "SELECT s.[ID],s.[Site],s.[CLI],s.[CustomerName],s.[FromDate],s.[ToDate],s.[Quantity],s.[UnitCost],s.[TotalCost],s.[Description],s.[filenameonly],s.billingmonth as [CurrentBillingMonth], s.[bill] From [DaisyServices].[dbo].[" + TextBox1.Text + "] s LEFT JOIN [DaisyServices].[dbo].[DaisyServicesIndigo] i on i.[SITE]=s.[SITE] AND i.[CLI]=s.[CLI] AND i.[Quantity]=s.[Quantity] AND i.[UnitCost]=s.[UnitCost] AND i.[TotalCost]=s.[TotalCost] AND i.[Description]=s.[Description] WHERE i.[CLI] is NULL"
Dim comm3 As SqlCommand = New SqlCommand(sql3, conn)
Dim dataadapter3 As SqlDataAdapter = New SqlDataAdapter(comm3)
How should I configure multiple adapters on the same form?
Thanks

How to equal both fields

I am new to VB Net.,. I have two tables.,.,., namely .,., studentsrecords and faculty they both have course field.,.,. I just want to check if that course = course of other table.,.,
Dim con As New OleDbConnection
Dim dr As OleDbDataReader
Dim arrImage() As Byte
Dim myMS As New IO.MemoryStream
Dim cmd As New OleDbCommand
Dim strcourse As String
Dim strcourse1
Dim da As New OleDb.OleDbDataAdapter
con = New OleDbConnection(Get_Constring)
con.Open()
strcourse = "select Course from facultynew where FacultyID"
strcourse1 = "select Course from studentsrecords where IDNumber"
da = New OleDb.OleDbDataAdapter(strcourse, con)
da = New OleDb.OleDbDataAdapter(strcourse1, con)
strcourse=strcourse1
You are probably looking for an INNER JOIN.

VB.NET error adding a record to database

I'm using the code from http://homeandlearn.co.uk/NET/nets12p9.html for adding a record to the database.
It says when using a commandbuilder I should not get the error message:
Update requires a valid InsertCommand when passed DataRow collection with new rows.
However when I do the update I still get the error message. How can I fix this?
This is my code:
Dim dbProv As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim Command As OleDb.OleDbCommand
Dim dr As DataRow
Dim cb As New OleDb.OleDbCommandBuilder(da)
sql = "SELECT * FROM Cliënten"
dbProv = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = [mydatabase]"
con.ConnectionString = dbProv & dbSource
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Cliënten")
dr = ds.Tables("Cliënten").NewRow()
dr.Item("Field1") = TextBox1.Text
dr.Item("Field2") = TextBox2.Text
ds.Tables("Cliënten").Rows.Add(dr)
da.Update(ds, "Cliënten")
MsgBox("New Record added to the Database")
con.Close()
To make da.Update() works, you must assign a valid InsertCommand, then the DataAdapter will execute it automatically. Here an example:
da.InsertCommand = New OleDb.OleDbCommand("INTERT INTO Cliënten (Field1, Field2) VALUES (#field1, #field2)")
da.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("#field1", OleDb.OleDbType.VarChar, 0, "Field1"))
da.InsertCommand.Parameters.Add(New OleDb.OleDbParameter("#field2", OleDb.OleDbType.VarChar, 0, "Field2"))
da.Update(ds, "Cliënten")
WARNING: I've presumed you are using OleDb.OleDbType.VarChar for Field1 and Field2; if not you have to replace it with correct DB data format.
From what I read here it seems as though CommandBuilder should auto-generate the INSERT command for you based on the SELECT.
I think you are creating your CommandBuilder object too early - ie Before you specify the SELECT command / initialise Connection etc.
Perhaps this may work better...
Dim dbProv As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim Command As OleDb.OleDbCommand
Dim dr As DataRow
sql = "SELECT * FROM Cliënten" 'Consider specifying columns
'individually rather than using *
dbProv = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = [mydatabase]"
con.ConnectionString = dbProv & dbSource
con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
Dim cb As New OleDb.OleDbCommandBuilder(da) 'Init CommandBuilder here
cb.RefreshSchema() 'This may also help
da.Fill(ds, "Cliënten")
dr = ds.Tables("Cliënten").NewRow()
dr.Item("Field1") = TextBox1.Text
dr.Item("Field2") = TextBox2.Text
ds.Tables("Cliënten").Rows.Add(dr)
da.Update(ds, "Cliënten")
MsgBox("New Record added to the Database")
con.Close()