How do I use DataBinding and SQLDataAdapter to Insert a record?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ranny

    How do I use DataBinding and SQLDataAdapter to Insert a record?

    Question: What is the process to Insert a record using Dataset and SQLDataAdapter?

    Explanation: Thanks to Cor I am able to get a record to load, modify it, and save it
    to the database. My current problem is one of my fields is NOT NULL and a primary
    key. I want to be able to open the form with the fields blank (new record).
    My current thinking is:

    'Create the SQL Connection, DataAdapter, and DataSet
    mySQLConnection = New SqlConnection(" server=myserver ;database=North wind;Uid=userid ;Pwd=userpwd;")
    mySLQDataAdapte r = New SqlDataAdapter( "select * from Employees", mySQLConnection )
    mySLQDataAdapte r.TableMappings .Add("Employees ", "Employees" )
    dim myDataSet as new DataSet
    myDataSet.ReadX MLSchema("North windEmployees.x sd")

    dim sqlCBuilder as SqlCommandBuild er = New SqlCommandBuild er(mySLQDataAda pter)
    mySLQDataAdapte r.DeleteCommand = sqlCBuilder.Get DeleteCommand()
    mySLQDataAdapte r.InsertCommand = sqlCBuilder.Get InsertCommand()
    mySLQDataAdapte r.UpdateCommand = sqlCBuilder.Get UpdateCommand()


    At this point, I believe I have connected to the SQL Server, created a DataAdapter
    with the sequel "select * from Employees", created an instance of a DataSet, and
    loaded the structure of the DataSet with a XML Schema file. I have not Filled the
    DataSet from the Adapter yet. If I try to Databind to the window forms controls
    I will get an error. (One of the controls is a label with the EmployeeID for display.

    txtEmployeeID.D ataBindings.Add (New Binding("Text", myDataSet, "Employees.Empl oyeeID"))
    txtLastName.Dat aBindings.Add(N ew Binding("Text", myDataSet, "Employees.Last Name"))
    txtFirstName.Da taBindings.Add( New Binding("Text", myDataSet, "Employees.Firs tName"))
    dtDOB.DataBindi ngs.Add(New Binding("Value" , myDataSet, "Employees.Birt hDate"))
    txtTitle.DataBi ndings.Add(New Binding("Text", myDataSet, "Employees.Titl eOfCourtesy"))


    I would like to have the form come up empty. The user would fill in the form. I
    would then call

    mySLQDataAdapte r.Update(myData Set,"Employees" )

    and the record would be added. I could then do something (?) to clear the
    form and they could add another record.

    Would someone set me straight on the correct sequence of events.

    Thanks
    Ranny



    User submitted from AEWNET (http://www.aewnet.com/)
  • Ken Tucker [MVP]

    #2
    Re: How do I use DataBinding and SQLDataAdapter to Insert a record?

    Hi,



    Ken
    ---------------------
    "Ranny" <Ranny@aew_nosp am.com> wrote in message
    news:%23K9ny0Gb FHA.3932@TK2MSF TNGP12.phx.gbl. ..
    Question: What is the process to Insert a record using Dataset and
    SQLDataAdapter?

    Explanation: Thanks to Cor I am able to get a record to load, modify it,
    and save it
    to the database. My current problem is one of my fields is NOT NULL and a
    primary
    key. I want to be able to open the form with the fields blank (new record).
    My current thinking is:

    'Create the SQL Connection, DataAdapter, and DataSet
    mySQLConnection = New
    SqlConnection(" server=myserver ;database=North wind;Uid=userid ;Pwd=userpwd;")
    mySLQDataAdapte r = New SqlDataAdapter( "select * from Employees",
    mySQLConnection )
    mySLQDataAdapte r.TableMappings .Add("Employees ", "Employees" )
    dim myDataSet as new DataSet
    myDataSet.ReadX MLSchema("North windEmployees.x sd")

    dim sqlCBuilder as SqlCommandBuild er = New
    SqlCommandBuild er(mySLQDataAda pter)
    mySLQDataAdapte r.DeleteCommand = sqlCBuilder.Get DeleteCommand()
    mySLQDataAdapte r.InsertCommand = sqlCBuilder.Get InsertCommand()
    mySLQDataAdapte r.UpdateCommand = sqlCBuilder.Get UpdateCommand()


    At this point, I believe I have connected to the SQL Server, created a
    DataAdapter
    with the sequel "select * from Employees", created an instance of a DataSet,
    and
    loaded the structure of the DataSet with a XML Schema file. I have not
    Filled the
    DataSet from the Adapter yet. If I try to Databind to the window forms
    controls
    I will get an error. (One of the controls is a label with the EmployeeID
    for display.

    txtEmployeeID.D ataBindings.Add (New Binding("Text", myDataSet,
    "Employees.Empl oyeeID"))
    txtLastName.Dat aBindings.Add(N ew Binding("Text", myDataSet,
    "Employees.Last Name"))
    txtFirstName.Da taBindings.Add( New Binding("Text", myDataSet,
    "Employees.Firs tName"))
    dtDOB.DataBindi ngs.Add(New Binding("Value" , myDataSet,
    "Employees.Birt hDate"))
    txtTitle.DataBi ndings.Add(New Binding("Text", myDataSet,
    "Employees.Titl eOfCourtesy"))


    I would like to have the form come up empty. The user would fill in the
    form. I
    would then call

    mySLQDataAdapte r.Update(myData Set,"Employees" )

    and the record would be added. I could then do something (?) to clear the
    form and they could add another record.

    Would someone set me straight on the correct sequence of events.

    Thanks
    Ranny



    User submitted from AEWNET (http://www.aewnet.com/)


    Comment

    Working...