Click here to Skip to main content
15,887,822 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: One button submit that updates my database(insert,delete,..) without using DataAdapter Pin
Member 1038849419-Dec-13 2:02
Member 1038849419-Dec-13 2:02 
GeneralRe: One button submit that updates my database(insert,delete,..) without using DataAdapter Pin
Dave Kreskowiak19-Dec-13 3:12
mveDave Kreskowiak19-Dec-13 3:12 
GeneralRe: One button submit that updates my database(insert,delete,..) without using DataAdapter Pin
Edward Giles19-Dec-13 22:27
Edward Giles19-Dec-13 22:27 
AnswerRe: One button submit that handles the work with the database Pin
Eddy Vluggen19-Dec-13 8:09
professionalEddy Vluggen19-Dec-13 8:09 
GeneralRe: One button submit that handles the work with the database Pin
Member 1038849419-Dec-13 8:16
Member 1038849419-Dec-13 8:16 
GeneralRe: One button submit that handles the work with the database Pin
Richard Deeming19-Dec-13 8:47
mveRichard Deeming19-Dec-13 8:47 
GeneralRe: One button submit that handles the work with the database Pin
Member 1038849419-Dec-13 9:38
Member 1038849419-Dec-13 9:38 
GeneralRe: One button submit that handles the work with the database Pin
Member 1038849420-Dec-13 7:24
Member 1038849420-Dec-13 7:24 
Hello again. Thanks again Richard Deeming.
I DID IT Big Grin | :-D

I found a simpler way to to what i asked before.
I realised that there is no need for the datatable. I already have a Dataset. so I can do the following:

Here i clear the textboxes in order to insert new data..
VB
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        _isadded = True
        txtAddress.Text = ""
        txtName.Text = ""
        txtEmail.Text = ""
        txtserial.Text = ""
    End Sub


Here: If _isadded = False: I am updating an existing record.
If _isadded = True I am inserting a new record.

VB
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click

        If _isadded = False Then
            _DataSet.Tables(0).Rows(txtCurrent.Text - 1).Item("Name") = txtName.Text
            _DataSet.Tables(0).Rows(txtCurrent.Text - 1).Item("Address") = txtAddress.Text
            _DataSet.Tables(0).Rows(txtCurrent.Text - 1).Item("Email") = txtEmail.Text
        Else
            txtCount.Text = CStr(CInt(txtCount.Text) + 1)
            txtCurrent.Text = txtCount.Text
            Dim _dr As DataRow = _DataSet.Tables(0).NewRow
            _dr.Item(1) = txtCurrent.Text
            _dr.Item(2) = txtName.Text
            _dr.Item(3) = txtEmail.Text
            _dr.Item(4) = txtAddress.Text
            _DataSet.Tables(0).Rows.Add(_dr)
        End If



As for the database..That is what i did:


