Click here to Skip to main content
15,907,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to update a column of a table where the column name will be entered in the textbox while executing. The following code is not performing any action. TextBox14 contains the column name and TextBox12 consists the value to be updated.

VB
Dim squery As String = "SELECT name,days from definedLeave"
                        Dim cmd6 As SqlCommand = New SqlCommand(squery, con)
                        Dim dr6 As SqlDataReader = cmd6.ExecuteReader()
                        Dim i As Integer = 0
                        While dr6.Read()
                            Try
                                s = dr6(i).ToString
                                TextBox14.Text = s
                                a = dr6(i + 1).ToString
                                TextBox12.Text = a
                               Dim sql = "update empLeave set " & TextBox14.Text.Trim & "=@na where epid='" + txtCode.Text + "'"

                                Using com7 = New SqlCommand(sql, con)
                                    com7.Parameters.AddWithValue("@na", TextBox12.Text)
                                    com7.ExecuteNonQuery()
                                End Using
                            Catch ex As Exception
        MsgBox("HELLO")
    End Try
                        End While
                        com7.Dispose()
                        dr6.Close()
Posted
Updated 28-Jun-14 9:27am
v2
Comments
Dave Kreskowiak 27-Jun-14 9:12am    
Rewrite your question in the body of the post instead of the subject line. Your question is being cut off because it's to long for three subject line.
Member 10711706 27-Jun-14 23:39pm    
I want to update a column of a table where the column name will be entered in the textbox while executing. The following code is not performing any action. TextBox14 contains the column name and TextBox12 consists the value to be updated.

1 solution

I take it that you're getting your "HELLO" message box when you try to run this code? You might want to remove the Try/Catch stuff and just let the code fail in the debugger so you can get the error message.

Also, you don't need the line com7.Dispose. The Using wrapper you have takes care of that for you.

Your code is a disaster and makes no real sense at all. You're reading some dataset from a another query record by record and then updating the exact same record in another query over and over again until the read operation in the outside query doesn't find any more records.
 
Share this answer
 
Comments
Member 10711706 29-Jun-14 2:16am    
I got the solution. Code is proper. Only i had to use is MARS. There is nothing like disaster.

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