Show Data in DataGrid with VB.Net 2003 and SQLServer 2000

Please support our VB.NET advertiser: Intel Parallel Studio Home
Jx_Man Jx_Man is offline Offline Feb 4th, 2008, 12:08 pm |
0
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.
Quick reply to this message  
VB.NET Syntax
  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..
0
ITKnight ITKnight is offline Offline | Feb 7th, 2008
worked nice
 
0
kimhanu kimhanu is offline Offline | Mar 26th, 2008
very good
thanks you
 
0
chrisdent1986 chrisdent1986 is offline Offline | 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
 
0
diorbabej diorbabej is offline Offline | Jul 15th, 2008
I received an error when running this :
Error: .NET SqlClient Data Provider: SQL Server does not exist or access denied.

This is what I have for the GetConnect funtion:

Public Function GetConnect()
Dim ServerName As String = "SA1SQL19"
Dim DatabaseName As String = "APPRAISAL"
conn = New SqlConnection("server = ServerName;database = DatabaseName;Trusted_Connection = yes")
Return conn
End Function

Please help! Thank you!
 
0
Jx_Man Jx_Man is offline Offline | Jul 17th, 2008
Public Function GetConnect()
Dim ServerName As String = "SA1SQL19"
Dim DatabaseName As String = "APPRAISAL"
conn = New SqlConnection("server = ServerName;database = DatabaseName;Trusted_Connection = yes")
Return conn
End Function

try this :
  1. Public Function GetConnect()
  2. conn = New SqlConnection("server = SA1SQL19;database = APPRAISAL;Trusted_Connection = yes")
  3. Return conn
  4. End Function
 
0
cindi_rella cindi_rella is offline Offline | Oct 21st, 2008
hi im a newbie here, and i tried your code above but it seems there's an error appear..
here is my code for the connection in sql server..
<conn = New SqlConnection("server =(local);database = HR;Trusted_Connection = yes>

Error: .net SQLclient data provider: Line1: incorrect syntax near '202'.

pls help me pls..tnx
 
0
Lovealious Lovealious is offline Offline | Jul 8th, 2009
In the sample code where is 'dgStudent' defined?
 
0
Jx_Man Jx_Man is offline Offline | Aug 25th, 2009
dg student is datagrid..
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC