Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
In the following code all the subject codes are read by SQLDataReader rd. How do I make the DataReader read the subject code of only the selected subject regardless of whether the subject name contain white space or not such as Biology, English Language, Principles of Cost Accounting, etc?
VB
Dim sql = "Select subjectCode From ProgramDetails.subjects where subjectname like @subname"
       Using con = New SqlConnection("Data Source=EBENEZERAKAGLO\SQLEXPRESS;Initial Catalog=Naass;Integrated Security=True")
           Using cmd = New SqlCommand(sql, con)
               cmd.Parameters.AddWithValue("@subname", String.Format("%{0}%", ""))
               con.Open()
               Using rd = cmd.ExecuteReader()
                   While rd.Read()
                       Dim subjectCode = rd.GetInt32(0)
                       subcode = subjectCode
                       ' ... '
                       MsgBox(subjectCode)
                   End While
               End Using
           End Using
       End Using
Posted
Updated 29-Jan-13 5:00am
v5

RTRIM[^] And/Or LTRIM[^] in your SQL statement.

Alternatively you could use String.Trim[^]
 
Share this answer
 
1. Do not remove spaces from the subname variable during the Form Load event.
subname = Module1.sname

2. String.Format("%{0}%", "") creates a string parameter that contains %%. Used with the LIKE operator, it will match everything in the database. Please change that line of code to this:
cmd.Parameters.AddWithValue("@subname", subname)

3. Change the SQL statement to this.
Dim Sql = "Select subjectCode From ProgramDetails.subjects where subjectname=@subname;"
 
Share this answer
 
v3
Comments
Akaglo 28-Jan-13 15:08pm    
Thank you. The variable name that contains the search value is " & subname & " and when I replace "" with this variable name, subjectcodes of subject names that contain white spaces are not read by the SQLDataReader. Pls help me out because I'm so much worried about this problem.
Mike Meinz 28-Jan-13 15:09pm    
Please show us the actual source code that is not working the way you expect. Also, provide examples of what actual values are in the variable when you do the test.

I tested this query on one of my databases and it found "Apple Valley" just fine.
select * from city where city like '%apple valley%'
Akaglo 28-Jan-13 15:20pm    
Dim sql = "Select subjectCode From ProgramDetails.subjects where subjectname like @subname"
Using con = New SqlConnection("Data Source=EBENEZERAKAGLO\SQLEXPRESS;Initial Catalog=Naass;Integrated Security=True")
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@subname", String.Format("%{0}%", "" & subname & ""))
con.Open()
Using rd = cmd.ExecuteReader()
While rd.Read()
Dim subjectCode = rd.GetInt32(0)
subcode = subjectCode
' ... '
MsgBox(subjectCode)
End While
End Using
End Using
End Using
Mike Meinz 28-Jan-13 15:22pm    
The line should look like this:
cmd.Parameters.AddWithValue("@subname", String.Format("%{0}%", subname))
You are putting quotes in the parameter value so it is trying to find rows with quotes in the subjectname.
Quotes are only used in a SQL statement when a value is included in the SQL statement. Not when a parameterized valuename is included in the SQL statement.
Akaglo 28-Jan-13 15:32pm    
I've copied your code and used it as such, but it's only the subjectcodes of subject names without white space that are being read. Is it because I have the following at Form_load: subj = Module1.sname
subj1 = subj.Replace(" ", "")
subname = subj1
And without the above code I wouldn't be able to log on with subject names with white space. Pls what shall I do again?

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