Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am passing the following simple command to update a table in database. The command runs without error but the database is not updated. Can someone pls point out the mistake i am making.

SQL
Dim Update As New SqlCommand("Update COMP set COMPN = @COMPN WHERE COMPC = @COMPC", connection)

      Update.Parameters.Add(New SqlParameter("@COMPC", (ddlCompanyCode.SelectedText)))
      Update.Parameters.Add(New SqlParameter("@COMPN", (txtCompanyName.Text)))

           Update.ExecuteNonQuery()
Posted
Updated 23-Jan-14 7:05am
v3
Comments
Ranjan.D 23-Jan-14 14:44pm    
You should let us know the database attribute type COMPC ?
atul sharma 5126 23-Jan-14 14:51pm    
nvarchar length 2

Probably the WHERE condition is not satisfied, but you didn't notice because your code ignores the ExecuteNonQuery return value (that is the rows affected by the command).
 
Share this answer
 
Comments
atul sharma 5126 23-Jan-14 14:43pm    
where is the selected text in the combobox which is also the key field in database. I have also added now the return value for message and it is showing not updated
atul sharma 5126 23-Jan-14 14:49pm    
End Function
I'm guessing the COMPC field is a numeric one, but you are inserting the parameter as text.

Try:

VB
Update.Parameters.Add(New SqlParameter("@COMPC", CType(ddlCompanyCode.SelectedText, Int)))
 
Share this answer
 
Comments
atul sharma 5126 23-Jan-14 14:41pm    
thanks for reply. COMPC is a nvarchar field of lenght 2 in database. the current data to this field includes 01, 02 etc. i still tried your suggestion but it is giving error of Type Int is not defined
Ron Beyer 23-Jan-14 14:53pm    
If its varchar then it wouldn't work this way, I'm not much of a VB guy, I think Integer is the actual term in VB. You could try specifying that its an nvarchar using this overload, you would just have to change the varchar to nvarchar, see the example below the documentation.
atul sharma 5126 23-Jan-14 15:05pm    
i tried but it didn't worked
Ron Beyer 23-Jan-14 15:07pm    
I would try to see exactly what command is being run on the database, copy it, and then run it in SQL Management Studio. It should be able to give you more information on why its not inserting. Is it that your page isn't updating, or is it really not inserting data?
atul sharma 5126 23-Jan-14 23:42pm    
No Its Not Working
I ran the following command in direct sql:

UPDATE COMP SET COMPN = A WHERE COMPC = 14

error: Msg 207, Level 16, State 1, Line 1
Invalid column name 'A'

Thanks for the support
ITS DONE.
I have to change the combo box property from selectedtext to text.

But why??

Isn't the selected text property of combobox which should be used as text relates to all the texts.

I am greatful and highly impressed by the community support specially by Ron :) thnks
 
Share this answer
 
Comments
Ron Beyer 24-Jan-14 8:27am    
You found a rather annoying feature. Selected text doesn't mean your selected item, it literally means if you take the mouse and highlight the text in the box, that would be the "SelectedText". You should use SelectedItem instead, its an object so you will have to cast it to a string, but it means the currently selected item. SelectedText really gets confusing since it doesn't do what you think it does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900