VB
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
     DataGridView2.Columns.Item(0).Visible = False
     If _isdeleted = True Then
         Dim cmd As New SqlCommand
         If _cn.State = ConnectionState.Closed Then _cn.Open()
         cmd.Connection = _cn
         cmd.CommandText = "delete from tblcustomer where serial = " & Val(txtserial.Text)
         cmd.ExecuteNonQuery()
         txtCurrent.Text = (txtCount.Text) - 1
         RefreshData(True)
     ElseIf _isadded = False Then
         Dim strconnection As String = "Data Source=ELIANE-VAIO\SQLEXPRESS;Initial Catalog=DatabaseConnection;Integrated Security=True;"
         Dim _cn As SqlConnection = New SqlConnection(strconnection)
         Dim cmd As New SqlCommand
         _cn.Open()


         cmd.CommandText = "update tblCustomer set ID=@ID, Name=@Name, Email=@Email, Address=@Address where serial = @serial"
         cmd.Connection = _cn
         cmd.Parameters.AddWithValue("@ID", Me.txtIDD.Text)
         cmd.Parameters.AddWithValue("@Name", Me.txtName.Text)
         cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text)
         cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text)
         cmd.Parameters.AddWithValue("@serial", Me.txtserial.Text)
         cmd.ExecuteNonQuery()
     Else
         Try
             Dim strconnection As String = "Data Source=eliane-vaio\sqlexpress;Initial Catalog=DatabaseConnection;Integrated Security=True;"
             Dim _cn As SqlConnection = New SqlConnection(strconnection)
             Dim cmd As New SqlCommand
             _cn.Open()
             cmd.CommandText = "INSERT INTO tblCustomer([ID],[Name], [Email], [Address]) VALUES (@ID, @Name, @Email, @Address) select @@IDENTITY;"
             cmd.Connection = _cn
             cmd.Parameters.AddWithValue("@ID", Me.txtCurrent.Text)
             cmd.Parameters.AddWithValue("@Name", Me.txtName.Text)
             cmd.Parameters.AddWithValue("@Email", Me.txtEmail.Text)
             cmd.Parameters.AddWithValue("@Address", Me.txtAddress.Text)
             txtserial.Text = cmd.ExecuteScalar.ToString()
             Me._DataAdapter.Fill(Me._DataSet)
             cmdRequery.PerformClick()
             cmdLast.PerformClick()
             'txtAddress.Enabled = False
             'txtEmail.Enabled = False
             'txtName.Enabled = False
         Catch ex As Exception
             MsgBox("Press add before you continue")
         End Try
     End If
 End Sub

Laugh | :laugh: Thumbs Up | :thumbsup:
GeneralRe: One button submit that handles the work with the database Pin
Eddy Vluggen20-Dec-13 7:38
professionalEddy Vluggen20-Dec-13 7:38 
GeneralRe: One button submit that handles the work with the database Pin
Dave Kreskowiak19-Dec-13 9:29
mveDave Kreskowiak19-Dec-13 9:29 
Questionsyntax error while updating a record Pin
Member 1038849418-Dec-13 3:07
Member 1038849418-Dec-13 3:07 
AnswerRe: syntax error while updating a record Pin
Pete O'Hanlon18-Dec-13 3:39
mvePete O'Hanlon18-Dec-13 3:39 
GeneralRe: syntax error while updating a record Pin
Member 1038849418-Dec-13 3:57
Member 1038849418-Dec-13 3:57 
GeneralRe: syntax error while updating a record Pin
Pete O'Hanlon18-Dec-13 4:00
mvePete O'Hanlon18-Dec-13 4:00 
GeneralRe: syntax error while updating a record Pin
Member 1038849418-Dec-13 4:03
Member 1038849418-Dec-13 4:03 
GeneralRe: syntax error while updating a record Pin
Pete O'Hanlon18-Dec-13 4:10
mvePete O'Hanlon18-Dec-13 4:10 
GeneralRe: syntax error while updating a record Pin
Richard Deeming18-Dec-13 4:13
mveRichard Deeming18-Dec-13 4:13 
GeneralRe: syntax error while updating a record Pin
Member 1038849418-Dec-13 4:24
Member 1038849418-Dec-13 4:24 
GeneralRe: syntax error while updating a record Pin
Pete O'Hanlon18-Dec-13 4:38
mvePete O'Hanlon18-Dec-13 4:38 
GeneralRe: syntax error while updating a record Pin
Richard Deeming18-Dec-13 5:02
mveRichard Deeming18-Dec-13 5:02 
JokeRe: syntax error while updating a record Pin
Pete O'Hanlon18-Dec-13 5:07
mvePete O'Hanlon18-Dec-13 5:07 
GeneralRe: syntax error while updating a record Pin
Member 1038849418-Dec-13 21:22
Member 1038849418-Dec-13 21:22 
AnswerRe: syntax error while updating a record Pin
Chris Quinn18-Dec-13 4:18
Chris Quinn18-Dec-13 4:18 
GeneralRe: syntax error while updating a record Pin
Member 1038849418-Dec-13 4:27
Member 1038849418-Dec-13 4:27 
QuestionVS2010 project referencing my .net 4.5 dll Pin
Bruno Leclerc III13-Dec-13 19:30
Bruno Leclerc III13-Dec-13 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.