PC Review


Reply
Thread Tools Rate Thread

Data is not inserting inn database

 
 
Pushpendra Vats
Guest
Posts: n/a
 
      7th Apr 2004
Hi ,
I am trying to insert records into database. I am trying
to write the following code.
On button1 click event i am inserting five records and
after that i am calling the update method of dataadapter
to update the records in database.
Records are inserting but all the five records have the
same value and that too of that last ones..
More over i tried to use the acceptchanges methods for
datatable and dataset but of no use
ihave used the following code

Dim da As New OleDb.OleDbDataAdapter("select * from
test", "Provider=SQLOLEDB;Data Source=192.168.16.4;Initial
Catalog=Northwind;user id=sa;password=;")
Dim myConnection As New OleDb.OleDbConnection
("Provider=SQLOLEDB;Data Source=192.168.16.4;Initial
Catalog=Northwind;user id=sa;password=;")
Dim dt As DataTable
Dim dr As DataRow
Dim ds As New DataSet()

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "test")


End Sub

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
dt = ds.Tables("test")
da.InsertCommand = New OleDb.OleDbCommand("insert
test values('" + TextBox1.Text + "','" + TextBox2.Text
+ "')", myConnection)


Dim i As Integer
dr = dt.NewRow()
dr("name") = TextBox1.Text
dr("address") = TextBox2.Text

dt.Rows.Add(dr)
MsgBox(dt.Rows.Count())
End Sub

Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
Try

da.Update(ds, "test")
MsgBox("Record saved")
Catch er As Exception
MsgBox(er.ToString)
End Try
End Sub
 
Reply With Quote
 
 
 
 
Cor
Guest
Posts: n/a
 
      7th Apr 2004
"Hi Pushpendra Vats

I deleted some things and added some things, that commandbuilder is not the
nicest however that insert command you was using also not.

Give it a try?

It is just changed in the text so watch typos or other errors.

Cor


"> Dim da As New OleDb.OleDbDataAdapter("select * from
> test", "Provider=SQLOLEDB;Data Source=192.168.16.4;Initial
> Catalog=Northwind;user id=sa;password=;")
> Dim myConnection As New OleDb.OleDbConnection
> ("Provider=SQLOLEDB;Data Source=192.168.16.4;Initial
> Catalog=Northwind;user id=sa;password=;")

Dim ds As New DataSet()
> Private Sub Form1_Load(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles MyBase.Load
> da.Fill(ds, "test")
> End Sub
> Private Sub Button1_Click(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> Button1.Click
> dt = ds.Tables("test")
> dim dr as datarow = dt.NewRow()
> dr("name") = TextBox1.Text
> dr("address") = TextBox2.Text
> dt.Rows.Add(dr)
> MsgBox(dt.Rows.Count())
> End Sub
> Private Sub Button2_Click(ByVal sender As
> System.Object, ByVal e As System.EventArgs) Handles
> Button2.Click
> Try

dim cmb as new OleDb.OleDbcommandbuilder(da)
> da.Update(ds, "test")
> MsgBox("Record saved")
> Catch er As Exception
> MsgBox(er.ToString)
> End Try
> End Sub



 
Reply With Quote
 
pushpendra vats
Guest
Posts: n/a
 
      8th Apr 2004
Dear Cor,
Thanks a lot fro the solution but i really could not
understand why the same is not working with the
insertcommand.

One more thing if i use the acceptchanges method with
datatablenad dataset still it was not working....

Thanks a lot....again....

pushpendra
>-----Original Message-----
>"Hi Pushpendra Vats
>
>I deleted some things and added some things, that

commandbuilder is not the
>nicest however that insert command you was using also not.
>
>Give it a try?
>
>It is just changed in the text so watch typos or other

errors.
>
>Cor
>
>
>"> Dim da As New OleDb.OleDbDataAdapter("select * from
>> test", "Provider=SQLOLEDB;Data

Source=192.168.16.4;Initial
>> Catalog=Northwind;user id=sa;password=;")
>> Dim myConnection As New OleDb.OleDbConnection
>> ("Provider=SQLOLEDB;Data Source=192.168.16.4;Initial
>> Catalog=Northwind;user id=sa;password=;")

> Dim ds As New DataSet()
>> Private Sub Form1_Load(ByVal sender As System.Object,
>> ByVal e As System.EventArgs) Handles MyBase.Load
>> da.Fill(ds, "test")
>> End Sub
>> Private Sub Button1_Click(ByVal sender As
>> System.Object, ByVal e As System.EventArgs) Handles
>> Button1.Click
>> dt = ds.Tables("test")
>> dim dr as datarow = dt.NewRow()
>> dr("name") = TextBox1.Text
>> dr("address") = TextBox2.Text
>> dt.Rows.Add(dr)
>> MsgBox(dt.Rows.Count())
>> End Sub
>> Private Sub Button2_Click(ByVal sender As
>> System.Object, ByVal e As System.EventArgs) Handles
>> Button2.Click
>> Try

> dim cmb as new OleDb.OleDbcommandbuilder(da)
>> da.Update(ds, "test")
>> MsgBox("Record saved")
>> Catch er As Exception
>> MsgBox(er.ToString)
>> End Try
>> End Sub

>
>
>.
>

 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      8th Apr 2004
Hi Pushpendra,

Maybe the insert does work also, however why do more work than needed.

The acceptchanges is to prevent updating.

It stands for Accept the changes as if you did the updating.

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting data to MS SQL Database Etayki Microsoft VB .NET 2 18th Apr 2007 02:46 AM
Problems inserting data into Access database Roshawn Dawson Microsoft ADO .NET 2 31st Mar 2005 05:40 PM
Data truncation inserting into an Oracle8.1.7 database =?Utf-8?B?R2FyeSBNaWxsZXI=?= Microsoft C# .NET 2 27th Aug 2004 05:24 PM
inserting data into a database dana livni Microsoft ASP .NET 4 20th Mar 2004 06:24 PM
inserting data into SQL Database djozy Microsoft C# .NET 1 4th Dec 2003 01:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:15 AM.