0

I'm working on a Project for an online form and I can't seem to find how to get the value from the SQL database and validate it against a string e.g. Q1 in Database = A and TextBox.text = A

Below is my code so far but can't seem to work , some variables in this are global,

 Dim Question As String = "Q" & count
 Dim textVal As String = TextBox.Text
 Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection
 Dim dbSource As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ben.miles\documents\visual studio 2012\Projects\WebTraningQuestions\WebTraningQuestions\App_Data\Usertest.mdf"
 Dim ds As New DataSet
 Dim sql As String = "SELECT FROM SAP"
 Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sql, conn)
 conn.ConnectionString = dbSource
 Dim SqSearch As String = "%" & textVal & "%"
 Dim sqlQ1 As String = sql & " WHERE Questions LIKE SqSearch"
 Dim cmd1 As New SqlCommand(sqlQ1, conn)
 conn.Open()

'This is where I cant get my code right but if you see below. I would like it something like this.

 If TextBox.Text = (database) Then
    Label.Text = "Correct"
    Label.ForeColor = Drawing.Color.Green
 Else 
    Label.Text = "Wrong"
    Label.ForeColor = Drawing.Color.Red
 End If
2
  • What is this (database)?
    – user1088172
    Apr 24, 2013 at 10:29
  • the (database) is the code that I cant seam to program so I deleted it so someone can easier program something sorry Apr 24, 2013 at 10:31

1 Answer 1

0

You can use a DataReader to retrieve the value, something like (air-code and it's been a while since I did any VB):

Dim Question As String = "Q" & count
Dim textVal As String = TextBox.Text
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection
Dim dbSource As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\ben.miles\documents\visual studio 2012\Projects\WebTraningQuestions\WebTraningQuestions\App_Data\Usertest.mdf"
Dim ds As New DataSet
Dim sql As String = "SELECT [fields..] FROM SAP"
Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sql, conn)
conn.ConnectionString = dbSource
Dim SqSearch As String = "%" & textVal & "%"
Dim sqlQ1 As String = sql & " WHERE Questions LIKE SqSearch"
Dim cmd1 As New SqlCommand(sqlQ1, conn)
conn.Open()

Dim myReader AS SqlDataReader = cmd1.ExecuteReader()

If myReader.HasRows Then
   myReader.Read()
   If TextBox.Text = (myReader.GetString(col_of_field)) Then
      Label.Text = "Correct"
      Label.ForeColor = Drawing.Color.Green
   Else 
      Label.Text = "Wrong"
      Label.ForeColor = Drawing.Color.Red
   End If
 End If

 myReader.Close()

I would remove the LIKE and instead query based on the specific ID of the question you are looking for. This would mean storing the question ID on your form somewhere probably. I would also parameterise the query rather than building it as a string; you are opening yourself up to potential SQL injection attacks otherwise.

3
  • Hello Simon, Sorry about the long delay I was on my lunch break. this line SqlDataReader myReader = myCommand.ExecuteReader() is not working at all just coming up with errors saying generate method sub Thank for your help Apr 24, 2013 at 12:11
  • Oops, sorry that was c# syntax, edited the answer. As I said, it's been a long time since I wrote any VB! You might have to fix a couple of typos.
    – Simon
    Apr 24, 2013 at 14:59
  • Hi Simon, that's okay thank for the help but there seams to be one erro left mycommand is not declared. please advise? Apr 24, 2013 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.