Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 132,230 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,081 people online right now. Registration is fast and FREE... Join Now!




SQL error in VB.NET application

 
Reply to this topicStart new topic

SQL error in VB.NET application, This is a small application wherein names from database are to be disp

vb_help
post 24 Feb, 2008 - 03:44 AM
Post #1


New D.I.C Head

*
Joined: 23 Feb, 2008
Posts: 4

vb

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

Dim pname As String
pname = txtName.Text


Dim connectionString As String = GetConString()
Dim queryString As String = "select name from Patientdata where name = " & pname & ""


Using connection As New OleDbConnection(connectionString)
Dim command As OleDbCommand = connection.CreateCommand()
command.CommandText = queryString

connection.Open()

Dim dr As OleDb.OleDbDataReader
dr = command.ExecuteReader()
Do While dr.Read()

ListBox1.Items.Add(dr.Item("name"))

'TextBox1.Text = dr.Item("name")
Loop
End Using
End Sub
---------------------------------------------------------------------
Public Shared Function GetConString() As String
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "E:\VB project\DrClinic.mdb"
End Function
End Class
----------------------------------------------------------------------


In this code am getting error on line (dr = command.ExecuteReader() ) as "oledbexception was unhandled"
No value given for one or more required parameters.

Please help me.

This post has been edited by PsychoCoder: 24 Feb, 2008 - 09:06 AM
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 24 Feb, 2008 - 09:17 AM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 117 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Your query will cause this error to happen as you're passing it a string, but dont have single quotes around it so your DB thinks its an integer value. Also, you're going to want to be using parameterized SQL to help prevent SQL Injection attacks. Your current query looks like


vb

Dim queryString As String = "select name from Patientdata where name = " & pname & ""


You need to change it to


vb

Dim queryString As String = "select name from Patientdata where name = '" & pname & "'"


Notice the single quotes before and after the variable, this will let your db know its a string value. If you want your code to be secure, I would take a look at the changes I've made in your code below


vb

#Region " Button Click "
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim pname As String = ""
pname = txtName.Text


Dim connectionString As String = GetConString()
'Remove the '" & Pname & "'" to help defend against SQL Inject attacks
Dim queryString As String = "select name from Patientdata where name = @pname"


Using connection As New OleDb.OleDbConnection(connectionString)
Dim command As OleDb.OleDbCommand = connection.CreateCommand()
'Tell our command object it's executing a text statement
command.CommandType = CommandType.Text
'Tell it what is is executing
command.CommandText = queryString
'Here we are using the Parameters Collection to add our parameter
command.Parameters.AddWithValue("@pname", pname)
'Set the connection of our command object
command.Connection = connection
'Open our connection
connection.Open()

Dim dr As OleDb.OleDbDataReader
dr = command.ExecuteReader()
Do While dr.Read()

ListBox1.Items.Add(dr.Item("name"))

'TextBox1.Text = dr.Item("name")
Loop
End Using
End Sub
#End Region


Hope that helps smile.gif

EDIT: Moved to VB.Net smile.gif

This post has been edited by PsychoCoder: 24 Feb, 2008 - 09:18 AM
User is online!Profile CardPM

Go to the top of the page

vb_help
post 24 Feb, 2008 - 07:42 PM
Post #3


New D.I.C Head

*
Joined: 23 Feb, 2008
Posts: 4

Hello Sir,

Thanks a lot!!!
its working now!!
:-)
Thank you so much..
-------------------------------------------------------------------------------------------------
QUOTE(PsychoCoder @ 24 Feb, 2008 - 10:17 AM) *

Your query will cause this error to happen as you're passing it a string, but dont have single quotes around it so your DB thinks its an integer value. Also, you're going to want to be using parameterized SQL to help prevent SQL Injection attacks. Your current query looks like


vb

Dim queryString As String = "select name from Patientdata where name = " & pname & ""


You need to change it to


vb

Dim queryString As String = "select name from Patientdata where name = '" & pname & "'"


Notice the single quotes before and after the variable, this will let your db know its a string value. If you want your code to be secure, I would take a look at the changes I've made in your code below


vb

#Region " Button Click "
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim pname As String = ""
pname = txtName.Text


Dim connectionString As String = GetConString()
'Remove the '" & Pname & "'" to help defend against SQL Inject attacks
Dim queryString As String = "select name from Patientdata where name = @pname"


Using connection As New OleDb.OleDbConnection(connectionString)
Dim command As OleDb.OleDbCommand = connection.CreateCommand()
'Tell our command object it's executing a text statement
command.CommandType = CommandType.Text
'Tell it what is is executing
command.CommandText = queryString
'Here we are using the Parameters Collection to add our parameter
command.Parameters.AddWithValue("@pname", pname)
'Set the connection of our command object
command.Connection = connection
'Open our connection
connection.Open()

Dim dr As OleDb.OleDbDataReader
dr = command.ExecuteReader()
Do While dr.Read()

ListBox1.Items.Add(dr.Item("name"))

'TextBox1.Text = dr.Item("name")
Loop
End Using
End Sub
#End Region


Hope that helps smile.gif

EDIT: Moved to VB.Net smile.gif

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 09:15PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month