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

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))

Related

Trying to use grid view with my data, but it outputs an error

Following is the code am using to bind the grid, can anyone please suggest me what am doing wrong in this snippet
Dim con As New SqlClient.SqlConnection
Dim ds As New DataSet
Dim adapter As SqlDataAdapter
Dim sql As String
con.ConnectionString = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234"
con.Open()
sql = "Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=****;Password=*****"
cmd.CommandText = "select * From demo_vb Where ID = '" & txtbox4.Text & "'"
adapter = New SqlClient.SqlDataAdapter
adapter.Fill(ds)
GridView1.ItemsSource = New DataView()
GridView1.DisplayMemberPath = "ID"
con.Close()
I get this error:
The SelectCommand property has not been initialized before calling 'Fill'
try following code:
SqlCommand cmd;
SqlConnection con = new SqlConnection("Data Source=SUNTECH-PC\\SQLEXPRESS;Initial Catalog=iSense;Integrated Security=True;");
con.Open();
cmd = new SqlCommand("Select Picture from Images",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
I think this would help
Dim con As New SqlClient.SqlConnection("Data Source=SHAHRUKH-PC\SQLEXPRESS;Initial Catalog=vb;User ID=sa;Password=sa#1234")
Dim ds As New DataSet
Dim sql As New SqlCommand("select * From demo_vb Where ID = '" & txtbox4.Text & "'",con)
Dim adapter As New SqlDataAdapter(sql)
con.Open()
adapter.Fill(ds)
GridView1.datasource = ds.Tables(0)
GridView1.databind()
con.Close()

Datagridview WHERE SQL Error

I'm trying to automatically fill a datagridview upon loading. This is what I have so far
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Downloads\RailwayDatabase2.accdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
ds = New DataSet
tables = ds.Tables
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
Dim view As New DataView(tables(0))
source1.DataSource = view
DataGridView2.DataSource = view
When I attempt this, I am met with an error reading
Cannot find table 0.
You must fill the dataset first.
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
da.Fill(ds)
Dim view As New DataView(tables(0))
This might help. Keep it simple.
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Downloads\RailwayDatabase2.accdb"
Dim MyConn As New OleDbConnection(connString)
Dim da As OleDbDataAdapter
Dim ds As DataSet
--For error handling, do this
Try
'Open the connection
MyConn.Open()
'Fill the dataset
da = New OleDbDataAdapter("Select * from tbl_shifts WHERE EmployeeName = '" & EmployeeLogin.usersname & "' AND Completed = True", MyConn)
ds = New DataSet
da.Fill(ds)
'Fill datagridview
DataGridView2.DataSource = ds.Tables(0)
'Close the connection
MyConn.Close()
Catch ex As Exception
'Make sure connection is closed
If MyConn.State <> ConnectionState.Closed Then MyConn.Close()
End Try
And of course, you will put second group of code in Form_Load Event.

Declare a DataSet for DataGridView CellFormatting

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

How to call a query from MS Access 2010 (.accdb) within VB.Net 2010?

I have this code that populates a datagridview with data from ms access:
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String
Sql = "SELECT * FROM myTable WHERE case_no=?"
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.accdb;Persist Security Info=True;Jet OLEDB:Database Password=dbadmin2010"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Case Info")
DataGridView1.DataSource = ds.Tables("Case Info")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I had just finished creating a query from design view within MS Access itself, is there a way to call that query and retrieve the results to my datagridview?
Just use the query name and set the command type, for example, in addition to what you already have, you can use the following notes:
Try
con.ConnectionString = enter_connection_string_here
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "NameOfQuery"
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
da.Fill(ds, "Case Info")

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()