I have a Vb.net program that queries a database to get a bunch of records. I can't quite figure out how to pass parameters. below is my code:
Dim connectionString As String
Dim sqlCnn As SqlConnection
Dim sqlCmd As SqlCommand
Dim sql As String
Private Function GetCustomerData() As DataTable
locationdb = "10.0.1.1"
connectionString = ("Data Source=" & locationdb & ";Initial Catalog=TestDB;Persist Security Info=True;User ID=user;Password=password")
sql = ("SELECT lCustomerID,CustomerName,address FROM customers where @active = True...ETC")
sqlCnn = New SqlConnection(connectionString)
Dim CategoryAdapter As New SqlDataAdapter(sql, sqlCnn)
Dim CustomerInfo As New DataSet()
sqlCmd.Parameters.AddWithValue("@StartDate", frmMain.Startdate)
sqlCmd.Parameters.AddWithValue("@EndDate", frmMain.Enddate)
sqlCmd.Parameters.AddWithValue("@Department", "ALL")
sqlCmd.Parameters.AddWithValue("@Active", "1")
sqlCmd.Parameters.AddWithValue("@Visits", "ALL")
CategoryAdapter.Fill(CustomerInfo, "Customers")
Return CustomerInfo.Tables(0)
End Function
I need to pass:
@stardate
@enddate
@Deparment
@Active
@Visits
I am receiving the error:
NullReferenceException was unhandled. Object reference not set to an instance of an object.
at line:
sqlCmd.Parameters.AddWithValue("@StartDate", frmMain.Startdate)
frmMain.Startdate
and frmMain.Enddate
are defined by a Datetime picker datetimepicker1
and datetimepicker2
on frmMain
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…