473,666 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pull SQL data in to a DatagridView... ....

Hello,
I am new to pulling data into VB and using DataGridViews.. ... so this
might be a dumb question but I can not seem to understand how to make this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView. The
columns must be created on the fly as the query may return different fields.

A small example I got to work to a List box. If you could convert this to a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #1
6 2341
Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:5E******** *************** ***********@mic rosoft.com...
Hello,
I am new to pulling data into VB and using DataGridViews.. ... so this
might be a dumb question but I can not seem to understand how to make this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView. The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo
Aug 7 '07 #2
Cor,
Sory I did not completly understand you commant but could you show me the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:5E******** *************** ***********@mic rosoft.com...
Hello,
I am new to pulling data into VB and using DataGridViews.. ... so this
might be a dumb question but I can not seem to understand how to make this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView. The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #3
Just the essential part, I have sent another message, however it seems to be
somewhere in nowhere land and I have no copy from it. I am not using the
Hungarian notation as you do, that is not adviced in dotNet. There it is
Pascal notation with some exceptions.

Instead of your datareader

dim dt as datatable
cmd.fill(dt)
dgv.Datasource = dt

I assume that you don't want to concatenate the name and adress now
otherwise you can add a column.

http://msdn2.microsoft.com/en-us/library/632cdz7z.aspx

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:4C******** *************** ***********@mic rosoft.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
>Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:5E******* *************** ************@mi crosoft.com...
Hello,
I am new to pulling data into VB and using DataGridViews.. ... so
this
might be a dumb question but I can not seem to understand how to make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #4
I have pasted this in your code, by the way, did you know that the Hungarian
method is rare used in dotNet.

I have typed this on the fly, so watch typos etc.

\\\
cmd.Connection = conn
cmd.CommandType = CommandType.Tex t
cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
conn.Open
dim dt as new datatable
Try
cmd.Fill(dt)
Catch ex as exception
MessageBox.Show (ex.Tostring)
Finally
conn.Close
End Try
dt.Columns.Add( "Name",System.S tring,"FullName + " " + Address")
Listbox1.DataSo urce = dt
ListBox1.DataMe mber = "Name"
///

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:4C******** *************** ***********@mic rosoft.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
>Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:5E******* *************** ************@mi crosoft.com...
Hello,
I am new to pulling data into VB and using DataGridViews.. ... so
this
might be a dumb question but I can not seem to understand how to make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 7 '07 #5
Cor,
I will look into the Pascal notation. Right now i have all the code in
Hungarian and I dont want to make mods till this works..... Below is the
code you have me with 3 errors I ws unable to repair. Also you dumped the
data into a listbox, I was looking for Datagridview... or shuold i wait till
i se thi code running.

Thanks again
Marcelo

oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT nAME, password FROM t_dssemp"
oSQLconn.Open()
Dim dt As New DataTable
Try
oSQLcmd.Fill(dt ) 'Error - 'Fill' is not a member of
system.data.sql client.sqlcomma nd
Catch ex As Exception
MessageBox.Show (ex.ToString)
Finally
oSQLconn.Close( )
End Try
dt.Columns.Add( "Name", System.String, "FullName") ' Error = 'Sting'
is not a type in 'System' and can not be used as a expression
Listbox1.DataSo urce = dt
ListBox1.DataMe mber = "Name" ' Error - DataMember is not a member of
system.windows. form.listbox
"Cor Ligthert[MVP]" wrote:
I have pasted this in your code, by the way, did you know that the Hungarian
method is rare used in dotNet.

I have typed this on the fly, so watch typos etc.

\\\
cmd.Connection = conn
cmd.CommandType = CommandType.Tex t
cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
conn.Open
dim dt as new datatable
Try
cmd.Fill(dt)
Catch ex as exception
MessageBox.Show (ex.Tostring)
Finally
conn.Close
End Try
dt.Columns.Add( "Name",System.S tring,"FullName + " " + Address")
Listbox1.DataSo urce = dt
ListBox1.DataMe mber = "Name"
///

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:4C******** *************** ***********@mic rosoft.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:
Hi Marcelo,

With the DataGridView a datatable is still working the best. You can use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:5E******** *************** ***********@mic rosoft.com...
Hello,
I am new to pulling data into VB and using DataGridViews.. ... so
this
might be a dumb question but I can not seem to understand how to make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo



Aug 11 '07 #6
Sorry Marcelo,

I did it to much by hand.

You need the dataadapter in it

dim da is new SQLDataAdapter( oSQLcom, oSQLconn)
da.Fill(dt)

