473,378 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

DataTable before DataAdapter possible?

Hello all,

I have a DataTable which I am building column by column and adding rows
after each new column. The DataTable columns match the columns in my
database table. I'm building the DataTable first and I then want to roll
through the DataTable while in memory checking for errors and then commit
the rows to my database table (btw this is in ASP.NET). Is it possible to
have data in a datable before attaching at DataAdapter? I'm a little new to
this and know I need a SqlCommandBuilder, so some code example would be very
helpful. Below is what I know about using the DataAdapter/CommandBuilder,
but I don't need to SELECT anything because the DataTable already had new
rows (that why I put Id = 0 to get no data).

dim dt as New DataTable
dim da as DataAdapter
dim sqlCmdBuild as SqlCommandBuilder

da = New SqlDataAdapter("SELECT * FROM Table WHERE Id = 0", OpenDbConn)
da..Fill(dt)

sqlCmdBuild = New SqlCommandBuilder(da)

TIA,

-Randy
Jul 21 '05 #1
5 2214

"randy" <ra***@someplace.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
Hello all,

I have a DataTable which I am building column by column and adding rows
after each new column. The DataTable columns match the columns in my
database table. I'm building the DataTable first and I then want to roll
through the DataTable while in memory checking for errors and then commit
the rows to my database table (btw this is in ASP.NET). Is it possible to
have data in a datable before attaching at DataAdapter?
Yes, and you never technically attach a datatable to an adapter. An adapter
could care less what you send to it, it only cares in that it matches the
commands that you try to have it send the db.
I'm a little new to this and know I need a SqlCommandBuilder,
Not so, and in all likelihood, avoid the command builder. Check out Bill
Vaughn's article Weaning Developer's from the commandBUilder at
www.betav.com ->Articles ->MSDN
so some code example would be very helpful. Below is what I know about using the DataAdapter/CommandBuilder,
but I don't need to SELECT anything because the DataTable already had new
rows (that why I put Id = 0 to get no data).
If you use the command builder, you should be ok, but that query will
probably cause drama. The commanduilder infers update/insert/delete logic
based on athe Select Command so that's all it needs. You don't need to
write over your exisitn gdata. You also probably want to use Parameterized
queries... Where ID = @SomeValue"

then do mySelectCommand.Parameters.Add("@SomeValue", SqlDbType.Whatever,
someLength).Value = 0

Run through the dataadapter configuration wizard at least once and check out
the code it generates for you. It will have parameters and column mappings
as well. While I don't recommend using it too much b/c it becomes a crutch,
it's a superb learning too.

Also, you don't have to call Select First. You don't even have to call
update on a datatable that you called fill on, or even one whose data came
from the db. The adapter doesn't care. I know this may seem like a lot so
you may want to check out Bill's article, play with the wizard, and just get
a valid Update command. Then do what you are now, and just call update on
your datatable. Let me know if you have any problems, I'll do my best to
get you through them.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

dim dt as New DataTable
dim da as DataAdapter
dim sqlCmdBuild as SqlCommandBuilder

da = New SqlDataAdapter("SELECT * FROM Table WHERE Id = 0", OpenDbConn)
da..Fill(dt)

sqlCmdBuild = New SqlCommandBuilder(da)

TIA,

-Randy

Jul 21 '05 #2
Thanks for you quick response. I went though the configuration wizdard and
looked over the example from betav.com. I mostly understand the code, but
I'm not making a connection between what I want/need to do and the example.

Simple example of what I'm doing. First I build my datatable and then add a
row to be inserted...

Dim dt as new datatable
Dim datCol as DataColumn
Dim DatRow as DataRow

datCol = new DataColumn("FName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datCol = new DataColumn("LName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datRow = dt.NewRow
datRow.Item("FName") = "Joe"
datRow.Item("LName") = "Smith"
dt.Rows.Add(datRow)

This is only one row, but I could have 500+ new rows. The "FName" and
"LName" are the same in my database table. Are you saying that I need to
loop though the rows of the datatable and assign them Paramerters? Could
please add some code below so I can better understand what I need next?


"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

"randy" <ra***@someplace.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
Hello all,

I have a DataTable which I am building column by column and adding rows
after each new column. The DataTable columns match the columns in my
database table. I'm building the DataTable first and I then want to roll through the DataTable while in memory checking for errors and then commit the rows to my database table (btw this is in ASP.NET). Is it possible to have data in a datable before attaching at DataAdapter?
Yes, and you never technically attach a datatable to an adapter. An

adapter could care less what you send to it, it only cares in that it matches the
commands that you try to have it send the db.
I'm a little new to
this and know I need a SqlCommandBuilder,
Not so, and in all likelihood, avoid the command builder. Check out Bill
Vaughn's article Weaning Developer's from the commandBUilder at
www.betav.com ->Articles ->MSDN
so some code example would be very
helpful. Below is what I know about using the DataAdapter/CommandBuilder, but I don't need to SELECT anything because the DataTable already had new rows (that why I put Id = 0 to get no data).


If you use the command builder, you should be ok, but that query will
probably cause drama. The commanduilder infers update/insert/delete logic
based on athe Select Command so that's all it needs. You don't need to
write over your exisitn gdata. You also probably want to use

Parameterized queries... Where ID = @SomeValue"

then do mySelectCommand.Parameters.Add("@SomeValue", SqlDbType.Whatever,
someLength).Value = 0

Run through the dataadapter configuration wizard at least once and check out the code it generates for you. It will have parameters and column mappings as well. While I don't recommend using it too much b/c it becomes a crutch, it's a superb learning too.

Also, you don't have to call Select First. You don't even have to call
update on a datatable that you called fill on, or even one whose data came
from the db. The adapter doesn't care. I know this may seem like a lot so you may want to check out Bill's article, play with the wizard, and just get a valid Update command. Then do what you are now, and just call update on
your datatable. Let me know if you have any problems, I'll do my best to
get you through them.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

dim dt as New DataTable
dim da as DataAdapter
dim sqlCmdBuild as SqlCommandBuilder

da = New SqlDataAdapter("SELECT * FROM Table WHERE Id = 0", OpenDbConn)
da..Fill(dt)

sqlCmdBuild = New SqlCommandBuilder(da)

TIA,

-Randy


Jul 21 '05 #3
If the rowstate of the rows isn't added/deleted etc,the call to your update
statement is never made. If set up correctly, the adapter will walk through
the table for you and handle all of this, automatically assigning the
parameters in your quereis to the values in the datatable.

If you are building it manually like below, you just need a valid Insert
command but that command must map to the datatable in order for you to call
..Update and have it do anything.
"randy" <rs****@portfoliorecovery.nospam.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
Thanks for you quick response. I went though the configuration wizdard and looked over the example from betav.com. I mostly understand the code, but
I'm not making a connection between what I want/need to do and the example.
Simple example of what I'm doing. First I build my datatable and then add a row to be inserted...

Dim dt as new datatable
Dim datCol as DataColumn
Dim DatRow as DataRow

datCol = new DataColumn("FName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datCol = new DataColumn("LName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datRow = dt.NewRow
datRow.Item("FName") = "Joe"
datRow.Item("LName") = "Smith"
dt.Rows.Add(datRow)

This is only one row, but I could have 500+ new rows. The "FName" and
"LName" are the same in my database table. Are you saying that I need to
loop though the rows of the datatable and assign them Paramerters? Could
please add some code below so I can better understand what I need next?


"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

"randy" <ra***@someplace.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
Hello all,

I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my
database table. I'm building the DataTable first and I then want to roll through the DataTable while in memory checking for errors and then commit the rows to my database table (btw this is in ASP.NET). Is it possible
to
have data in a datable before attaching at DataAdapter?


Yes, and you never technically attach a datatable to an adapter. An

adapter
could care less what you send to it, it only cares in that it matches the
commands that you try to have it send the db.
I'm a little new to
this and know I need a SqlCommandBuilder,


Not so, and in all likelihood, avoid the command builder. Check out Bill Vaughn's article Weaning Developer's from the commandBUilder at
www.betav.com ->Articles ->MSDN
so some code example would be very
helpful. Below is what I know about using the DataAdapter/CommandBuilder, but I don't need to SELECT anything because the DataTable already had new rows (that why I put Id = 0 to get no data).


If you use the command builder, you should be ok, but that query will
probably cause drama. The commanduilder infers update/insert/delete

logic based on athe Select Command so that's all it needs. You don't need to
write over your exisitn gdata. You also probably want to use

Parameterized
queries... Where ID = @SomeValue"

then do mySelectCommand.Parameters.Add("@SomeValue", SqlDbType.Whatever,
someLength).Value = 0

Run through the dataadapter configuration wizard at least once and check

out
the code it generates for you. It will have parameters and column

mappings
as well. While I don't recommend using it too much b/c it becomes a

crutch,
it's a superb learning too.

Also, you don't have to call Select First. You don't even have to call
update on a datatable that you called fill on, or even one whose data came from the db. The adapter doesn't care. I know this may seem like a lot

so
you may want to check out Bill's article, play with the wizard, and just

get
a valid Update command. Then do what you are now, and just call update on your datatable. Let me know if you have any problems, I'll do my best to get you through them.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

dim dt as New DataTable
dim da as DataAdapter
dim sqlCmdBuild as SqlCommandBuilder

da = New SqlDataAdapter("SELECT * FROM Table WHERE Id = 0",

OpenDbConn) da..Fill(dt)

sqlCmdBuild = New SqlCommandBuilder(da)

TIA,

-Randy



Jul 21 '05 #4
Could you PLEASE supply a little code for my example???

Thanks,

-Randy

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:ei*************@TK2MSFTNGP09.phx.gbl...
If the rowstate of the rows isn't added/deleted etc,the call to your update statement is never made. If set up correctly, the adapter will walk through the table for you and handle all of this, automatically assigning the
parameters in your quereis to the values in the datatable.

If you are building it manually like below, you just need a valid Insert
command but that command must map to the datatable in order for you to call .Update and have it do anything.
"randy" <rs****@portfoliorecovery.nospam.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
Thanks for you quick response. I went though the configuration wizdard and
looked over the example from betav.com. I mostly understand the code, but
I'm not making a connection between what I want/need to do and the

example.

Simple example of what I'm doing. First I build my datatable and then add a
row to be inserted...

Dim dt as new datatable
Dim datCol as DataColumn
Dim DatRow as DataRow

datCol = new DataColumn("FName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datCol = new DataColumn("LName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datRow = dt.NewRow
datRow.Item("FName") = "Joe"
datRow.Item("LName") = "Smith"
dt.Rows.Add(datRow)

This is only one row, but I could have 500+ new rows. The "FName" and
"LName" are the same in my database table. Are you saying that I need
to loop though the rows of the datatable and assign them Paramerters? Could please add some code below so I can better understand what I need next?


"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

"randy" <ra***@someplace.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
> Hello all,
>
> I have a DataTable which I am building column by column and adding rows > after each new column. The DataTable columns match the columns in my > database table. I'm building the DataTable first and I then want to

roll
> through the DataTable while in memory checking for errors and then

commit
> the rows to my database table (btw this is in ASP.NET). Is it possible
to
> have data in a datable before attaching at DataAdapter?

Yes, and you never technically attach a datatable to an adapter. An

adapter
could care less what you send to it, it only cares in that it matches

the commands that you try to have it send the db.
I'm a little new to
> this and know I need a SqlCommandBuilder,

Not so, and in all likelihood, avoid the command builder. Check out Bill Vaughn's article Weaning Developer's from the commandBUilder at
www.betav.com ->Articles ->MSDN
so some code example would be very
> helpful. Below is what I know about using the

DataAdapter/CommandBuilder,
> but I don't need to SELECT anything because the DataTable already had new
> rows (that why I put Id = 0 to get no data).

If you use the command builder, you should be ok, but that query will
probably cause drama. The commanduilder infers update/insert/delete logic based on athe Select Command so that's all it needs. You don't need
to write over your exisitn gdata. You also probably want to use

Parameterized
queries... Where ID = @SomeValue"

then do mySelectCommand.Parameters.Add("@SomeValue", SqlDbType.Whatever, someLength).Value = 0

Run through the dataadapter configuration wizard at least once and check
out
the code it generates for you. It will have parameters and column

mappings
as well. While I don't recommend using it too much b/c it becomes a

crutch,
it's a superb learning too.

Also, you don't have to call Select First. You don't even have to
call update on a datatable that you called fill on, or even one whose data

came from the db. The adapter doesn't care. I know this may seem like a
lot so
you may want to check out Bill's article, play with the wizard, and
just get
a valid Update command. Then do what you are now, and just call

update on your datatable. Let me know if you have any problems, I'll do my best to get you through them.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com

>
> dim dt as New DataTable
> dim da as DataAdapter
> dim sqlCmdBuild as SqlCommandBuilder
>
> da = New SqlDataAdapter("SELECT * FROM Table WHERE Id = 0", OpenDbConn) > da..Fill(dt)
>
> sqlCmdBuild = New SqlCommandBuilder(da)
>
> TIA,
>
> -Randy
>
>



Jul 21 '05 #5
INSERT INTO Employees
( LName, FName)
VALUES (@LName, @FName)
This will be the CommandText for the insert command. Add an update command
to dataAdapter1 for instance and use this text.

At the end of your code where you are adding your row (dt.Rows.Add(datRow);

call dataAdapter1.Update(dt)

I've just did this making the mods however I configured my dataadapter using
the wizard. Nonetlesss this is pretty much it (I used emplnum as a key but
you can just remove it)
Me.SqlInsertCommand2.CommandText = "INSERT INTO Employees(EmplNum, LastName,
FirstName) VALUES (@EmplNum, @LastName, " & _

"@FirstName); SELECT EmplNum, LastName, FirstName FROM Employees WHERE
(EmplNum =" & _

" @EmplNum)" THE SELECT STATEMENT CAN BE REMOVED TOO, IT'S ONLY USED TO
REFRESH THE DATASET

Me.SqlInsertCommand2.Connection = Me.cn

Me.SqlInsertCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@EmplNum", System.Data.SqlDbType.Int, 4,
"EmplNum")) 'REMOVE THIS LINE

Me.SqlInsertCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@LastName",
System.Data.SqlDbType.NVarChar, 20, "LastName"))

Me.SqlInsertCommand2.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@FirstName",
System.Data.SqlDbType.NVarChar, 20, "FirstName"))

