Insert datatable record into database table

T

Thila

I'm new in C# and below is the code that I use to create a temporary table
to insert multiple record. I want all the record inserted into the temporary
datatable to be save/insert into database when I press the save button. I
cound't find any sample on it.

private void btnAddAddress_Click(object sender, System.EventArgs e)

{

if (isCreateAdd != true)

{

myConn.Close();

AddDataSet = new DataSet("AddDSet");

AddTable = new DataTable("TempAddress");

AddDataSet.Tables.Add(AddTable);

DataColumn AddCol = AddTable.Columns.Add("TempAddressKey", typeof(Int32));

AddCol.AllowDBNull = false;

AddCol.AutoIncrement = true;

AddCol.AutoIncrementSeed = 1;


AddTable.Columns.Add("AddressTypeKey", typeof(Int32));

AddTable.Columns.Add("Street", typeof(string));

AddTable.Columns.Add("Town", typeof(string));

AddTable.Columns.Add("Telephone", typeof(string));

AddTable.Columns.Add("TelExt", typeof(string));

AddTable.Columns.Add("Email", typeof(string));

AddTable.Columns.Add("FaxNo", typeof(string));

AddTable.Columns.Add("PostcodeKey", typeof(Int32));

AddTable.Columns.Add("CountryKey", typeof(Int32));

AddTable.Columns.Add("StateKey", typeof(Int32));

AddTable.PrimaryKey = new DataColumn[] {AddTable.Columns["TempAddressKey"]};

isCreateAdd = true;

AddRow();

}

else

{

AddRow();

}

}


private void AddRow()

{

DataRow myNewRow;

int index;

for(index=0; index < AddTypeKey; index++)

{

myNewRow = AddTable.NewRow();

myNewRow["AddressTypeKey"] = cboAddressType.SelectedValue.ToString();

myNewRow["Street"] = txtBoxStreet.Text;

myNewRow["Town"] = txtBoxTown.Text;

myNewRow["Telephone"] = txtTelephone.Text;

myNewRow["TelExt"] = txtTelExt.Text;

myNewRow["Email"] = txtEmail.Text;

myNewRow["FaxNo"] = txtFaxNo.Text;

myNewRow["PostcodeKey"] = txtBoxPostcodeKey.Text;

myNewRow["CountryKey"] = cboCountry.SelectedValue.ToString();

myNewRow["StateKey"] = cboState.SelectedValue.ToString();

AddTable.Rows.Add(myNewRow);

}

dataGrid1.DataSource = AddTable;

dataGrid1.Visible = true;

}

Can anyone give me a solution for my problem?

Thank you
Thila
 
Top