Crystal Report Showing data multiple times in visual studio - vb.net

My code is as below
Public Class AutoReportForm
Dim con1 = Form1.con1
Dim cmd1, cmd2 As OleDbCommand
Dim adp1, adp2 As OleDbDataAdapter
Dim dtb1, dtb2, dtb3, dtb4, dtb5 As New DataTable
Dim myrpt As New ReportDocument
Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()
Dim txt As String = Form1.txt
Dim ds As New DataSet()
Sub viewdata()
con1.open()
Dim query As String = "Select * from AUTO_AND_RECIPE where AUTO_BATCH_UNIQUE_CODE = '" + txt + "'"
cmd2 = New OleDbCommand(query, con1)
adp2 = New OleDbDataAdapter(cmd2)
adp2.Fill(dtb2)
cmd1 = New OleDbCommand("Select * from PROCESS where AUTO_BATCH_UNIQUE_CODE_1 = '" + txt + "'", con1)
adp1 = New OleDbDataAdapter(cmd1)
adp1.Fill(dtb1)
con1.Close()
con1.Dispose()
End Sub
Private Sub AutoReportForm_Load(sender As Object, e As EventArgs) Handles Me.Load
dtb1.Clear()
dtb2.Clear()
myrpt.Load("F:\VB2022\SCADAReport\Debug\AutoRpt.rpt")
'TextBox1.Text = txt
viewdata()
myrpt.Database.Tables("AUTO_AND_RECIPE").SetDataSource(dtb2)
myrpt.Database.Tables("PROCESS").SetDataSource(dtb1)
CrystalReportViewer1.ReportSource = Nothing
CrystalReportViewer1.ReportSource = myrpt
End Sub
End Class
This is image of my PROCESS table's datagrid
[This image is showing my crystal report is showing it multiple times][2]

Most likely, you need to add a JOIN condition between the tables.
Without a JOIN condition, every record in Table1 is free to join to every record in Table2.

Related

Binding a Data Set/Table to a DataGridView

I am currently writing a simple stock control Visual Basic program linked to a database.
Here's what it looks like so far.
The form displays the data in a DataGrid View and I am trying to refresh my DataGridView automatically when data is changed (using SQL) in the database I am using.
After doing some research I have found that the best way to do this is by binding the data table (using BindingSource) to the DataGridView
However I am struggling to implement this, as every implementation I have tried results in a blank DataGridView and would thoroughly appreciate it if someone could assist me.
Here's the code:
Imports System.Data.OleDb
Public Class Form1
Public connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbStock.accdb"
Public conn As New OleDb.OleDbConnection(connectionString)
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TblStockControlTableAdapter.Fill(Me.DbStockDataSet.tblStockControl)
For i As Integer = 1 To 5
ComboBoxQty1.Items.Add(i)
ComboBoxQty2.Items.Add(i)
Next
Dim SqlQuery As String = "Select tblStockControl.[EggType] FROM tblStockControl"
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "tblStockControl")
Dim dt As DataTable = ds.Tables("tblStockControl")
'DataGridView1.DataSource = dt
For Each row As DataRow In dt.Rows
ComboBoxAdd.Items.Add(row.Item(0))
Next
For Each row As DataRow In dt.Rows
ComboBoxTake.Items.Add(row.Item(0))
Next
End Sub
Private Sub btnAddEgg_Click(sender As Object, e As EventArgs) Handles btnAddEgg.Click
conn.Open()
Dim SqlQuery As String = "Select tblStockControl.[Quantity] FROM tblStockControl WHERE EggType = '" & ComboBoxAdd.Text & "'"
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "tblStockControl")
Dim dt As DataTable = ds.Tables("tblStockControl")
Dim qty As Integer = 0
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
qty = row(column)
Next
Next
Dim NewQty As Integer = qty + CInt(ComboBoxQty2.Text)
UpdateAddQty(NewQty)
conn.Close()
End Sub
Function UpdateAddQty(ByRef NewQty As Integer) As Integer
Dim SqlUpdate As String = "UPDATE tblStockControl SET Quantity = '" & NewQty & "' WHERE EggType = '" & ComboBoxAdd.Text & "'"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlUpdate
.Connection = conn
.ExecuteNonQuery()
End With
Return (Nothing)
End Function
Private Sub btnViewStock_Click(sender As Object, e As EventArgs) Handles btnViewStock.Click
'Add code to open Access file.
End Sub
Private Sub btnTakeEgg_Click(sender As Object, e As EventArgs) Handles btnTakeEgg.Click
conn.Open()
Dim SqlQuery As String = "Select tblStockControl.[Quantity] FROM tblStockControl WHERE EggType = '" & ComboBoxTake.Text & "'"
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "tblStockControl")
Dim dt As DataTable = ds.Tables("tblStockControl")
Dim qty As Integer = 0
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
qty = row(column)
Next
Next
Dim NewQty As Integer = CInt(ComboBoxQty1.Text) - qty
UpdateTakeQty(NewQty)
conn.Close()
End Sub
Function UpdateTakeQty(ByRef NewQty As Integer) As Integer
Dim SqlUpdate As String = "UPDATE tblStockControl SET Quantity = '" & NewQty & "' WHERE EggType = '" & ComboBoxTake.Text & "'"
Dim SqlCommand As New OleDbCommand
With SqlCommand
.CommandText = SqlUpdate
.Connection = conn
.ExecuteNonQuery()
End With
Return (Nothing)
End Function
End Class
If you are using a DataTable , then reset the dataasource of the DataGridView.I mean you need to read data from the database again. :
'Do this every time you add/Update a data
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "tblStockControl")
Dim dt As DataTable = ds.Tables("tblStockControl")
Or you can just create a row for every data you add to your database :
'Do this every time you add a data
DataGridViewRow row = (DataGridViewRow)DataGridView1.Rows[0].Clone()
row.Cells[0].Value = "Alex"
row.Cells[1].Value = "Jordan"
DataGridView1.Rows.Add(row)

