Ok I can successfully connect to my Access database but I am still learning on how to output that data. Here is the run down.
I am designing a page where the user inputs the customer ID and then gets a list of incidents for that customer. Then I would like the user to be able to add a survey about the incident.
Problem I am running into is the ability to display this data. I have tried various method. Eventually I want to be able to display the data into a listbox and have them select it and take the survey but I am not near that far yet. Here is what I have on the connection and my attempt which doesn’t work to output the data:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sql As String = "SELECT [Title], [Description] FROM [Incidents] WHERE ([CustomerID] = textbox1.text)"
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\Users\Allen PC7\Documents\Visual Studio 2008\Projects\1-A SportsPro\App_Data\TechSupport.mdb"
Dim myCommand As New System.Data.OleDb.OleDbCommand(sql, conn)
conn.Open()
Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader()
Try
While myReader.Read()
Console.WriteLine(myReader.GetInt32(0).ToString() + ", " + myReader.GetString(1))
End While
Finally
myReader.Close()
conn.Close()
End Try
End Sub
I just can’t get anything displayed in the console and instead get an error saying Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader()…System.Data.OleDb.OleDbException: No value given for one or more required parameters.
Thanks
You are not passing in a proper value to the SQL:
It will try to look for
CustomerIDthat is equal to the database value “textbox1.text”. Since this is probably the wrong data type and a table/column that doesn’t exist, you are getting the error.You should be using a parameterized query to avoid SQL Injection.
This will fail if the value in
textbox1.textis not of the right type – you should both validate and convert to the right type (my code assumes an integer).