How to push data from datatable to database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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?
 
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
 
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
 
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
 
Back
Top