PC Review


Reply
Thread Tools Rate Thread

Re: Can't bind text box to database

 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      23rd Feb 2009
Hi,

Try to use the load event of the main form, this fires as every initializing
is done but before that the form is showed.

Be aware that with Net you cannot bind a textbox to a database. By instance
your dataset is disconnected so you have to do everything yourself.
In the way you do it, you have to clean everytime your dataset and to refill
again. A little bit expensive in used time.

In my idea you can better simple add an update button to your form.

But first a simple question, you make everything Public, do you have a
reason for that?

Cor

>
> Public Class Form3
>
> Public dbConn As New OleDbConnection(ConnectionString)
> Public queryString As String = "select * from Info where ClientNum =
> '123' ;"
> Public ds As New DataSet
> Public da As New OleDbDataAdapter(queryString, dbConn)
> Public cb As New OleDbCommandBuilder(da)
>
>
> Public Sub New()
> InitializeComponent()
>
> da.UpdateCommand = cb.GetUpdateCommand
> da.DeleteCommand = cb.GetDeleteCommand
> da.InsertCommand = cb.GetInsertCommand
> da.Fill(ds, "info")
>
> TextBox1.DataBindings.Clear()
> TextBox1.DataBindings.Add("text", ds, "info.namelast")
>
> End Sub
>
> Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.
> EventArgs) Handles TextBox1.Validated
>
> TextBox1.BindingContext(ds, "info").EndCurrentEdit()
> MsgBox(ds.Tables(0).Rows(0).Item("namelast").ToString())
> MsgBox(ds.HasChanges())
> da.Update(ds, "info")
>
> End Sub
> End Class
>
> I change the text in the text box and then move focus somewhere else. At
> that
> point, the first message box returns the modified text. The second message
> box returns a "True". I then receive a "No value given for one or more
> required parameters." unhandled OleDbException error at the da.Update
> line. I
> guess the error stems from having only one row in the dataset??
>
> Thanks for any help,
> Bob
>
> --
> Message posted via DotNetMonster.com
> http://www.dotnetmonster.com/Uwe/For...b-net/200902/1
>


 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      23rd Feb 2009
Tiki,

What I mostly do as I use the commandbuilder is like this.
>
> Imports System.Data.OleDb
> Public Class Form3
> Private ds As New DataSet
> Private da As OleDbDataAdapter(queryString, dbConn)
> Public Sub New()
> InitializeComponent()
>
> End Sub
>
> Private Sub Form3_Load(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Handles Me.Load


Dim dbConn As New OleDbConnection(ConnectionString)
Dim queryString As String = "select * from Info where ClientNum = '123'
"
da = New OleDbDataAdapter(queryString, dbConn)
dim cb As OleDbCommandBuilder(da)

'da.UpdateCommand = cb.GetUpdateCommand (You don't need these 3)
'da.DeleteCommand = cb.GetDeleteCommand
'da.InsertCommand = cb.GetInsertCommand
> da.Fill(ds, "info")

cb = new OleDBCommandBuilder(da)
> TextBox1.DataBindings.Clear()
> TextBox1.DataBindings.Add("text", ds, "info.address")
> End Sub
>
> Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.
> EventArgs) Handles Button3.Click
>
> Me.BindingContext(Me.ds, "info").EndCurrentEdit()
> MsgBox(ds.HasChanges())

Me.da.Update(ds, "info")
'Me.ds.AcceptChanges() (only the changed rows are updated, and in
the update set as unchanged (status=new)
>
> End Sub
>


Cor

 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      23rd Feb 2009
Hi,

I have done this (it is using SQL because I have not Access database
anymore, but in fact it is the same as you read for Sql Oledb)

It did work perfectly

\\\
Imports System.Data.SqlClient
Public Class Form1
Private da As SqlDataAdapter
Private ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object _
, ByVal e As System.EventArgs) Handles
MyBase.Load
da = New SqlDataAdapter("Select * from Employees where EmployeeID =
'1'" _
, "Data Source=MyServer;Initial
Catalog=Northwind;Integrated Security=True")
da.Fill(ds, "Employees")
TextBox1.DataBindings.Add("text", ds, "Employees.LastName")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.BindingContext(ds, "Employees").EndCurrentEdit()
Dim cb = New SqlCommandBuilder(da)
Me.da.Update(ds, "Employees")
End Sub
End Class
///
Cor


"tiki99 via DotNetMonster.com" <u42576@uwe> wrote in message
news:9224e8ad2aca7@uwe...
> Thanks Cor.
>
> I moved the code you supplied into the button_click event as follows:
>
> Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.
> EventArgs) Handles Button3.Click
>
> Me.BindingContext(Me.ds, "info").EndCurrentEdit()
>
> Dim cb As OleDbCommandBuilder
> cb = New OleDbCommandBuilder(da)
>
> MsgBox(ds.HasChanges())
> Me.da.Update(Me.ds, "info")
> Me.ds.AcceptChanges()
>
> End Sub
>
> It again showed that the dataset had changed, but again gave me the same
> error. Am I trying to do something that can't be done? In other words, I'm
> filling the textbox with data from the db. The user would then manually
> change the text in that texbox and click the update button. The new (text)
> information would then be saved(updated) to the database. I know how to
> manually insert or update an entire record into a database, I just thought
> this would be easier on a single field by field basis. I'm obviously
> missing
> some point here.
>
> If you have any other suggestions, I'd like to try them.
>
> thanks,
> Bob
>
> Cor Ligthert[MVP] wrote:
>>Tiki,
>>
>>What I mostly do as I use the commandbuilder is like this.
>>
>>> Imports System.Data.OleDb
>>> Public Class Form3

>>[quoted text clipped - 8 lines]
>>> System.EventArgs)
>>> Handles Me.Load

>>
>> Dim dbConn As New OleDbConnection(ConnectionString)
>> Dim queryString As String = "select * from Info where ClientNum =
>> '123'
>>"
>> da = New OleDbDataAdapter(queryString, dbConn)
>> dim cb As OleDbCommandBuilder(da)
>>
>> 'da.UpdateCommand = cb.GetUpdateCommand (You don't need these 3)
>> 'da.DeleteCommand = cb.GetDeleteCommand
>> 'da.InsertCommand = cb.GetInsertCommand
>>> da.Fill(ds, "info")

>> cb = new OleDBCommandBuilder(da)
>>> TextBox1.DataBindings.Clear()
>>> TextBox1.DataBindings.Add("text", ds, "info.address")

>>[quoted text clipped - 5 lines]
>>> Me.BindingContext(Me.ds, "info").EndCurrentEdit()
>>> MsgBox(ds.HasChanges())

>> Me.da.Update(ds, "info")
>> 'Me.ds.AcceptChanges() (only the changed rows are updated, and in
>>the update set as unchanged (status=new)
>>
>>> End Sub

>>
>>Cor

>
> --
> Message posted via DotNetMonster.com
> http://www.dotnetmonster.com/Uwe/For...b-net/200902/1
>


 
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
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 Microsoft ASP .NET 1 2nd Oct 2005 06:07 PM
to bind or not to bind that is the question? Wiredless Microsoft Dot NET Compact Framework 2 2nd Jun 2005 10:00 PM
DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP A_PK Microsoft Dot NET Compact Framework 16 13th Apr 2005 04:27 AM
DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP A_PK Microsoft VB .NET 17 13th Apr 2005 04:27 AM
Re: Bind support win2k - Questions about 2000 & Bind Kenneth Porter Microsoft Windows 2000 DNS 9 13th Jul 2003 06:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:28 AM.