Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In a search form I have a dromdown list(it shows the car name and the c_id is the value)
when I select the item in the dropdown I want to view the data in the next textboxes.
Always I get the error in the last line
I don't know what the problem is?
----------------------------------------------------------------

What I have tried:

Imports System.Data.OleDb
Partial Class links
Inherits System.Web.UI.Page
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("carrent.mdb") & ";Jet OLEDB:Database Password=;"
objConn = New OleDbConnection(strConnString)
objConn.Open()

Dim intNumRows As Integer
Dim strSQL = "select * from ListOfCars where car_id=" & DropDownList1.SelectedValue & " "

objConn.Open()
objCmd = New OleDbCommand(strSQL, objConn)
Dim myreader = objCmd.ExecuteReader()

Response.Write(DropDownList1.SelectedValue)
Response.Write(myreader.GetString(1)) 'Here I get the error

End Sub
Posted
Updated 13-Oct-22 21:48pm
Comments
Member 15627495 14-Oct-22 3:45am    
if your c_id is an integer, you don't need to enclose your datas.
....where car_id= & dropdownlist1.selectedvalue

OleDbconnection make a difference between Types. strings are to put betwween " and " ... integers don't need to be enclose.
Nour Abdel-Salam 14-Oct-22 3:52am    
yes car_id is int
----------------------------------------------------------------------
Dim strSQL = "select * from ListOfCars where car_id= &DropDownList1.SelectedValue& "
objCmd = New OleDbCommand(strSQL, objConn)

Dim myreader = objCmd.ExecuteReader 'a syntax error appears
Richard MacCutchan 14-Oct-22 4:42am    
If you get errors then please provide the full details. We cannot see your screen, or read your mind.
Nour Abdel-Salam 14-Oct-22 4:57am    
No data exists for the row/column.
here is the error message.
Mr. SMART
Richard MacCutchan 14-Oct-22 5:23am    
So what do you think is meant by "No data exists for the row/column."?

1 solution

Without knowing the exact error message, or having any access to your DB we can only guess.
But ... VB array indices are zero based: so GetString(1) may be wrong, and you have not read any rows from the reader (or checked that there are actually any rows returned).

Two other things:
1) Using an Access DB in a web based project is a bad idea: while access can be used by multiple users it's generally a pain - particularly when you keep the connection open forever. I'd strongly recommend that you look at Using block around your DB code, and replace Access / OleDb with either Sql Server or MySql as they are designed for multiuser access.

2) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Comments
Nour Abdel-Salam 14-Oct-22 3:59am    
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("carrent.mdb") & ";Jet OLEDB:Database Password=;"
objConn = New OleDbConnection(strConnString)
objConn.Open()

Dim strSQL = "select * from ListOfCars where car_id=" & DropDownList1.SelectedValue & " "

objCmd = New OleDbCommand(strSQL, objConn)
Dim myreader = objCmd.ExecuteReader

Response.Write(DropDownList1.SelectedValue)
Response.Write(myreader.GetString(1)) 'here is the error message (No data exists for the row/column)


End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900