943,155 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3335
  • VB.NET RSS
Feb 4th, 2010
0

Saving datagrid data in SQL database

Expand Post »
hie if you could help me out i'll be forever grateful!!

okay i am creating an ordering system but when i try to add a new customer it allos me to add but it doesnt save to the database and i can not also call it up the code i have is:

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub frmAddCustomer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. ' Purpose: Add new Customer record and assign its default values
  3. ' Clear the tables in the DataSet ready for a new record to be added
  4. 'dtasetAmway.Tables("OrderDeatils").Clear()
  5. dtasetAmway.Tables("Customer").Clear()
  6. ' Add a new record to Customer table
  7. dtasetAmway.Tables("Customer").Rows.Add(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
  8. ' Make this the current row
  9. currentrowno = 0
  10. ' Update the Customer table in the database so the CustomerNo gets allocated by SQL Server
  11. dtaadpCustomer.Update(dtasetAmway, "Customer")
  12. ' Retrieve the record just added to the Customer table in the database so user can see CustomerNo
  13. ' Create query to retrieve required record: the record with highest CustomerNo
  14. dtaadpCustomer.SelectCommand.CommandText = "SELECT TOP 1 * FROM Customer ORDER BY CustomerNo DESC"
  15. ' Empty the Customer table in the DataSet so that the Customer record can be retrieved from the database
  16. dtasetAmway.Tables("Customer").Clear()
  17. ' Execute the SqlCommand to fill a table called Customer in the DataSet
  18. dtaadpCustomer.Fill(dtasetAmway, "Customer")
  19. ' Make a note that user is adding a record
  20. AddRecord = True
  21. 'View CustomerNo in textbox
  22. txtCustomerNo.Text = dtasetAmway.Tables("Customer").Rows(currentrowno).Item("CustomerNo")
  23. 'Make txtCustomer Read Only so that it can not be changed
  24. txtCustomerNo.ReadOnly = True
  25. End Sub

Save:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  2.  
  3. ' Purpose: Save the changes made to the Customer record on the screen
  4. ' Check if compulsory fields don't contain data, show error message and place cursor back in field
  5. If txtFirstName.Text = "" Then
  6. MessageBox.Show("First Name must be filled in.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  7. txtFirstName.Focus()
  8. ElseIf txtSurname.Text = "" Then
  9. MessageBox.Show("Surname must be filled in.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  10. txtSurname.Focus()
  11. ElseIf txtDOB.Text = "" Then
  12. MessageBox.Show("D.O.B. must be filled in.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  13. txtDOB.Focus()
  14. ElseIf txtAddress.Text = "" Then
  15. MessageBox.Show("Address must be filled in.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  16. txtAddress.Focus()
  17. ElseIf txtTelephone.Text = "" Then
  18. MessageBox.Show("Telephone must be filled in.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  19. txtTelephone.Focus()
  20.  
  21. Else
  22. ' Purpose: Save the changes made to the Customer record on the screen
  23. ' Copy the data from the screen into the DataSet
  24. dtasetAmway.Tables("Customer").Rows(currentrowno)("FirstName") = txtFirstName.Text
  25. dtasetAmway.Tables("Customer").Rows(currentrowno)("Surname") = txtSurname.Text
  26. dtasetAmway.Tables("Customer").Rows(currentrowno)("DOB") = txtDOB.Text
  27. dtasetAmway.Tables("Customer").Rows(currentrowno)("Address") = txtAddress.Text
  28. dtasetAmway.Tables("Customer").Rows(currentrowno)("TelephoneNo") = txtTelephone.Text
  29. ' Update the Account table in the database
  30. dtaadpCustomer.Update(dtasetAmway, "Customer")
  31. MessageBox.Show("The updates have been saved to disk.", "Save Updates", MessageBoxButtons.OK, MessageBoxIcon.Information)
  32. 'close form and open Menu
  33. Me.Hide()
  34. frmMenu.Show()
  35. End If
  36. End Sub

thanks in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MCBambi is offline Offline
1 posts
since Feb 2010
Feb 5th, 2010
0
Re: Saving datagrid data in SQL database
>but it doesnt save to the database...
Please post connectionString (do not add username/password) and exception trace.
PS: Please check the database file located at Bin folder of application.
Last edited by adatapost; Feb 5th, 2010 at 6:15 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008
Feb 8th, 2010
-1
Re: Saving datagrid data in SQL database
This is code add data in to SQL 2000 using VB.NET 2008
Database:
Table: tbl_users
id char(5) Primary key
Acc nvarchar(32)
Pass nvarchar(32)
Email nvarchar(200)
Module
VB.NET Syntax (Toggle Plain Text)
  1. Imports System.Data.SqlClient
  2. Module Module1
  3. Public strSQL As String
  4. Public MyConn As New SqlConnection("Server=QTMLC;Database=Test;UID=sa;PWD=sa")
  5. End Module

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.Data.SqlClient
  2. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  3. Try
  4. If txtId.Text = "" Or txtAcc.Text = "" Or txtPass.Text = "" Or txtEmail = "" Then
  5. MsgBox("You must enter fully in to textbox", MsgBoxStyle.OkOnly, "Error")
  6. Return
  7. End If
  8. strSQL = "Insert into tbl_users (Id, Acc, Pass, email)"
  9. strSQL = strSQL + " values('" & UCase(txtId.Text) & "','" & txtAcc.Text & "','" & txtPass.Text & "','" & txtEmail.Text & "')"
  10. MyConn.Open()
  11. Dim cmd As New SqlCommand(strSQL, MyConn)
  12. cmd.ExecuteNonQuery()
  13. MyConn.Close()
  14. strSQL = Nothing
  15. Catch ex As Exception
  16. MyConn.Close()
  17. MsgBox("Account: " & txtAcc.Text & "is registed!")
  18. End Try
  19. ShowData()
  20. End Sub

VB.NET Syntax (Toggle Plain Text)
  1. Public Sub ShowData()
  2. Try
  3. strSQL = "SELECT id, Acc, Pass, Email FROM tbl_users"
  4. MyConn.Open()
  5. Dim Myda As New SqlDataAdapter(strSQL, MyConn)
  6. Dim Mydt As New DataTable
  7. Myda.Fill(Mydt)
  8. Me.DataGridView1.DataSource = Mydt
  9. KT_DN = True
  10. strSQL = Nothing
  11. MyConn.Close()
  12. Catch ex As Exception
  13. MyConn.Close()
  14. MsgBox("Disconnect from server")
  15. End Try
  16. End Sub

File attach Pass: www.gd210.com
Attached Files
File Type: zip Test.zip (341.9 KB, 158 views)
Last edited by gd2010; Feb 8th, 2010 at 1:57 am. Reason: edit code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gd2010 is offline Offline
1 posts
since Feb 2010
Feb 8th, 2010
0
Re: Saving datagrid data in SQL database
To help protect against SQL statement exploits, never create SQL queries using string concatenation. Instead, use a parameterized query and assign user input to parameter objects.

Read - ADO.NET Security Best Practices
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: radiobutton
Next Thread in VB.NET Forum Timeline: [OOD] Class design for with Database back end





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC