doubt: sql + ado.net + vb.net

doubt: sql + ado.net + vb.net

Post by Charlie Pa » Fri, 20 Mar 2009 08:05:45


Hi there,

I'm trying to recycle myself to VB.net. I'm trying to get this to work but
when I click the button a window pops up with "Could not find
ArgumentException" and "column 'reffactura' doesn't belong to the table".
Well my desktop language is spanish and in english this may differ a
little...

I have this:

Imports System.Data

Imports System.Data.SqlClient

Public Class Form2

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click



Dim GeoSQLConn As SqlClient.SqlConnection

GeoSQLConn = New SqlClient.SqlConnection()

GeoSQLConn.ConnectionString = "data source = DAVID-PORTATIL\David; initial
catalog = geoflux; integrated security = true"

GeoSQLConn.Open()

Dim GeoAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT reffactura
FROM ordre", GeoSQLConn)

Dim GeoSet As DataSet = New DataSet()

GeoAdapter.Fill(GeoSet)

Dim GeoTable As DataTable = New DataTable()

Dim nouOrdre As DataRow = GeoTable.NewRow()

nouOrdre("reffactura") = RefFactura.Text

GeoTable.Rows.Add(nouOrdre)

GeoAdapter.Update(GeoTable)

GeoTable.AcceptChanges()

End Sub

End Class

What's wrong?

Thanks in advance
 
 
 

doubt: sql + ado.net + vb.net

Post by Norman Yua » Fri, 20 Mar 2009 08:17:02

See comments inline.





You just created a new DataTable, which is blank DataTable without
DataColumn being defined.

I guess you meant for a table that is in the DataSet with data filled. So,
you should find the table in the DataSet, not creating a new, blank one, as
you did below.



So, the DataRow you got here does not have item corresponding to any
DataColumn



Of course the DataRow does not have a Column called "reffactura", it has no
column at all.
Here is where you got the runtime error.

 
 
 

doubt: sql + ado.net + vb.net

Post by Cor Ligthe » Fri, 20 Mar 2009 16:35:36

Charlie,

Your datatabe to use is in your case:

Geoset.Tables(0) 'much more possibilities to write this

Cor
 
 
 

doubt: sql + ado.net + vb.net

Post by Charlie Pa » Sat, 21 Mar 2009 08:24:15

Hello,

Thanks for your help, but I must confess I'm not getting the point.

Please, be patient... and correct me if I'm wrong:

1.-The connection:

Dim GeoSQLConn As SqlClient.SqlConnection

GeoSQLConn = New SqlClient.SqlConnection()

2.- The connection String:

GeoSQLConn.ConnectionString = "data source = DAVID-PORTATIL\David; initial
catalog = geoflux; integrated security = true"

3.- Open connection:

GeoSQLConn.Open()

4.- From now on I'm not very sure of anyting. Let's start supposing:

4.1.- I see in tutorials that to get data from a db I need a connection, a
dataadapter and a dataset. We've seen the connection, now let's get to the
dataadapter:

Dim GeoAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM ordre",
GeoSQLConn)

Question: In the Sql instruction should I use the column names separated by
commas or the asterisk is ok?

5.- The Dataset:

Dim GeoSet As DataSet = New DataSet()

GeoAdapter.Fill(GeoSet)

6.- Well as you can see this is a db called geoflux and the table is called
ordre. This db is empty. I have a form and want to fill in the db with the
text in the textboxes. "reffactura" is a column in Table "ordre". I want to
fill the column "reffactura" with RefFactura.text containt

Question: How should I proceed?

Thanks very much for your help. Can you recommend me some tutorial with
clear examples? I am not able to learn from msdn, I'm afraid.



"Charlie Parker" < XXXX@XXXXX.COM > escribi?en el mensaje
 
 
 

doubt: sql + ado.net + vb.net

Post by Cor Ligthe » Sat, 21 Mar 2009 16:44:11

> Please, be patient... and correct me if I'm wrong:
Dim GeoSQLConn As new SqlClient.SqlConnection(TheConnectionString)




http://www.yqcomputer.com/

This should be done, only if you do something with that connection (not with
a dataadapter, that does it automaticly)

Commas, the * means all columns, there is no comma needed after the last
column


Dim GeoSet as New DataSet
The way you use it is a kind of what you see done by C# people.
VB has shorter code for this

Creating a form with a control on it, by instance a DataGridView
You can then add your table to that by
dim bs as new BindingSource
bs.DataSource = GeoSet.Tables(0)
dgv.DataSource = bs