User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 363,778 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,473 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser:
Feb 4th, 2008
Views: 3,240
This Code is easy way to load / show data in Datagrid. This Code write in vb.net 2003 and use sqlserver 2000 as database. I use module to connected VB.Net with SqlServer 2000. so the function to connected can be use in every form in this project.
vbnet Syntax | 5 stars
  1. 'This Code needed one DataGrid On Form.
  2.  
  3.  
  4.  
  5. 'In Module
  6. 'Declare outside of class
  7. Imports System.Data
  8. Imports System.Data.SqlClient
  9.  
  10. Module Koneksi
  11. Public conn As SqlConnection
  12. Public Function GetConnect()
  13. conn = New SqlConnection("server = MyServerName;database = MyDatabaseName;Trusted_Connection = yes")
  14. Return conn
  15. End Function
  16. End Module
  17.  
  18. 'In Form
  19. 'Declare outside of class
  20. Imports System.Data
  21. Imports System.Data.SqlClient
  22.  
  23. 'Procedure To show data in DataGrid
  24. Private Sub ShowDataGrid()
  25. Dim conn As SqlConnection
  26. Dim cmdStudent As New SqlCommand
  27. Dim daStudent As New SqlDataAdapter
  28. Dim dsStudent As New DataSet
  29. Dim dtStudent As New DataTable
  30.  
  31. conn = GetConnect()
  32. Try
  33. cmdStudent = conn.CreateCommand
  34. cmdStudent.CommandText = "SELECT * FROM YourTableName"
  35. daStudent.SelectCommand = cmdStudent
  36. daStudent.Fill(dsStudent, "YourTableName")
  37. dgStudent.DataSource = dsStudent
  38. dgStudent.DataMember = "YourTableName"
  39. dgStudent.ReadOnly = True
  40. Catch ex As Exception
  41. MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
  42. End Try
  43. End Sub
  44.  
  45. ' Call procedure ShowDataGrid() on event Form Load
  46. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  47. ShowDataGrid() ' Load data to view in data grid
  48. End Sub
  49. ' You can change the event to call this procedure like in button pressed and etc..
Comments (Newest First)
chrisdent1986 | Newbie Poster | May 29th, 2008
hi I'm doing a similar project where I use a VB system to create users for the SQL server.

I created a dataset (I'm assuming this is the same as creating a module?) and I have a text box called txtUsername and one called txtPassword and a command button called cmdCreate

Obviously the user would fill in the text boxes with information on the new user and when they click the command button it will create a new user with those details. Could you possibly modify the above code to do this and perhaps show error messages in the VB display for instance if a user already exists etc etc.

Thanks for your help

Chris
kimhanu | Newbie Poster | Mar 26th, 2008
very good
thanks you
ITKnight | Light Poster | Feb 7th, 2008
worked nice
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC