Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to update a sql database field with a string value. I'm getting a wierd error saying "Invalid column name 'NO'". The field that I'm trying to update is just simply the value out of a dropdownlist, and the two values are 'yes' and 'no'. Here is my update statement.

 

       Dim sqlcommand As New SqlCommand
       Dim judgement As String = ddlLegal.SelectedValue
       sqlcommand.Connection = connPayables
       sqlcommand.CommandText = "UPDATE ClientInfo SET OutJudgements = " & judgement & " WHERE LeadID = " & intLeadid
       connPayables.Open()
       sqlcommand.ExecuteNonQuery()
       connPayables.Close()

 

I know that the variable "judgement" is holding the correct value, what could be causeing my error?

Posted
what could be causeing my error?
Not using parameters!

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
What do you mean?

I use the same type of update statement on other forms, and they work fine.

That is just a coincidence. . .

 

Use parameters. . . never build sql!

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

  • *Experts*
Posted

You don't have to use SqlParameter objects, but in your example I definitely would. Your problem, however, is probably because OutJudgements is a string column and you're not putting the value in single quotes. Try this:

' ...
sqlcommand.CommandText = "UPDATE ClientInfo SET OutJudgements = '" & judgement.Replace("'", "''") & "' WHERE LeadID = " & intLeadid
' ...

 

Two things I changed:

1. I put single quotes around the value in judgement (they're embedded in the CommandText string - look next to each side of the double quotes).

2. Added Replace to the judgement variable. This replace will double up the single quotes. Without that, you may allow SQL injection to get in your code. By using parameters you won't have to worry about that.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
Without that' date=' you may allow SQL injection to get in your code. By using parameters you won't have to worry about that.[/quote']Exactly.

Also the code reads cleanly:

 

 

 

 

 

Dim cmd As New SqlCommand("UPDATE ClientInfo SET " & _ 
[indent]"OutJudgements = @judgement where LeadID = @leadId", new SqlConnection(someConnStr))
[/indent]


try 
[indent]

cmd.Parameters.Add(@judgement, ddlLegal.SelectedValue) 

cmd.Parameters.Add(@leadId, intLeadid) 

cmd.Connection.Open() cmd.ExecuteNonQuery()
[/indent]
finally 

[indent]

try [indent]cmd.Connection.Dispose()
[/indent]


finally 
[indent]cmd.Dispose()
[/indent]
end
[/indent]end

BTW, there are alot of bad practices you are employing in your example.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...