dataset question

jvcoach23

Member
Joined
Feb 21, 2005
Messages
17
Location
IL
Programming Experience
1-3
I have a dataset that I'm brining data into from a sql server. when i bring data into the dataset.. it creates a table in the dataset and that table has 1 column. What I want to do is add a column to that table, so that I can put an image in it from the file system. The column I'm bringing back from the database holds the path to the image. I've tried adding that column to the datasets table, but every time I do it, it ends up creating a second table in the dataset. How do I add the column to the existing table in the dataset.

the dataset
ex: da.Fill(ds, "Pic")

adding a column to it
dt.Columns.Add("Image", GetType(System.Byte))

thanks for the help
Shannon
 
A dataset is usually used for containing multiple dataTables (and perhaps relations). If you don't need a dataset, simply fill only a dataTable, then add the new column to that same dataTable.
If you do need the functionality of a dataSet, then when adding the new column be sure to reference the same dataTable. The code could be something like: ds.Tables(0).Columns.Add("Image", GetType(System.Byte)).
Yu haven't showed you declaration for dt, but I'm assuming it's a different dataTable than the one created during the Fill method by the dataAdapter.
 
thanks for the replie.

The book i'm using for my example is for use with Crystal Reports CR. It says to use the dataset so that I can do a writexmlscheme for crystal to use. The it says that you use the database within to store the data and the images. So another question I have.. if doing the fill from the dataadapter creates a table in the dataset..do I really need to create another table. So here is what I've got right now.. it doesn't work.. but you'll likely see my problem right off.

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] cn [/size][size=2][color=#0000ff]As[/color][/size][size=2] SqlConnection
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] cm [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlCommand
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] da [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlDataAdapter
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ds [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] DataSet
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] dr [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataRow
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] drds [/size][size=2][color=#0000ff]As[/color][/size][size=2] DataRow
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] dt [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] DataTable
 
cn = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlConnection("pwd=sidskip1; UID=sa; server=(local); database=Shannon")
 
cm = [/size][size=2][color=#0000ff]New[/color][/size][size=2] SqlCommand("select vcFileName from tblShannon", cn)
 
cm.CommandType = CommandType.Text
 
[/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]cn.Open()
 
da.SelectCommand = cm
 
cn.Close()
 
[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception
 
[/size][size=2][color=#0000ff]Throw[/color][/size][size=2] ex
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Try
 
[/color][/size][size=2]da.Fill(ds, "Pic")
 
ds.Tables("Pics").Columns.Add("Image", [/size][size=2][color=#0000ff]GetType[/color][/size][size=2](System.Byte))[/size]

the error is object reference not set to an instance of an object. it's in the line above.
 
I think i am around that last issue... did it like you example where

table(0)

instead of
table("Pics")

can you explain why the 0 worked. that is the first table in the dataset correct.. but I had already named that table with the "Pics". why can't i referrence it to add the column..

hope your up to do a little educating.
thanks
shannon
 
jvcoach23 said:
if doing the fill from the dataadapter creates a table in the dataset..do I really need to create another table
If the Fill method executes correctly then a table is created and you don't need to create another table. In your code, you close the connection before calling the Fill method. The Fill method needs an open connection to work. I'm surprised if you didn't get an error.
If you have an open connection while calling the Fill method, the line 'ds.Tables("Pics").Columns.Add("Image", GetType(System.Byte))' should work since the table created by the Fill command that you've shown names the table "Pic".
As I said before, if you don't need the functionality of a dataset (more than one Table), simply fill only a dataTable.
 
ok.. since i need the dataset to do an xmlwritescheme.. I guess I could do that once.. then after that just populate to a datatable...

thanks again for you help
shannon
 
Back
Top