'
"randy" <rs****@portfoliorecovery.nospam.com> wrote
in message news:#v**************@tk2msftngp13.phx.gbl...
Could you PLEASE supply a little code for my example???

Thanks,

-Randy

"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:ei*************@TK2MSFTNGP09.phx.gbl...
If the rowstate of the rows isn't added/deleted etc,the call to your

update
statement is never made. If set up correctly, the adapter will walk

through
the table for you and handle all of this, automatically assigning the
parameters in your quereis to the values in the datatable.

If you are building it manually like below, you just need a valid Insert
command but that command must map to the datatable in order for you to

call
.Update and have it do anything.
"randy" <rs****@portfoliorecovery.nospam.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...
Thanks for you quick response. I went though the configuration wizdard
and
looked over the example from betav.com. I mostly understand the code, but I'm not making a connection between what I want/need to do and the

example.

Simple example of what I'm doing. First I build my datatable and then add
a
row to be inserted...

Dim dt as new datatable
Dim datCol as DataColumn
Dim DatRow as DataRow

datCol = new DataColumn("FName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datCol = new DataColumn("LName", System.Type.GetType("System.String"))
dt.Columns.Add(datCol)

datRow = dt.NewRow
datRow.Item("FName") = "Joe"
datRow.Item("LName") = "Smith"
dt.Rows.Add(datRow)

This is only one row, but I could have 500+ new rows. The "FName" and
"LName" are the same in my database table. Are you saying that I need to loop though the rows of the datatable and assign them Paramerters? Could please add some code below so I can better understand what I need
next?



"William Ryan eMVP" <do********@comcast.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
>
> "randy" <ra***@someplace.com> wrote in message
> news:uJ**************@TK2MSFTNGP09.phx.gbl...
> > Hello all,
> >
> > I have a DataTable which I am building column by column and adding

rows
> > after each new column. The DataTable columns match the columns in

my > > database table. I'm building the DataTable first and I then want to roll
> > through the DataTable while in memory checking for errors and then
commit
> > the rows to my database table (btw this is in ASP.NET). Is it

possible
to
> > have data in a datable before attaching at DataAdapter?
>
> Yes, and you never technically attach a datatable to an adapter. An
adapter
> could care less what you send to it, it only cares in that it matches the
> commands that you try to have it send the db.
> I'm a little new to
> > this and know I need a SqlCommandBuilder,
>
> Not so, and in all likelihood, avoid the command builder. Check out

Bill
> Vaughn's article Weaning Developer's from the commandBUilder at
> www.betav.com ->Articles ->MSDN
> so some code example would be very
> > helpful. Below is what I know about using the
DataAdapter/CommandBuilder,
> > but I don't need to SELECT anything because the DataTable already had new
> > rows (that why I put Id = 0 to get no data).
>
> If you use the command builder, you should be ok, but that query
will > probably cause drama. The commanduilder infers update/insert/delete

logic
> based on athe Select Command so that's all it needs. You don't need to > write over your exisitn gdata. You also probably want to use
Parameterized
> queries... Where ID = @SomeValue"
>
> then do mySelectCommand.Parameters.Add("@SomeValue", SqlDbType.Whatever, > someLength).Value = 0
>
> Run through the dataadapter configuration wizard at least once and check out
> the code it generates for you. It will have parameters and column
mappings
> as well. While I don't recommend using it too much b/c it becomes a
crutch,
> it's a superb learning too.
>
> Also, you don't have to call Select First. You don't even have to call > update on a datatable that you called fill on, or even one whose
data came
> from the db. The adapter doesn't care. I know this may seem like a lot so
> you may want to check out Bill's article, play with the wizard, and just get
> a valid Update command. Then do what you are now, and just call

update
on
> your datatable. Let me know if you have any problems, I'll do my

best to
> get you through them.
>
>
>
> HTH,
>
> Bill
>
> www.devbuzz.com
> www.knowdotnet.com
>
> >
> > dim dt as New DataTable
> > dim da as DataAdapter
> > dim sqlCmdBuild as SqlCommandBuilder
> >
> > da = New SqlDataAdapter("SELECT * FROM Table WHERE Id = 0",

OpenDbConn)
> > da..Fill(dt)
> >
> > sqlCmdBuild = New SqlCommandBuilder(da)
> >
> > TIA,
> >
> > -Randy
> >
> >
>
>



Jul 21 '05 #6

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

Similar topics

5
by: randy | last post by:
Hello all, I have a DataTable which I am building column by column and adding rows after each new column. The DataTable columns match the columns in my database table. I'm building the...
6
by: Danny Ni | last post by:
Hi, If I want to programatically add rows to a DataTable, do I call AcceptChanges per row? Or do I call AcceptChanges after all rows added? TIA
4
by: George | last post by:
Got a question about the side effect of DataAdapter.Update() and DataTable.GetChanges(). Say I set up a DataTable and a DataAdapter in a class. Delete (Not remove) a row in the data table and...
7
by: Varangian | last post by:
Hi all, the question I want to ask if the conversion of a DataReader to a Table looping through the DataReader is better than using the Fill Method of the DataAdapter... I'm asking because...
0
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: ...
7
by: =?Utf-8?B?QWxla3MgS2xleW4=?= | last post by:
I use in my asp.net code dataset and populate datatable using dataadapter. The problem is that my code demand huge amount of memory and I am looking for way reduce this demand. At this time I...
7
by: Kevin | last post by:
I'm new to ADO.NET--trying to make the switch from ADO, which I've been using in my VB2005 apps up until now. Here's what I have: Sub Save_Record() Dim OldRecord as DataTable Dim NewRecord as...
2
by: Ryan Liu | last post by:
Hi, If I have a very big view in database, it covers 15 tables, each table has 1000 columns. When I issue select * from view, the database will give error -- too many columns. Can I use a...
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.