How to display data in crystal report without duplicating the data? - vb.net

i have a problem in displaying the data in crystal reports using left join because its duplicating my data even though i am using GROUP BY in sqlcommand.
here is the list of my tables:
table 1: complaint
table 2: errordesc (error description)
here is my code in crystal reports:
Dim objConn As MySqlConnection
Dim daT1, daT2 As MySqlDataAdapter
Dim activecomp As DataSet
Dim strConnection As String
Dim strSQL As String
strConnection = "server=localhost;user id=root;password=;database=ticketing_system;"
objConn = New MySqlConnection(strConnection)
objConn.Open()
strSQL = "SELECT * FROM errordesc LEFT JOIN complaint ON errordesc.tran_no=complaint.tran_no WHERE errordesc.status='On-process' group by errordesc.err_id "
daT1 = New MySqlDataAdapter(strSQL, objConn)
activecomp = New DataSet
daT1.Fill(activecomp, "comp")
daT1.Fill(activecomp, "active")
Dim rpt As New CrystalReport1
rpt.SetDataSource(activecomp)
CrystalReportViewer1.ReportSource = rpt
objConn.Close()
and my outout was this:
it duplicated my errordesc.err_id even thought i grouped it. :( please help if anyone know. thanks in advance...

i think that the simple way to remove your duplicate rows is changing your strSQL:
strSQL = "SELECT DISTINCT * FROM errordesc LEFT JOIN "+
"complaint ON errordesc.tran_no=complaint.tran_no "+
"WHERE errordesc.status='On-process' group by errordesc.err_id "

oohh. i solved my own problem by coding my to crystal reports like this:
Dim objConn As MySqlConnection
Dim daT1, daT2 As MySqlDataAdapter
Dim activecomp As DataSet
Dim strConnection As String
Dim strSQL As String
strConnection = "server=localhost;user id=root;password=;database=ticketing_system;"
objConn = New MySqlConnection(strConnection)
objConn.Open()
strSQL = "SELECT * FROM errordesc WHERE errordesc.status='On-process' group by errordesc.err_id "
daT1 = New MySqlDataAdapter(strSQL, objConn)
activecomp = New DataSet
daT1.Fill(activecomp, "errordesc")
strSQL = "SELECT * FROM complaint left join errordesc on complaint.tran_no=errordesc.tran_no group by complaint.tran_no"
daT2 = New MySqlDataAdapter(strSQL, objConn)
daT2.Fill(activecomp, "complaint")
Dim rpt As New CrystalReport1
rpt.SetDataSource(activecomp)
CrystalReportViewer1.ReportSource = rpt
objConn.Close()

Related

How to join two Access tables into one DataGridView?

I want the user to be able to see what tickets (tblTickets) and orders (tblOrders) in a datagridview (dgvDynamic).
Howver, I recieve in error when clicking "btnDisplay" that states there was an error with the formatting of the FROM statement. I'm unsure of how to actually join the two tables so that when the condition is met, the records would appear in the table.
Here is the code:
Imports System.Data.OleDb
Public Class frmViewTables
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb"
Dim MyConn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim tables As DataTableCollection
Dim source1 As New BindingSource
Dim dt As DataTable
Private Sub btnDisplayDataGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayDataGrid.Click
Dim source1 As New BindingSource
Dim ds = New DataSet
Dim tables = ds.Tables
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Application.StartupPath & "\SAC1 Database.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As New OleDbCommand("Select * from [tblOrders] inner join [tblTickets] where Username = #username", cn)
cmd.Parameters.Add("#username", OleDbType.VarChar, 255).Value = frmLogin.SuccessfulLoginUsername
da.SelectCommand = cmd
da.Fill(ds, "tblOrders")
Dim view As New DataView(tables(0))
source1.DataSource = view
dgvDynamic.DataSource = view
End Sub
End Class
Thank you.
It looks the error is in query syntax. The inner join should specify joining columns from both tables something like this.
Dim cmd As New OleDbCommand("Select * from [tblOrders] inner join [tblTickets] ON [tblOrders].OrderID=[tblTickets].OrderID where Username = #username", cn)
I assumed that parent table's tblOrders primary key is OrderID and tblTickets has same name column as foreign key. Correct the "ON [tblOrders].OrderID=[tblTickets].OrderID" in above query and feedback whether it worked or not. if not, then share both tables columns info.
-Thanks

How to create a query in Visual Studio 2013 using Visual Basic

I have the following code and through debugging the problem begins at the While loop. I am trying to retrieve information from 2 tables and insert it into the table created. The information is not being inserted into the table and I am getting blank rows.
Imports System.Data.OleDb
Public Class RouteToCruise
Private Sub RouteToCruise_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Route_Btn_Click(sender As Object, e As EventArgs) Handles Route_Btn.Click
Dim row As String
Dim connectString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=M:\ICT-Group-Project\DeepBlueProject\DeepBlueProject\DeepBlueTables.mdb"
Dim cn As OleDbConnection = New OleDbConnection(connectString)
cn.Open()
Dim CruiseQuery As String = "SELECT CruiseID, RouteID FROM Cruise WHERE CruiseID =?"
Dim RouteQuery As String = "SELECT RouteName FROM Route WHERE RouteID =?"
Dim cmd As OleDbCommand = New OleDbCommand(CruiseQuery, cn)
Dim cmd2 As OleDbCommand = New OleDbCommand(RouteQuery, cn)
cmd.Parameters.AddWithValue("?", Route_Txt.Text)
Dim reader As OleDbDataReader
reader = cmd.ExecuteReader
'RCTable.Width = Unit.Percentage(90.0)
RCTable.ColumnCount = 2
RCTable.Rows.Add()
RCTable.Columns(0).Name = "CruiseID"
RCTable.Columns(1).Name = "Route"
While reader.Read()
Dim rID As String = reader("RouteID").ToString()
cmd2.Parameters.AddWithValue("?", rID)
Dim reader2 As OleDbDataReader = cmd2.ExecuteReader()
'MsgBox(reader.GetValue(0) & "," & reader.GetValue(1))
row = reader("CruiseID") & "," & reader2("RouteName")
RCTable.Rows.Add(row)
cmd.ExecuteNonQuery()
reader2.Close()
End While
reader.Close()
cn.Close()
End Sub
End Class
If I understand your tables structure well then you could just run a single query extracting data from the two table and joining them in a new table returned by the query
Dim sql = "SELECT c.CruiseID c.CruiseID & ',' & r.RouteName as Route " & _
"FROM Cruise c INNER JOIN Route r ON c.RouteID = c.RouteID " & _
" WHERE c.CruiseID = #cruiseID"
Dim cmd As OleDbCommand = New OleDbCommand(sql, cn)
cmd.Parameters.Add("#cruiseID", OleDbType.Int).Value = Convert.ToInt32(Route_Txt.Text)
Dim RCTable = new DataTable()
Dim reader = cmd.ExecuteReader()
reader.Load(RCTable)
At this point the RCTable is filled with the data coming from the two tables and selected using the Route_Txt textbox converted to an integer. If the CruiseID field is not a numeric field then you should create the parameter as of type OleDbType.VarWChar and remove the conversion to integer
Try simplifying your query.
Dim CruiseRouteQuery As String = "SELECT C.CruiseID, C.RoutID, R.Routename FROM Cruise C LEFT JOIN Route R ON C.RouteID = R.RoutID WHERE CruiseID =?"
Also, please let us know what errors you are getting.

Table in dataset in vb.net is not saving as a foxpro table

I have a dataset in vb.net. I have read a few tables from visual foxpro and added tables in the dataset. A new table is created in the dataset. I now want to create a new dbf file from vb.net which is this new Table.
But the SQL is not accepting the source table and gives an error.
The relevant part of the code is shown.
Dim NewTable As String
NewTable = "Test.DBF"
connstr = "Provider=VFPOLEDB.1;Data Source="
connStr2 = connstr & sourceDir & "\"
Dim sourceTable As DataTable
sourceTable = myDS.Tables(3).Copy
NewTable = sourceDir & "\" & NewTable
Dim oConn As OleDbConnection
Dim oCmd As OleDbCommand
oConn = New OleDbConnection(connStr2)
oCmd = New OleDbCommand()
oCmd.Connection = oConn
oCmd.Connection.Open()
oCmd.CommandText = "SELECT * FROM " & sourceTable.ToString & " INTO TABLE " & NewTable
oCmd.ExecuteNonQuery()
oConn.Close()
The error is while executing the oCmd.ExecuteNonQuery
File out1.dbf does not exist.
(out1.dbf is the name of myDS.Tables(3).name)
So how do I proceed?
Thanks for the help, in advance.

OleDBDataAdapter Fill error Select statement using VB.net and MS access

Help, My code fails on da.fill(dt). The error says OleDBexception was unhandled
no value given for one or more required parameter
My code
Dim Conn As OleDb.OleDbConnection = New OleDb.OleDbConnection
Dim connString As String
Dim da As OleDb.OleDbDataAdapter
Dim dt As New DataTable
Dim oCmd As OleDb.OleDbCommand
Dim SQLString As String
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & sRemoteAccessFolder & "Projects.MDB"
Conn.ConnectionString = connString
Conn.Open()
SQLString = "select * from tblProjects where ProjectNumber='10100'"
da = New OleDb.OleDbDataAdapter(SQLString, Conn)
da.Fill(dt)
Any idea?
thx u
This line has problems probably:
SQLString = "select * from tblProjects where ProjectNumber='10100'"
The field ProjectNumber has to match what is in the table. If there is a space, then you need to include brackets:
SQLString = "select * from tblProjects where [Project Number]='10100'"
If it's a numeric field, then drop the quotes:
SQLString = "select * from tblProjects where [Project Number]=10100"
If you still have errors, then make sure you have a table called tblProjects in the database.
As always, make sure to use Parameters instead of doing the sql statement completely by hand. That will avoid potential sql injection issues.
I haven't used it for long time. But may be do as follow.
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & sRemoteAccessFolder & "Projects.MDB"
Conn.ConnectionString = connString
Conn.Open()
Dim oCmd As new OleDb.OleDbCommand
oCmd.CommandText= "select ...."
Dim da As OleDb.OleDbDataAdapter
Dim dt As New DataTable
da = New OleDb.OleDbDataAdapter(oCmd)
da.Fill(dt)
The same thing happened in my project as well.
SQLString = "select * from tblProjects where ProjectNumber='10100'"
I followed the same and concluded this way it helped me. The correct statement which worked for me is
SQLString = "select * from tblProjects where [ProjectNumber]='10100'"

Displaying data in Gridview in VB

I want to display data from database in DataGridView...This is my code...Its not working...Can anyone help me wat to do......
Dim DBCONSRT, QRYSTR As String
Dim strSQL, skunbr As String
Dim DBCON, myConn, myCommand, rs As Object
Dim NoOfRecords As Long
skunbr = TextBox1.Text
rs = CreateObject("ADODB.Recordset")
Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=user;PASSWORD=pass;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Initial Catalog=QA1MM;"
myConn = CreateObject("ADODB.Connection")
myCommand = CreateObject("ADODB.Command")
myConn.Open(DB_CONNECT_STRING)
myCommand.ActiveConnection = myConn
myCommand.CommandText = "update QA1MM.STRSK_OH set OH_QTY = 250 where SKU_NBR = 100013 and STR_NBR = 116;"
myCommand.Execute()
strSQL = "select * from QA1MM.STRSK_OH where SKU_NBR = " & skunbr & " with ur FETCH FIRST 10 ROWS ONLY;"
rs.Open(strSQL, myConn)
DataGridView1.DataSource = rs
DataGridView1.Refresh()
myConn.Close()
Instead of assigning a ADODB.RecordSet directly to the datasource of datagridview like that, first convert/fill the recordset values to a dataset like below
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter
Dim myDS As DataSet = New DataSet
myDA.Fill(myDS, rs, "MyTable")
DataGridView1.DataSource = myDS.Tables(0)
DataGridView1.Refresh()
This is the classic VB way. Not .net
Please refer this MSDN link