binding a datagridview is so easy
dgrv.datasource = dt
(it had to be displaymember, I don't know why I wrote datamember)

I thought it was this
dt.datacolumn.a dd("Name", GetType(System. String), "FullName")

http://msdn2.microsoft.com/en-us/lib...datatable.aspx

Not tested again,

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:A4******** *************** ***********@mic rosoft.com...
Cor,
I will look into the Pascal notation. Right now i have all the code in
Hungarian and I dont want to make mods till this works..... Below is the
code you have me with 3 errors I ws unable to repair. Also you dumped the
data into a listbox, I was looking for Datagridview... or shuold i wait
till
i se thi code running.

Thanks again
Marcelo

oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT nAME, password FROM t_dssemp"
oSQLconn.Open()
Dim dt As New DataTable
Try
oSQLcmd.Fill(dt ) 'Error - 'Fill' is not a member of
system.data.sql client.sqlcomma nd
Catch ex As Exception
MessageBox.Show (ex.ToString)
Finally
oSQLconn.Close( )
End Try
dt.Columns.Add( "Name", System.String, "FullName") ' Error = 'Sting'
is not a type in 'System' and can not be used as a expression
Listbox1.DataSo urce = dt
ListBox1.DataMe mber = "Name" ' Error - DataMember is not a member
of
system.windows. form.listbox
"Cor Ligthert[MVP]" wrote:
>I have pasted this in your code, by the way, did you know that the
Hungarian
method is rare used in dotNet.

I have typed this on the fly, so watch typos etc.

\\\
cmd.Connection = conn
cmd.CommandType = CommandType.Tex t
cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
conn.Open
dim dt as new datatable
Try
cmd.Fill(dt)
Catch ex as exception
MessageBox.Show (ex.Tostring)
Finally
conn.Close
End Try
dt.Columns.Add( "Name",System.S tring,"FullName + " " + Address")
Listbox1.DataSo urce = dt
ListBox1.DataMe mber = "Name"
///

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:4C******* *************** ************@mi crosoft.com...
Cor,
Sory I did not completly understand you commant but could you show me
the
code to take my list view and make it a Datagridview?

thanks
Marcelo

"Cor Ligthert[MVP]" wrote:

Hi Marcelo,

With the DataGridView a datatable is still working the best. You can
use
a
datareader however are always busy to synchronise the indexes from the
columns.

Just my opinion

Cor

"Marcelo" <Ma*****@discus sions.microsoft .comschreef in bericht
news:5E******* *************** ************@mi crosoft.com...
Hello,
I am new to pulling data into VB and using DataGridViews.. ... so
this
might be a dumb question but I can not seem to understand how to
make
this
work.

What I need acomplished:
Exec a select statement and out put the results to a
DataGridView.
The
columns must be created on the fly as the query may return different
fields.

A small example I got to work to a List box. If you could convert
this
to
a
DGV for me it would be great ....
Col1 - Full Name
Col2 - Address
oSQLcmd.Connect ion = oSQLconn
oSQLcmd.Command Type = CommandType.Tex t
oSQLcmd.Command Text = "SELECT FullNAME, Address FROM
t_dssemp"

strSQLopen = OpenSQLConn() ' Open Connection

If oSQLconn.State = ConnectionState .Open Then
oSQLdr = oSQLcmd.Execute Reader() 'executing the command
and
assigning it to connection

While oSQLdr.Read()
ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
oSQLdr.Item("Ad dress"))
End While
End If
oSQLdr.Close()

Thank you very much
Marcelo

Aug 14 '07 #7

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

Similar topics

3
4320
by: Rich | last post by:
Hello, I need to pull data from 2 tables that reside on the same sql server but 2 different databases. In Query Analyzer I can say this select t1.*, t2.* from tbl1 t1 join database2.dbo.tbl1 t2 on t1.RecordID = t2.RecordID The .Net sql command object does not appear to support this kind of syntax. I need to populate one datagridview with the data from DB1.tbl1 and
2
11599
by: Andy | last post by:
I have a C# application that is using a DataGridView (DGV). The DGV uses a table adapter to connect to the datasource, which uses a DataSet as the data. The DataSet has some constraints placed on them (PK on two columns). When I add / update a column that violates the PK constraint, I get an error stating the following: System.Data.ConstraintException: Column 'Item, BItem' is constrained to be unique. at...
2
5013
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and column A binds to string A and column B binds to int B. When i start two threads, the datagridview able to show two rows and updated int B at run time. How? public class Foo { public Foo()
2
8713
by: Rick Shaw | last post by:
Hi, I have a problem with the datagridview not refreshed when the application first appear on the screen. The datagridview display data from a table in the dataset. At the same time, I've added checkbox columns that are not bounded the table. This datagridview is located in the tab (2nd). I thought I mention that since that might be part of the problem (?). When the applications start, it will need some parameter criteria selection...
6
1877
by: alexia.bee | last post by:
Hi all, I am new to C# so please bear me :). I have an app which uses datagridview. it has 8 columns. I also have struct which has 8 members(the same as the datagridview). when I add row to the gridview, i add the same data to this staruct and then add this struct instance to a list. the app creates more then one file with different name. depending on
0
1797
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
9
2674
by: trint | last post by:
How can I retrieve all of the data from a datagridview? Any help is appreciated. Thanks, Trint
4
2557
by: =?Utf-8?B?UmludSBHb3BhbGFrcmlzaG5hIFBpbGxhaQ==?= | last post by:
Hi All, I have an application that fetch data thru a store proc and display in a datagrid, but after successful execution of proc the data grid is not visible.Please go thru the following code and plz let me know what is missing.The data grid propertiy "Visible is true". try { OracleDataAccess oracleDA = new OracleDataAccess("Data
2
11576
by: hjin | last post by:
DataGridView takes very much time if I update a lot of data I have a DataGridView which has a DataSource (a DataTable) of 3000 rows. If I want to update one column of every rows in the DataTable, my whole appliation freezes with any response. After a lot of time, it comes back. If I set the DataSource to null before changing the data and set it back after changing. There' s no problem. But I lost the status of the DataGridView, like...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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,...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5662
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
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.