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

Dheeg
  • 1
  • 2
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Feb 02 '15 at 19:36
  • You don't have a connection String to your server. Have a look to the different [connection strings](http://www.connectionstrings.com/sql-server/) – phil652 Feb 02 '15 at 19:48
  • still there is a error – Dheeg Feb 02 '15 at 19:52

1 Answers1

0

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")
b.pell
  • 3,873
  • 2
  • 28
  • 39