Dataadapter update with VB

I hope some vb expert could tell me what I am doing wrong.
I am trying to open, modify and update a record in Northwind MDF.
The DA.Update(DS) statement always generate an error.
I find several examples on the net but only in C and thatI dond’t understand, only work in VB. I find myself so stuppid not to understand the update mechanism.
My source:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Call GETNWDS()
ContactNamebox.Text = ContactNamebox.Text + " " + Now
Call UpdateNWDS()
End If
End Sub
Public Sub GETNWDS()
'' open custumor DataAdapter daaset DS
Dim connectionString = ConfigurationManager.ConnectionStrings("NW testConnectionString").ConnectionString
Dim queryString As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers where CustomerID='ALFKI'"
Dim CN As New SqlConnection(connectionString)
Dim DA As SqlDataAdapter = New SqlDataAdapter(queryString, CN)
Dim DS As DataSet = New DataSet
DA.Fill(DS)
Dim DT As DataTable = DS.Tables(0)
Dim row As DataRow = DT(0)
CustomerIDbox.Text = row("CustomerID").ToString
CompanyNamebox.Text = row("CompanyName").ToString
ContactNamebox.Text = row("ContactName").ToString
CN.Close()
End Sub
Public Sub UpdateNWDS()
Dim connectionString = ConfigurationManager.ConnectionStrings("NW testConnectionString").ConnectionString
Dim queryString As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers where CustomerID='ALFKI'"
Dim CN As New SqlConnection(connectionString)
Dim DA As SqlDataAdapter = New SqlDataAdapter(queryString, CN)
Dim DS As DataSet = New DataSet
DA.Fill(DS)
Dim DT As DataTable = DS.Tables(0)
Dim row As DataRow = DT(0)
row("CustomerID") = CustomerIDbox.Text
row("CompanyName") = CompanyNamebox.Text
row("ContactName") = ContactNamebox.Text
Textbox.Text = row("ContactName").ToString
' DA.Update(DS) ' skip to avoud error
CN.Close()
End Sub
The webform is being filled with 3 DB items.
Then 1 box is changed and the update toutineis called but there is an error on the update statement.
Skippimg this statement shows this webform.
Note that the new row value is displalyed in a textbox.

How to select an excel data Table using a drop downlist value

