开发者

Edit data in a column using insert?

开发者 https://www.devze.com 2023-03-29 08:32 出处:网络
I\'m trying to edit the data of a column. I load the column in textboxes and let the user edit the fields. But now to insert it, I know I can delete the column and add a new one, but couldn\'t I just

I'm trying to edit the data of a column. I load the column in textboxes and let the user edit the fields. But now to insert it, I know I can delete the column and add a new one, but couldn't I just overwrite the data in the column?

Thanks!

'Edit Customer
Dim bllKlant As New bllKlant
Dim objKlant As New Klant
objKlant.KlantNaam = txtNaam.Text
objKlant.KlantBtwNr = txtBtw.Text
objKlant.KlantCode = txtCode.Text
objKlant.KlantAdminLidID = ddlLeden.SelectedV开发者_运维知识库alue
objKlant.KlantDealerID = ddlDealers.SelectedValue

'Edit+ checks
bllKlant.bewerkKlant(objKlant)

In bllKlant I call the function to insert/update the column.

In dalKlant I use the function and return a boolean whether the update was successful. Klant = Customer, it's just in dutch.


Okay, here is how I solved it.

In the aspx.vb page I did the following

Dim bllCust As New bllCust
Dim objCust As New Cust
objCust.Custname = txtName.Text
objCust.CustBtwNr = txtBtw.Text
objCust.CustCode = txtCode.Text
objCust.CustAdminUserID = ddlUsers.SelectedValue
objCust.CustDealerID = ddlDealers.SelectedValue

'Edit+ checks
Try
   bllCust.EditCust(objCust)
Catch ex As Exception
   lblFeedback.Text = ex.Message
End Try

In the bllCust:

Public Function EditCust(ByVal objCust As Cust) As Boolean
<!-- I have checks here to see whether the values don't already excist. -->

    If dataKlant.EditCust(objCust) Then
        Throw New Exception("Customer was edited.")
        Return True
    Else
        Throw New Exception("Customer couldn't be edited, try again.")
        Return False
    End If
End Function

Then finally in the dalCust:

Public Function EditCust(ByVal objCust As Cust) As Boolean
    myconn.Open()

    Dim result As New Boolean

    Dim SQL As String = "update Cust set CustName= @CustName, CustCode = @CustCode where CustID= @CustID"
    Dim cmd As New MySqlCommand(SQL, myconn)

    cmd.Parameters.AddWithValue("@CustID", objCust.CustID)
    cmd.Parameters.AddWithValue("@CustName", objCust.CustName)
    cmd.Parameters.AddWithValue("@CustCode", objCust.CustCode)

    result= cmd.ExecuteNonQuery()

    myconn.Close()
    Return result
End Function

That's how I did it. I also translated the dutch to english so everyone understands it better.

0

精彩评论

暂无评论...
验证码 换一张
取 消