When I insert a field into crystal report and that's the code the field does not appear in the report - vb.net

This is the code on the print command
and the dataset connected with database successfully
Try
Dim rpt As New rptallergy2
Dim myConnection As SqlConnection
Dim MyCommand1 As New SqlCommand()
Dim myDA1 As New SqlDataAdapter()
Dim myDS As New DataSet
myConnection = New SqlConnection(cs)
MyCommand1.Connection = myConnection
MyCommand1.CommandText = "SELECT RTRIM(patient.name) from patient "
MyCommand1.CommandType = CommandType.Text
myDA1.SelectCommand = MyCommand1
myDA1.Fill(myDS, "patient")
rpt.SetDataSource(myDS)
frmReport.CrystalReportViewer1.ReportSource = rpt
frmReport.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

MyCommand1.CommandText = "SELECT RTRIM(patient.name) as pName from patient"
Then match the pName field to the field on your crystal reports is what Jonathan Willcock was saying.

Related

Why is my VB program throwing an exception complaining that a OleDbDataReader is closed, when it should absolutely be open?

I am trying to use OleDB and SQLBulkCopy to pull data off an excel spreadsheet, into an SQL database. When I try and run my code, I get the following exception, triggered by
"bulkCopy.WriteToServer(objDR)" (which is also described in the question title): exception data
OleDB successfully reads the excel sheet which is then populated properly in the DataGridView I created within a WinForm for debugging purposes.
Below is a snippet of the relevant code.
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\damon\Everyone\sheet1erictest.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
ExcelConnection.Open()
Dim sheet As String = "Sheet5$"
Dim expr As String = "SELECT * FROM [" + sheet + "]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "SERVER=SqlDEV;DATABASE=Freight;Integrated Security=True"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "dbo.test"
Try
objDR = objCmdSelect.ExecuteReader
Dim dt = New DataTable()
dt.Load(objDR)
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = dt
DataGridView1.Refresh()
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
Your reader object is being used by the DataTable, so comment out those lines and run them separately later with a new instance:
objDR = objCmdSelect.ExecuteReader
'Dim dt = New DataTable()
'dt.Load(objDR)
'DataGridView1.AutoGenerateColumns = True
'DataGridView1.DataSource = dt
'DataGridView1.Refresh()
bulkCopy.WriteToServer(objDR)

Data between years using select query in MS Access

I want data between years using select query in MS Access in VB.NET.
My code is:
Try
Cursor = Cursors.WaitCursor
Dim rpt As New rptLedger()
Dim myConnection As OleDbConnection
Dim MyCommand As New OleDbCommand()
Dim myDA As New OleDbDataAdapter()
Dim myDS As New DataSet 'The DataSet you created.
myConnection = New OleDbConnection(cnString)
MyCommand.Connection = myConnection
MyCommand.CommandText = "SELECT Ledger.rdate, Ledger.pDate, Ledger.receipttotal, Ledger.PaymentTotal, Ledger.PartyName, Ledger.CompCode, Ledger.Cmpcode, Ledger.rnarration, Ledger.pnarration FROM Ledger where pDate >= dateadd(year,-1,GETD())"
MsgBox(MyCommand.CommandText)
MyCommand.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand
myDA.Fill(myDS, "Ledger")
CrystalReportViewer1.ReportSource = rpt
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
You need Access functions and syntax:
MyCommand.CommandText = "SELECT Ledger.rdate, Ledger.pDate, Ledger.receipttotal, Ledger.PaymentTotal, Ledger.PartyName, Ledger.CompCode, Ledger.Cmpcode, Ledger.rnarration, Ledger.pnarration FROM Ledger where pDate >= DateAdd('yyyy', -1, Date())"

Trouble loading data into textbox

Upon the click of a button, I want the program to load in details about the user (stored in an access database) into the textboxes.
This is what I have to far
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Documents\RailwayDatabase2.accdb"
Dim MyConn As New OleDbConnection(connString)
Dim da As OleDbDataAdapter
Dim ds As DataSet
Try
MyConn.Open()
da = New OleDbDataAdapter("Select * from tbl_employees WHERE FirstName = """ & EmployeeLogin.usersname & """", MyConn)
ds = New DataSet
da.Fill(ds)
TextBox1.Text = ds.Tables("tbl_employees").Rows(0).Item(2)
MyConn.Close()
Catch ex As Exception
If MyConn.State <> ConnectionState.Closed Then MyConn.Close()
End Try
End Sub
any idea why this isn't working? thanks

how to add list items in listbox in vb.net

i am trying to add list items in listbox by the below code
if i am wrong please correct me ..
the below code is giving weird output.
Try
Dim adapter As New OleDbDataAdapter
con = New OleDbConnection(connectionString)
con.Open()
Dim dt As New DataTable
Dim ds As New DataSet
sqlstr = "select * from partyinfo "
ds.Tables.Add(dt)
adapter.SelectCommand = New OleDbCommand(sqlstr, con)
adapter.Fill(dt)
lbpartylink.DataSource = ds.Tables(0)
lbpartyunlink.ValueMember = "partyid"
lbpartyunlink.DisplayMember = "partyname"
con.Close()
Catch ex As Exception
MsgBox("error found")
End Try
my fault i wrote
lbpartylink.DataSource = ds.Tables(0)
in place of
lbpartyulink.DataSource = ds.Tables(0)

between two date to show report crystal vb

Dim rpt As New CheckoutReportpreview
Dim cn As SqlConnection
Dim cmd As New SqlCommand
Dim myDA As New SqlDataAdapter
Dim ds As New CheckoutSet
Dim crConnectionInfo As New ConnectionInfo
Try
With crConnectionInfo
.ServerName = "."
.DatabaseName = "Hotel Management System"
.UserID = "sa"
.Password = "s123"
End With
cmd.Connection = cn
cn.Open()
Dim da As New SqlDataAdapter("select * from tblCheckout where CheckoutDate>='" & CDate(Date_from.Value) & "' and CheckoutDate<='" & CDate(Date_to.Value) & "' ", cn)
cmd.Prepare()
cmd.CommandType = CommandType.Text
myDA.SelectCommand = cmd
myDA.Fill(ds, "tblCheckout")
rpt.SetDataSource(ds)
CheckoutReport.Show()
CheckoutReport.CrystalReportViewer1.ReportSource = rpt
cn.Close()
Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I HAVE A CODE HERE AND I WANT TO DISPLAY THE REPORT BETWEEN TO DATES TO OTHER FORM TO CLICK PREVIEW BUTTON, SO CAN YOU HELP ME WHEN I RUN THE MESSAGE APPEAR IS
object reference not set to an instance of an object
To answer your question on why you're getting a "object reference not set to an instance of an object" look at your SqlConnection. You declare it but never instantiate it so it's null/nothing when you reference it giving you that error. Your probably getting it on the cn.Open() line.
Change:
Dim cn As SqlConnection
to something like:
Dim cn As New SqlConnection("server=yourserver;database=yourdatabase;uid=sa=pwd=sa123")