By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

How to push data from datatable to database

P: n/a
Hello

I want to push the data in my datatable into my database. How must i do this? I have been told to create a insert query. I have done this and have a select statement below it to retrieve the Identity value
What i want to ask is how i add my insertCommand to the dataadapter

I keep getting a error when i click my 'Submit' button..
Update requires a valid InsertCommand when passed DataRow collection with new rows

Im Sure my coding is wrong cos i was trying to play around to see if i could fluke it :o

This is my coding..

Private Sub btnConfirmBooking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmBooking.Clic

Dim cmdInsRenter As New SqlCommand("InsRenterAndSelIdentity", cn
cmdInsRenter.CommandType = CommandType.StoredProcedur

'declare the parameter
cmdInsRenter.Parameters.Add("@Lname", SqlDbType.Char, 50
cmdInsRenter.Parameters.Add("@Fname", SqlDbType.Char, 50
cmdInsRenter.Parameters.Add("@Addr", SqlDbType.Char, 300
cmdInsRenter.Parameters.Add("@RenterPostcode", SqlDbType.Char, 50
cmdInsRenter.Parameters.Add("@RenterPhoneNo", SqlDbType.Char, 25

'now set the value
cmdInsRenter.Parameters("@Lname").Value = txtLname.Tex
cmdInsRenter.Parameters("@Fname").Value = txtFname.Tex
cmdInsRenter.Parameters("@Addr").Value = txtAddr.Tex
cmdInsRenter.Parameters("@RenterPostcode").Value = txtRenterPcode.Tex
cmdInsRenter.Parameters("@RenterPhoneNo").Value = txtRenterPhoneNo.Tex

Tr
cn.Open(
daRenter.Update(Mydataset.Tables("MyDataTable")
cmdInsRenter.ExecuteNonQuery(

Catch x As Exceptio
MsgBox(x.Message
Finall
cn.Close(
End Tr
End Su

plz can someone help to modify my coding
Thank u in advance

Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Cor
Hi Bhavna,

The dataAdapter update needs
a Select command
a Update command
a Delete command
a Insert command

Did you use the IDE to make your dataadapter or did you do it by hand?

It is difficult to answer you now.

Cor
Jul 21 '05 #2

P: n/a
Hello Cor,

I created the dataadapter by hand. I did not use the IDE.
The only thing i wrote for it was its declaration.

Private daRenter As SqlDataAdapter

Is there more i need to do?
Jul 21 '05 #3

P: n/a
Cor
> I created the dataadapter by hand. I did not use the IDE.
The only thing i wrote for it was its declaration.

Private daRenter As SqlDataAdapter

Is there more i need to do?


A lot or maybe not.

First you have to decide if you use a dataadpater or a single insert.
But let supose you choise for the datadapter.

I tell you only some higlights, you have to do some search for yourself I, I
never did those things with a stored procedure, I asume that your stored
procedure is a select.
Dim cmdInsRenter As New SqlCommand("InsRenterAndSelIdentity", cn)
cmdInsRenter.CommandType = CommandType.StoredProcedure

'declare the parameters
cmdInsRenter.Parameters.Add("@Lname", SqlDbType.Char, 50)
cmdInsRenter.Parameters.Add("@Fname", SqlDbType.Char, 50)
cmdInsRenter.Parameters.Add("@Addr", SqlDbType.Char, 300)
cmdInsRenter.Parameters.Add("@RenterPostcode", SqlDbType.Char, 50)
cmdInsRenter.Parameters.Add("@RenterPhoneNo", SqlDbType.Char, 25)

You do not have to add the values to the dataadapter but to the datatable if
you use a dataadapter and you can (if you do not want to write them by hand)
use the commanbuilder to make the insert, delete and update from to your
dataadapter.

But first add the command to the dataadapter, I think you did not do it
seeing your code below.
daRenter = new SQLdataadapter(cmdInsRenter)

I do not know if it will go with a stored procedure but you can try,
otherwise I would first skip the stored procedure and use a normal select
and than change the program back when it works to with a stored procedures

cmd = new SQLcommandbuilder(daRenter)

Try
--------------------
cn.Open()
You can do it, but the datadapter does it also for you
--------------------
daRenter.Update(Mydataset.Tables("MyDataTable"))
-----------------------------------------
cmdInsRenter.ExecuteNonQuery()
this one above you do not need with the dataadapter, you have connected the
command to it as I did above.
---------------------------------------

Catch x As Exception
MsgBox(x.Message)
Finally
cn.Close()
End Try
End Sub

I hope this brings you a little bit on the route?

Cor
Jul 21 '05 #4

P: n/a
When u say that u assume my SPROC is a select, do u mean that it is a Select statment
My SPROC i created is a Insert query that i use to push the added rows in my datatable to my database

CREATE PROCEDURE dbo.InsRentAndSelIdentit

@Lname char(50)
@Fname char(50)
@Address varchar(300)
@Postcode varchar(50)
@PhoneNo varchar(25

A
SET NOCOUNT ON

INSERT INTO Rent(Lname, Fname, Addr, RentPostcode, RentPhoneNo) VALUES (@Lname, @Fname, @Address, @Postcode, @PhoneNo)
SELECT Lname, Fname, Addr, RentPostcode, RentPhoneNo, RentID FROM Rent WHERE (RentID = @@IDENTITY)

RETURN

Is the the easiest way to do this

I also do not know what u meant by the following statemnet..
"You do not have to add the values to the dataadapter but to the datatable if
you use a dataadapter and you can (if you do not want to write them by hand
use the commanbuilder to make the insert, delete and update from to you
dataadapter.

What do i add to the datatable instead if the Dataadapter

And are u saying that if the stored procedure does not work i should use the ide to create all the commands via the commandBuilder
Is there no way to do it manually by code

I added the one line u suggested to my code but it still states that i need to create a valid insert command!

This is the code i tried

Private Sub btnConfirmBooking_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmBooking.Clic

Dim cmdInsRenter As New SqlCommand("InsRenterAndSelIdentity", cn
cmdInsRenter.CommandType = CommandType.StoredProcedur

'declare the parameter
cmdInsRenter.Parameters.Add("@Lname", SqlDbType.Char, 50
cmdInsRenter.Parameters.Add("@Fname", SqlDbType.Char, 50
cmdInsRenter.Parameters.Add("@Addr", SqlDbType.Char, 300
cmdInsRenter.Parameters.Add("@RenterPostcode", SqlDbType.Char, 50
cmdInsRenter.Parameters.Add("@RenterPhoneNo", SqlDbType.Char, 25

'now set the value
cmdInsRenter.Parameters("@Lname").Value = txtLname.Tex
cmdInsRenter.Parameters("@Fname").Value = txtFname.Tex
cmdInsRenter.Parameters("@Addr").Value = txtAddr.Tex
cmdInsRenter.Parameters("@RenterPostcode").Value = txtRenterPcode.Tex
cmdInsRenter.Parameters("@RenterPhoneNo").Value = txtRenterPhoneNo.Tex

daRenter = new SQLdataadapter(cmdInsRenter

Tr
cn.Open(
daRenter.Update(Mydataset.Tables("MyDataTable")

Catch x As Exceptio
MsgBox(x.Message
Finall
cn.Close(
End Tr
End Su


Jul 21 '05 #5

P: n/a
Cor
Hi Bhavna,

Can you show me with a link to MSDN where you got this code?
,


'now set the values
cmdInsRenter.Parameters("@Lname").Value = txtLname.Text
cmdInsRenter.Parameters("@Fname").Value = txtFname.Text
cmdInsRenter.Parameters("@Addr").Value = txtAddr.Text
cmdInsRenter.Parameters("@RenterPostcode").Value = txtRenterPcode.Text cmdInsRenter.Parameters("@RenterPhoneNo").Value =

txtRenterPhoneNo.Text

To update a dataset?

I never saw it

Cor
Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.