Re: Can't bind text box to database

Discussion in 'Microsoft VB .NET' started by Cor Ligthert[MVP], Feb 23, 2009.

  1. 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/Forums.aspx/dotnet-vb-net/200902/1
    >
     
    Cor Ligthert[MVP], Feb 23, 2009
    #1
    1. Advertisements

  2. 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
     
    Cor Ligthert[MVP], Feb 23, 2009
    #2
    1. Advertisements

  3. 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/Forums.aspx/dotnet-vb-net/200902/1
    >
     
    Cor Ligthert[MVP], Feb 23, 2009
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. toke
    Replies:
    1
    Views:
    572
    Ken Tucker [MVP]
    Feb 20, 2004
  2. Lori Markle via .NET 247

    Cannot bind a simple text box

    Lori Markle via .NET 247, May 7, 2004, in forum: Microsoft VB .NET
    Replies:
    3
    Views:
    218
    William Ryan eMVP
    May 7, 2004
  3. A_PK
    Replies:
    17
    Views:
    838
  4. Monty
    Replies:
    12
    Views:
    500
    Cor Ligthert [MVP]
    Apr 4, 2006
  5. Dick Sutton
    Replies:
    3
    Views:
    365
    Dick Sutton
    May 23, 2006
Loading...

Share This Page