Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding new row to dataset 1

Status
Not open for further replies.

goneWildCoder

Programmer
May 26, 2004
20
US
I have a textbox driven windows form created in C# and linked to a dataset. On the form, I have an add button that should allow the user to add a new record. How do I create a new record in the dataset which would be updated with values from the user?

I tried the following:


/// <summary>
/// Add a new record
/// </summary>
private void addBtn_Click(object sender, System.EventArgs e)
{
this.BindingContext[this.salesDs1,"Salesperson"].AddNew();
}

which causes this error:

Error: Index and length must refer to a location within the string
Parameter name: length

Please help!!
 
Hey there -

A couple of background items that might help:

1) I think that you're referring to a DataSet in place of a DataTable.
2) DataSets are used to manage DataTable objects
3) DataTable objects hold DataRow objects
4) Additionally, depending on what you're doing, you might not even need to employ a DataSet. DataSets are best used when managing DataTables in a disconnected editing environment.

Here's how you'd add a row to a DataTable that resides within a DataSet:

UsingSystem.Data

private void YourRoutineName()
{
//*****************************************
// you only do the section between
// the astericks once

// create a dataset
DataSet ds = new DataSet();

// create a new datatable
DataTable dt = new DataTable("myTable");

// add the datatable to the dataset
ds.Tables.Add(dt);
//*****************************************

// create a new datarow object from the datatable
// that resides within the dataset
// this method uses the ordinal to create the data row
DataRow dr = ds.Tables[0].NewRow();
// this method uses the table name to create the data row
//DataRow dr = ds.Tables["myTable"].NewRow();

// then, based on the schema of your data table (number and types of columns)
// you add the items to your data row object.
// if your table has FirstName, LastName, and Email fields, you'd do this:
dr["FirstName"] = txtFirstName.Text;
dr["LastName"] = txtLastName.Text;
dr["Email"] = txtEmail.Text;

// then add the new row to your datatable
ds.Tables[0].Rows.Add(dr);
}

I hope that helps.

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
sacheson,

Thanks for your reply on clearing up my concepts.

How do I move/ focus to the last row in the datatable?

 
are you binding to the textbox control? If so, I don't know that I can help you ... I generally shy [read: run] away from any type of data binding.

If you're looking to programatically get the last row from the table, all you should have to do is get the highest index from the DataTable.Rows collection. That returns a DataRow object, and you then retreive the data pretty much the same way you enter it.

I don't have time to test this right now, so I might have a syntax error in here, but you should be able to figure out any errors ...

// you get the index of the last row by passing
// in the total row count minus 1 (because Rows is
// a zero-based index collection
DataRow drLastRow = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1];

// return the data from the column name
drLastRow["FirstName"].ToString();
// you can do it ordinally as well
// drLastRow[0].ToString();
// I think there's another overload that you
// can pass in a column object ... shouldn't need
// to do that, though.

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top