Results 1 to 6 of 6

Thread: a problem in sql update via .net

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    Srilanka
    Posts
    8

    a problem in sql update via .net

    hi. now im currently developing an application as my semester project and i strucked becuse of an small error. i have tried to update a record set in the sql server 2005 databse table by giving the following code. but its not showing any errors its working and displaying a message succeeded. but in database table i cant find any changes. pls help me........

    my advance thanks for those who viewing my thread.
    sql Code:
    1. Public Shared ReadOnly SQLConnectionString As String = "Server=RUSHAN\SQLEXPRESS;Database=nolimit;Trusted_Connection=True;"
    2.    
    3. Dim cnn As SqlClient.SqlConnection
    4. Dim cmd As SqlClient.SqlCommand
    5. Dim bc As SqlClient.SqlDataAdapter
    6. Dim dr As SqlClient.SqlDataReader
    7.  
    8. Private Sub cus_mod_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cus_mod_btn.Click
    9.         Dim sql2 As String = "UPDATE customer SET name='" & cus_mod_nme.Text & "', address='" & cus_mod_add.Text & "' ,contact='" & cus_mod_con.Text & "' WHERE (id='" & cus_mod_id.SelectedIndex & "') "
    10.         cnn = New SqlClient.SqlConnection(SQLConnectionString)
    11.  
    12.         'cnn = New SqlClient.SqlConnection(SQLConnectionString)
    13.         cnn.Open()
    14.  
    15.         cmd = New SqlClient.SqlCommand(sql2, cnn)
    16.  
    17.         cmd.ExecuteNonQuery()
    18.         MsgBox("Records affected")
    19.         cnn.Close()
    20.     End Sub
    Last edited by Hack; Jul 20th, 2010 at 07:54 AM. Reason: Added Highlight Tags

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: a problem in sql update via .net

    Wellcome to the forums!

    You should display the value of your string sql2 and make sure your statement is OK and that the values are right.

    I also noticed that you are using cus_mod_id.SelectedIndex for the ID value, are you sure that cus_mod_id.SelectedIndex really return the proper value for the ID field? I mean, if cus_mod_id is a Listbox or something like that it doesn't mean that the SelectedIndex really correspond to the ID of the record in your database.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: a problem in sql update via .net

    Also, your code is insecure for SQL injections. It's probably all right with a learning project but bad habits must be broken right from the beginning. Use parametrized queries like this, not only it is more secure, but also more readable as well:
    vb Code:
    1. Private Sub cus_mod_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cus_mod_btn.Click
    2.  
    3.     Dim sql2 As String = "UPDATE customer SET name=@p_name, address=@p_address, contact=@p_contact  WHERE id=@p_id;"
    4.  
    5.     Try
    6.         cnn = New SqlClient.SqlConnection(SQLConnectionString)
    7.         cnn.Open()
    8.  
    9.         cmd = New SqlClient.SqlCommand(sql2, cnn)
    10.  
    11.         With cmd.Parameters
    12.             .AddWithValue("@p_name", cus_mod_nme.Text)
    13.             .AddWithValue("@p_address", cus_mod_add.Text)
    14.             .AddWithValue("@p_contact", cus_mod_con.Text)
    15.             .AddWithValue("p_id", cus_mod_id.SelectedIndex) ' CHECK THIS!!!! .SelectedIndex is probably not the ID from the table.
    16.         End With
    17.  
    18.         Dim ret As Integer = cmd.ExecuteNonQuery()
    19.  
    20.         MsgBox(String.Format("Records affected: {0}", ret))
    21.  
    22.     Catch ex As Exception
    23.         MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
    24.     Finally
    25.         cnn.Close()
    26.     End Try
    27. End Sub

    As you see, the ret variable will actually show the number of affected records (if it's 0 then something is wrong).
    And most probably it's due to a combobox/listbox selected index which doesn't correspond to the actual id from the table.

    You should bound your cus_mod_id to a data source (some Datatable perhaps) containing IDs and Display names of your curtomers. Then you should assign its .DisplayMember and .ValueMember properties and use .SelectedValue instead of .SelectedIndex

    For example (provided you have ID and NAME in your customer table):

    Use a data adapter to fill a data table with results of 'SELECT ID, NAME FROM Customer'
    Dim dt As New DataTable("customer_ds")

    Code:
    cus_mod_id.DataSource = dt
    cus_mod_id.DisplayMember = "NAME"
    cus_mod_id.ValueMember = "ID"
    Then you should use .SelectedValue property of the listbox instead of .SelectedIndex

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    Srilanka
    Posts
    8

    Re: a problem in sql update via .net

    Quote Originally Posted by stlaural View Post
    Wellcome to the forums!

    You should display the value of your string sql2 and make sure your statement is OK and that the values are right.

    I also noticed that you are using cus_mod_id.SelectedIndex for the ID value, are you sure that cus_mod_id.SelectedIndex really return the proper value for the ID field? I mean, if cus_mod_id is a Listbox or something like that it doesn't mean that the SelectedIndex really correspond to the ID of the record in your database.

    its showing correct records what are in the table in combo box. when i try that with a text box by manually entered the id, its ok, i can modify. why its not updating when i did the same via the combo box. do u have any solution to use the combo box and to update?

  5. #5
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: a problem in sql update via .net

    Quote Originally Posted by rockers123 View Post
    its showing correct records what are in the table in combo box. when i try that with a text box by manually entered the id, its ok, i can modify
    So when you enter the ID manually it works? If so, Like I said, maybe the SelectedIndex value is not equal to the record ID.

    Have a look at the code that Cicatrix posted, it also displays the number of records updated, whit that you will see if the record you want to update was found or not.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Location
    Srilanka
    Posts
    8

    Re: a problem in sql update via .net

    Quote Originally Posted by cicatrix View Post
    Also, your code is insecure for SQL injections. It's probably all right with a learning project but bad habits must be broken right from the beginning. Use parametrized queries like this, not only it is more secure, but also more readable as well:
    vb Code:
    1. Private Sub cus_mod_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cus_mod_btn.Click
    2.  
    3.     Dim sql2 As String = "UPDATE customer SET name=@p_name, address=@p_address, contact=@p_contact  WHERE id=@p_id;"
    4.  
    5.     Try
    6.         cnn = New SqlClient.SqlConnection(SQLConnectionString)
    7.         cnn.Open()
    8.  
    9.         cmd = New SqlClient.SqlCommand(sql2, cnn)
    10.  
    11.         With cmd.Parameters
    12.             .AddWithValue("@p_name", cus_mod_nme.Text)
    13.             .AddWithValue("@p_address", cus_mod_add.Text)
    14.             .AddWithValue("@p_contact", cus_mod_con.Text)
    15.             .AddWithValue("p_id", cus_mod_id.SelectedIndex) ' CHECK THIS!!!! .SelectedIndex is probably not the ID from the table.
    16.         End With
    17.  
    18.         Dim ret As Integer = cmd.ExecuteNonQuery()
    19.  
    20.         MsgBox(String.Format("Records affected: {0}", ret))
    21.  
    22.     Catch ex As Exception
    23.         MsgBox("Error: " & ex.Message, MsgBoxStyle.Critical)
    24.     Finally
    25.         cnn.Close()
    26.     End Try
    27. End Sub

    As you see, the ret variable will actually show the number of affected records (if it's 0 then something is wrong).
    And most probably it's due to a combobox/listbox selected index which doesn't correspond to the actual id from the table.

    You should bound your cus_mod_id to a data source (some Datatable perhaps) containing IDs and Display names of your curtomers. Then you should assign its .DisplayMember and .ValueMember properties and use .SelectedValue instead of .SelectedIndex

    For example (provided you have ID and NAME in your customer table):

    Use a data adapter to fill a data table with results of 'SELECT ID, NAME FROM Customer'
    Dim dt As New DataTable("customer_ds")

    Code:
    cus_mod_id.DataSource = dt
    cus_mod_id.DisplayMember = "NAME"
    cus_mod_id.ValueMember = "ID"
    Then you should use .SelectedValue property of the listbox instead of .SelectedIndex
    i tried with a text box by manually entering the id. now its ok. i did using the way posted and i feel more secure than what i done earlier. herafter i'll do my queris parametrized. thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width