473,756 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dataset's - datagridview help

VS 2005 - vb .net - windows xp pro - windows app

All I'm trying to do for now is populate a datagridveiw (dgvTest).
I trying to learn how to retrieve records from DB and add, edit, delete.
I know there are records in the DB b/c I'm able to display records using the
control itself.
Now I want to do it dynamically. I'm really trying to retrieve records from
DB and manipulate them and update them. After I get this to run I want to
use a datareader and learn how that works.

dgvTest is empty when I run this code. I don't know how to bind the data or
if that is even the problem. The code runs w/o error.

Dim SqlCon As SqlConnection
Dim SQL As String
Dim SqlCmd As New SqlCommand
Dim SqlAdpt As New SqlDataAdapter
Dim SqlDS As New DataSet
SqlCon = New SqlConnection(" Server=.\SQLExp ress;AttachDbFi lename=C:\Progr am
Files\Microsoft SQL Server\MSSQL.1\ MSSQL\Data\MyFi rstDatabase.mdf ;
Database=dbname ;Trusted_Connec tion=Yes;")
Try
SQL = "Select * from TonysTable2"
SqlAdpt = New SqlDataAdapter( SQL, SqlCon)
SqlAdpt.SelectC ommand.Connecti on.Open()
SqlAdpt.Fill(Sq lDS, "TonysTable 2")
dgvTest.DataSou rce = SqlDS.DefaultVi ewManager
SqlCon.Close()
SqlCon = Nothing
MsgBox("Whoo hooo connected")
Catch ex As Exception
If SqlCon.State Then
SqlCon.Close()
SqlCon = Nothing
End If
MsgBox(ex.ToStr ing)
End Try
End Sub

Thanks
Aug 27 '08 #1
1 1346
For starters - if you have sql server express running on your
workstation - I would reference the server (by name) in the connection
string and only the name of the database - not the physical mdf file.
At least with the full version of sql server you don't reference the
physical file. Try it as follows and see.

conn1.Connectio nString = "Data Source=serverNa me;Initial
Catalog=DBname; Integrated Security=True"

Here is a sample how to connect to/read data/display it in a
datagridview control:

Imports System
Imports System.Data.Sql Client

Private Sub GetData()
Dim conn1 As New SqlConnection, da As New SqlDataAdapter
Dim ds As New Dataset

conn1.Connectio nString = "Data Source=serverNa me;Initial
Catalog=DBname; Integrated Security=True"

da.SelectComman d = New SqlCommand
da.SelectComman d.Connection = conn1
da.SelectComman d.CommandText = "Select * From TonysTable2"
da.Fill(ds, "tbl1")
Datagridview1.D atasource = ds.Tables("tbl1 ")

End Sub

the line da.Fill(ds, "tbl1") will automatically create a copy of the
table structure of TonysTable2 and load whatever data you select in your
select statement. You can name this table anything you want. It does
not have to be the same name as the server table (or it could be -
doesn't matter). But whatever you name it - that name has to follow in
all the code which references your dataset ds.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8365
by: Robert Koller | last post by:
Hello i have this problem: In a DataGridView the user work with data from a DataSet. on Start the row index from datagridview is the same as the row index from the dataset. Also: DataGridView.Rows(5) <=====> DataSet.Rows(5) .
2
24557
by: bob | last post by:
Can anyone tell me the best way to update a dataset while it is being edited/viewed in the DataGridView control? Is this something that should be inserted into one of the grid's events? or should you update after closing the grid/form, etc.? Also, can you tell me the best book to buy that fully explains the DataGridView control? Thanks.
2
3281
by: Bob | last post by:
I have a CreatedOn field , datetime, which has GetDate() as the default value in SQL server 2000 table. When I create a new record in the table itself in enterprise manager, the field gets populated OK, but when I try to create a new record with a Microsoft datagridview control, I notice that the datecreated value does not get created. It looks as if the datagridview insert statements overrides the default value and prevent the default...
0
1807
by: David Mayerovitch | last post by:
As an exercise in learning Visual Basic 2005, I am putting together a simple XML editor. I place on the form the objects DataGridView1 and DataSet1. ' Read an XML file into DataSet1: DataSet1.ReadXml("BIRDS.XML") ' Bind the DataGridView to the DataSet: DataGridView1.DataSource = DataSet1
1
2881
by: JD | last post by:
I have a DataGridView with a DataSet as DataSource. The user can update the contents of the DataGridView, and then click on a Save button to save the data to an XML file. When they click on Save, I call the subroutine below. The first time that I call it, it works fine. However, if the user makes some more changes in the DataGridView and then wants to save again, I get the following error message: "DataSet can be associated with at...
0
1202
by: twigboy | last post by:
Hi everyone, I have a strongly typed dataset, with my main form having a datagridview that is binded to an access database. The main form has three buttons one to create a new entry in the database one to edit an entry in the database and one to close an entry in the database. All three buttons work in the similar way, such that the entry you select in the datagridview is the entry you want to close or edit. When I click on the open or edit...
0
3251
JordanMartz
by: JordanMartz | last post by:
''' <summary> ''' Populates the DataGridView ''' </summary> Private Sub PopulateDataGridView() ' Set the column header names. userAccessGrid.ColumnCount = 5 userAccessGrid.RowsDefaultCellStyle.BackColor = Color.White userAccessGrid.RowHeadersVisible = True userAccessGrid.Name = "User Access Grid" userAccessGrid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised
1
1525
by: Freddy Coal | last post by:
Hi, I'm newbye in that theme. I would like put the data of a dBase in a DataGridView, why make that?. I'm open my database with this instructions: '---------------- Dim Tabla As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _ & Folder & ";Extended Properties=dBASE IV") Tabla.Open() Folder = Path.GetFileNameWithoutExtension(ruta) 'Name of the file
0
7174
by: Dennis Francek | last post by:
Hello I have populated a treeview from my dataset and by clicking in the treeview i want to get the corresponding table shown in a datagridview. Ive managed this by looking at each DataRow and child rows and added the nodes with a columnname from each datarow. At the same time i have put in a tag at each treenode with a filter string and the current table im looking at as a string to use for later so i can create a datatable and set a filter...
0
2448
by: JerryShaw | last post by:
Problem: create a new winform app in C# VS2008. Drop a PropertyGrid component, Drop a DataSet (untyped) Set the PropertyGrid.SelectedObject to the DataSet. Run the application, and try to select the table editor. Can not bring up the tables collection editor Drop a DataGridView, set the PropertyGrid.SelectedObject to the DataGridView. Can not access the Columns collection. A little history, I am building a Form Designer. I want to...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.