PC Review


Reply
Thread Tools Rate Thread

HOWTO: Insert New Row

 
 
Greif
Guest
Posts: n/a
 
      28th Oct 2003
I want to write a funciton that has 2 parameters(string
tableName and IDbDataParameter[] params). I can't seem to
figure out how to create a new record using a combination
of a Dataset/DataTable/DataRow. Or is there a better way to
insert rows?
 
Reply With Quote
 
 
 
 
Ravikanth[MVP]
Guest
Posts: n/a
 
      28th Oct 2003
Hi

Check the following article
http://www.superdotnet.com/Article.aspx?ArticleID=149

HTH
Ravikanth[MVP]


>-----Original Message-----
>I want to write a funciton that has 2 parameters(string
>tableName and IDbDataParameter[] params). I can't seem to
>figure out how to create a new record using a

combination
>of a Dataset/DataTable/DataRow. Or is there a better way

to
>insert rows?
>.
>

 
Reply With Quote
 
 
 
 
Robert Morris
Guest
Posts: n/a
 
      28th Oct 2003
Are you trying to populate a DataTable/DataSet with the
parameters from the method?

if so, has the table been created at the point you
describe below?

- Rob

>-----Original Message-----
>I want to write a funciton that has 2 parameters(string
>tableName and IDbDataParameter[] params). I can't seem to
>figure out how to create a new record using a combination
>of a Dataset/DataTable/DataRow. Or is there a better way

to
>insert rows?
>.
>

 
Reply With Quote
 
Guest
Posts: n/a
 
      28th Oct 2003
Nothing has been created. This would be a static method to
insert a row via a disconnected db connection.

>-----Original Message-----
>Are you trying to populate a DataTable/DataSet with the
>parameters from the method?
>
>if so, has the table been created at the point you
>describe below?
>
>- Rob

 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      30th Oct 2003
Hi Greif,
Rough written

A dataset without a primary key
dataset.table(0).rows.add(dataset.table(0).newrow)

With keys that has to be filled in advance, you first have to make a
datarow, fill the keys and than add the newrow

dr=dataset.table(0).newrow
dr("keyfield")=keyobject(0)
dataset.table(0).rows.add(dr)

I hope this helps a little bit?

Cor


 
Reply With Quote
 
Greif
Guest
Posts: n/a
 
      30th Oct 2003
The hard part of this was trying to keep it abstract. I had
to write some abstract functions to create the DataAdapter
and the InsertCommand (using the data provider's
commandbuilder). But it works. This is what I came up with.

public virtual int singleInsert2(string tableName, params
IDbDataParameter[] values)
{
if(ConnectionString == "" || ConnectionString.Length == 0)
throw new InvalidOperationException("The connection
string is not set. Set the 'ConnectionString' member.");

DataSet ds = new DataSet(tableName);
IDbDataAdapter dataAdapter = GetDataAdapter(tableName,
ConnectionString);
dataAdapter.InsertCommand = GetInsertCommand(dataAdapter);
dataAdapter.FillSchema(ds, SchemaType.Source);
DataRow newRow = ds.Tables["Table"].NewRow();

foreach(IDbDataParameter param in values)
{
newRow[param.ParameterName] = param.Value;
}

ds.Tables["Table"].Rows.Add(newRow);
return dataAdapter.Update(ds);
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to create a "Insert Row" button to insert a new row at the. =?Utf-8?B?R2Vvcmdlcw==?= Microsoft Excel Programming 1 16th Mar 2005 03:57 AM
Find a value, then insert a row below that row and copy the contents to the new row stelllar Microsoft Excel Programming 4 15th Feb 2005 01:29 PM
Can't Insert New Row Between Row and Row While Existing DataTable Looping Kelvin Microsoft C# .NET 0 12th Dec 2004 01:22 PM
Can't Insert New Row Between Row and Row While Existing DataTable Looping Kelvin Microsoft C# .NET 1 11th Dec 2004 01:43 PM
Inserting a row in sheet A should Insert a row in sheet B, removing a row in Sheet A should remove the corresponding row in sheet B Hannes Heckner Microsoft Excel Programming 1 5th Mar 2004 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:53 AM.