Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Professionals,
i had assigned a assignment for insert,update, delete operation in a table for my student. he had done this assignment fine. but his code made me crazy that he wrote update code like below

Working sample:


C#
Public Function Toeditdetails(ByVal item As Boolean)

        If item = True Then

            Dim con As SqlConnection
            Dim retval As Integer

            con = New SqlConnection(conn)
            con.Open()
            Dim cmd As SqlCommand
            cmd = New SqlCommand("updatelogin", con)
            cmd.CommandType = CommandType.StoredProcedure


            cmd.Parameters.Add(New SqlParameter("@username", SqlDbType.VarChar, 100)).Value() = TextBox1.Text.Trim()
            cmd.Parameters.Add(New SqlParameter("@user_password", SqlDbType.VarChar, 30)).Value() = TextBox2.Text.Trim()
            cmd.Parameters.Add(New SqlParameter("@nameofuser", SqlDbType.VarChar, 100)).Value() = TextBox3.Text.Trim()
            cmd.Parameters.Add(New SqlParameter("@contact_no", SqlDbType.VarChar, 10)).Value() = TextBox4.Text.Trim()
            cmd.Parameters.Add(New SqlParameter("@email", SqlDbType.VarChar, 100)).Value() = TextBox5.Text.Trim()
            cmd.Parameters.Add(New SqlParameter("@joiningdate", SqlDbType.Date)).Value() = DateTimePicker1.Value
            cmd.Parameters.Add(New SqlParameter("@usergroup_id", SqlDbType.Int)).Value() = SelectedID(ComboBox1.Text)

            If item = True Then
                cmd.Parameters.Add(New SqlParameter("@id", SqlDbType.Int)).Value() = Label9.Text.Trim()
            End If

            Dim da As SqlDataAdapter
            da = New SqlDataAdapter(cmd)
            Dim dst As New DataSet
            da.Fill(dst)

            If dst.Tables.Count > 0 Then
                DataGridView1.DataSource = dst.Tables(0)
            End If

            If Not cmd Is Nothing Then
                cmd.Dispose()
            End If
        End If

End Function


Note there is no ExecuteNonQuery() method for his SqlCommand object and he didn't call this method in entire project. when i call this function result still updating... how its happen? anyone genius can you explain what's going on above statement? any help would be appreciated


Regards and Thanks
Posted

1 solution

The reason it happens is because your student is using a DataSet and SqlDataAdapter which still executes code (da.Fill). Normally they are used for selecting data and you would use the SqlCommand for updating/inserting but this method can still work.
 
Share this answer
 

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