storing table from dataset to database table

nitin

Member
Joined
Mar 29, 2005
Messages
9
Programming Experience
Beginner
' Create a new DataTable.

Dim myDataTable As DataTable = New DataTable("temp")

' Declare variables for DataColumn and DataRow objects.

Dim myDataColumn As DataColumn

Dim myDataRow As DataRow

Public myDataset As DataSet


Private Sub createtable()
' Create new DataColumn, set DataType, ColumnName and add to DataTable.

myDataColumn = New DataColumn()

myDataColumn.DataType = System.Type.GetType("System.String")

myDataColumn.ColumnName = "Name"

myDataColumn.ReadOnly =
True

myDataColumn.Unique = True

' Add the Column to the DataColumnCollection.

myDataTable.Columns.Add(myDataColumn)
End sub

Private Sub addrecord()



myDataRow = myDataTable.NewRow()

myDataRow("Name") = pname.Text

myDataRow("Qty") = pqty.Text

myDataRow("Price") = pprice.Text

myDataTable.Rows.Add(myDataRow)

End Sub


i first created a datatable set then a dataset then i add rows using addrow method.Now i want this temp table frm dataset to be stored in database????



how will i do that.pls
 
Hi,
as u have added New row in Datatable,Use Dataadpter to update ur Database.......
I assume u have "temp" table in ur DB where u want to insert data....

(While creating ur dataTable u haven't add column Qty and Price??)


Dim da As SqlDataAdapter = New SqlDataAdapter()
Dim cmd As SqlCommand
' Create the SelectCommand.

' Create the InsertCommand.
cmd = New SqlCommand("INSERT INTO temp(Name, Qty,Price) " & _
"VALUES (@Name, @Qty,@Price)", conn)
cmd.Parameters.Add("@Name", SqlDbType.NChar, 50, "Name")
cmd.Parameters.Add("@Qty", SqlDbType.Decimal, 8, "Qty")
cmd.Parameters.Add("@Price", SqlDbType.Decimal, 8, "Price")
da.InsertCommand = cmd

da.Update(myDataTable)


I hope this will help u..................

Regards,
Ritesh
 
Back
Top