I am working on an application that uses excel files as its data source. I would love the DataGridView to populate with the columns of a sheet when a worksheet name is selected from the drop down list.
Here is what I have tried doing:
Imports System.Data.OleDb
Public Class Form101
Public cn As New OleDbConnection
Public cm As New OleDbCommand
Public da As OleDbDataAdapter
Dim comb As String
Public dt As New DataTable
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Comb1.SelectedIndexChanged
comb = Comb1.SelectedText
End Sub
Public Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "Date"
.Columns(1).HeaderText = "Qty brought"
.Columns(2).HeaderText = "Qty sold"
.Columns(3).HeaderText = "Goods balance"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\toojah app\Stock card.xls; Extended Properties= Excel 8.0;"
cn.Open()
FillDataGridView("select * FROM ['" & comb & "'] ")
End Sub
End Class
Try something like this. The first function will collect all of the columns and records in your excel file and place it into a datatable. Then If you wanted to add additional columns to the datable you can do it in the Public Sub CreateDataGridView. This has been tested and works.
Friend Shared Function BuildDatatable () as datatable
Dim dt As New DataTable
Dim Conn As System.Data.OleDb.OleDbConnection
Dim cmd As System.Data.OleDb.OleDbDataAdapter
dim MyFile as string = "C:\toojah app\Stock card.xls"
Conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + MyFile + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';")
Conn.Open()
Dim dtSheets As DataTable = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim listSheet As New List(Of String)
Dim drSheet As DataRow
For Each drSheet In dtSheets.Rows
listSheet.Add(drSheet("TABLE_NAME").ToString())
cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [" & drSheet("TABLE_NAME").ToString() & "]", Conn)
cmd.TableMappings.Add("Table", "Net-informations.com")
cmd.Fill(dt)
Conn.Close()
Next
Return dt
Catch ex As Exception
Return Nothing
End Try
End Function
'This is used to pass the function datatable as a dt to then pass to the public sub as shown below.
Dim dt As DataTable = BuildDatatable
Public Sub CreateDataGridView(dt)
Dim newColumn As New Data.DataColumn("ComeColumnName", GetType(System.String))
newColumn.DefaultValue = "YourValues"
dt.Columns.Add(newColumn)
DataGridView1.DataSource = dt
End Sub

Cascading ComboBoxes

I am trying to create a cascading ComboBox that is populated from a SQL data set.
My problem is I am not getting any errors when compiling the code. but the two ComboBoxes are not populating as intended with data. Can you help me find where I am making the mistake?
Private Sub FillSalesPerson()
Dim strConnString As String
strConnString = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim DS As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Connection = DS
cmd.CommandType = CommandType.Text
cmd.CommandText = "select distinct Name,Salesperson_Code from dbo.View_Customers_With_Sales_People"
Dim objeDS As DataSet = New DataSet()
Dim dAdapter As New SqlDataAdapter
dAdapter.SelectCommand = cmd
DS.Open()
dAdapter.Fill(objeDS)
DS.Close()
ComboBox1.ValueMember = "Salesperson_Code"
ComboBox1.DisplayMember = "Name"
ComboBox1.DataSource = objeDS.Tables(0)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedValue.ToString() <> " " Then
Dim Salesperson_Code As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
FillStates(Salesperson_Code)
'ComboBox2.SelectedIndex = 0'
End If
End Sub
Private Sub FillStates(Salesperson_Code As Integer)
Dim strConnString As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim DS As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Connection = DS
cmd.CommandType = CommandType.Text
cmd.CommandText = "Select [No_],[Company Name] from [dbo].[View_Customers_With_Sales_People] where (Salesperson_Code= #Salesperson_Code)"
cmd.Parameters.AddWithValue("#Salesperson_Code", Salesperson_Code)
Dim objDs As New DataSet()
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = cmd
DS.Open()
adapter.Fill(objDs)
DS.Close()
If objDs.Tables(0).Rows.Count > 0 Then
ComboBox2.ValueMember = "[No_]"
ComboBox2.DisplayMember = "[Company Name]"
ComboBox2.DataSource = objDs.Tables(0)
End If
End Sub
Try changing ComboBox1.DataSource = objeDS.Tables(0) for ComboBox1.RowSource = objeDS.Tables(0)

selecting in vb.net with ms access database

I want to select some data in my table and I use this code :
Public Class frmLogin
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim t As New DataTable
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As OleDbCommand
Dim reader As OleDbDataReader = Nothing
Dim tuser As String = txtUsername.Text
Dim sql As String = "SELECT * FROM dosen WHERE nip=tuser"
Try
cmd = New OleDbCommand(sql, conn)
reader = cmd.ExecuteReader()
While reader.Read
MessageBox.Show(reader.GetString(0).ToString & _
vbTab & vbTab & reader.GetString(1).ToString)
End While
Finally
If reader IsNot Nothing Then reader.Close()
End Try
End Sub
But there is an error in reader = cmd.ExecuteReader() line. Anyone can help me?
Correct the mistakes. The code should be
Dim reader As OleDbDataReader ' no need for nothing
Dim adapter As New OleDbDataAdapter()
Dim sql As String = "SELECT * FROM dosen WHERE nip='" & tuser & "'"
tuser is a variable, not the content