2

In my program I have a function titled runSQL, here it is:

Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=TrainingLog.accdb")
    Dim DT As New DataTable
    Dim DataAdapter As OleDb.OleDbDataAdapter

    Connection.Open()
    DataAdapter = New OleDb.OleDbDataAdapter(query, Connection)
    DT.Clear()
    DataAdapter.Fill(DT)
    Connection.Close()
    Return DT

And I'm trying to update a record in a database using the update string, sourced from this code:

    Dim sqlString As String

    sqlString = "UPDATE UserRecords set FirstName = '" & txtName.Text
    sqlString = sqlString & "', LastName = '" & txtSurname.Text

    If ChkSmoker.Checked = True Then
        sqlString = sqlString & "', Smoker = true"
    ElseIf ChkSmoker.Checked = False Then
        sqlString = sqlString & "', Smoker = false"
    End If

    sqlString = sqlString & ", Weight = " & txtWeight.Text

    If RdoMale.Checked = True Then
        sqlString = sqlString & ", Gender = 'm'"
    ElseIf RdoFemale.Checked = True Then
        sqlString = sqlString & ", Gender = 'f'"
    End If

    sqlString = sqlString & " WHERE UserName = '" & LstUsers.SelectedItem.ToString & "'"

    runSQL(sqlString)

However once I click the save button, I get an error from line 7 of the runSQL function (not including empty line, so that's the DataAdapter.Fill(DT) line) which says "No value given for one or more required parameters."

I wondered if anyone knew why this is or how to fix it.

One thing I did think of is that, in the table being updated, there are fields other than those being mentioned in my UPDATE statement. For example there is a Yes/no field titled "TeamMember", which I don't mention in the update statement. When using the update function, do I have to give values for every field, even those not being changed?

Thanks for reading, and hopefully helping!

JotaBe
  • 38,030
  • 8
  • 98
  • 117
DatBrummie
  • 63
  • 1
  • 4
  • 10
  • Honestly I don't know. I'm just learning Visual Basic in school, and I've been told the SQL parts to use, not taught them. – DatBrummie Feb 23 '15 at 07:16
  • Have you actually looked at the SQL statement that all that code generates? That would be the first thing to do, to see whether it is what you expect it to be. If you have looked then please tell us what you see. – jmcilhinney Feb 23 '15 at 07:19
  • UPDATE UserRecords set FirstName = 'Dave', LastName = 'Smith', Smoker = false, Weight = 55, Gender = 'm' WHERE UserName = 'Dave1' – DatBrummie Feb 23 '15 at 07:39
  • The runSQL function looks like something used to select rows and place in a datatable. The sql you are running is an update not a select. The command will only return how many rows were affected not a row of data that could be inserted into a datatable. – Mych Feb 23 '15 at 09:08
  • My teacher provided us with help sheet PDFs and this is what he said to use though, its the same as for when adding a user to the database, I'm in physics not computing at the moment but I'll get on to a PC and check the code again once I can – DatBrummie Feb 23 '15 at 09:54
  • I'm fairly sure that the problem lies in the SQL syntax somewhere, as I used the exact same Function, elsewhere in the program and it worked perfectly, but I don't see anything wrong with the SQL string – DatBrummie Feb 23 '15 at 13:35

1 Answers1

0

You should never composea SQL query yourself. It much easies and safer (to vaoid SQL injection) to create a parameterized query, or use an stored procedure. And then execute it by pasing the query or stored procedure name and the parameter values.

Besides, in this way, you don't have to take care of what the right format is for a particular value. For example, how do you format a date? And, how do you format a boolean value? Most probably the problem with your query is the false or true value that you're trying to set for the Smoker column, because in TSQL that's a bit value, and can only be 0 or 1.

Check this to see samples of using parameters: ADO.NET Code Examples (Click the VB tab to see it in VB). You'll see that you define a parameter specifying a name with an @ prefix in the query, and then you simply pass a value for each parameter in the query, and it will be passed to the server in the correct format without you taking care of it.

Taken from one of the samples:

Dim queryString As String = _
        "SELECT ProductID, UnitPrice, ProductName from dbo.Products " _
        & "WHERE UnitPrice > @pricePoint " _
        & "ORDER BY UnitPrice DESC;"
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@pricePoint", paramValue)

'' command.ExecuteXXX 

NOTE that you can execute the command in different ways, depending on your need to simply execute it or get an scalar value or a full dataset as a result.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • When I'm adding a user to the database though, I use the code: (nevermind, comments I don't think will allow me to paste it in and format it nicely) but when adding a user, true or false is what I used for the Yes/No column in access, and it worked for adding so I assumed it would work for editing – DatBrummie Feb 23 '15 at 10:58
  • Elsewhere in the program I used the code here: http://pastebin.com/qPwW2zTw (not sure if pastebin is allowed on the site but I don't think I can format code properly in the comments) and that works fine, using the SQL function, and using true as the value for it. (TeamMember is checkbox same as smoker in the original problem) so I'm fairly sure the problem lies somewhere else. I just can't find where – DatBrummie Feb 23 '15 at 13:50
  • You're wirgt, @DatBrummie. I'm sorry I didn't realize it was Access. Nexttiem, try to tag your question correectly. I've corrected the tags. – JotaBe Feb 23 '15 at 20:19
  • Sorry my bad. This was my first post on this site and I wasn't sure that the tags were actually supposed to be. I found the problem though, it was something really stupid that I should have seen before... turns out in the sqlstring I was trying to fill a field called "LastName" however in the database, the field was called "Surname" I feel like such an idiot. How do I mark the question closed? – DatBrummie Feb 24 '15 at 01:34