dataset will not save to database

db Ireland
  • 11 years ago
    Edit: If I set the value of the field hardcoded it still does not save and haschanges is still false:

    e.g
    m_ds.Tables("tblScripts").Rows(0).Item("Script_Desc") = "HELP"

    Hi,

    Please someone help me I'm at my wits end.  My texbox populates with the data.  I can edit it and the dataset gets updated.....but the data will not save to the database!  dataset.haschanges = false.  But if I check the dataset my changes are there....very frustrating Angry [:@]

    I can confirm that the stored procs work fine.  If I bind the same field to a grid column it saves, with exact same CreateDS and UpdateDS methods.

    Please help.

    The code is as follows:

    (There is a form with a textbox and a button)


    Imports System.Data.SqlClient

    Public Class Form1
        Dim m_cnn As SqlConnection
        Dim m_ds As DataSet
        Dim m_da As SqlDataAdapter
        Dim m_cmdScriptsSEL As SqlCommand
        Dim m_cmdScriptsINS As SqlCommand
        Dim m_cmdScriptsUP As SqlCommand
        Dim m_bnd As Binding

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            m_cnn = New SqlConnection("Data Source=LAPTOP;Initial Catalog=dbScriptManager;" & _
            "Integrated Security=True")
            CreateDS()
            bindBox()
        End Sub

        Private Sub CreateDS()
            m_cmdScriptsSEL = New SqlCommand
            With m_cmdScriptsSEL
                .CommandType = CommandType.StoredProcedure
                .Connection = m_cnn
                .CommandText = "sp_ScriptsSEL"
                .Parameters.Add("@ScriptID", SqlDbType.BigInt, 0, "ScriptID")
                .Parameters("@ScriptID").Value = 1
            End With

            m_cmdScriptsINS = New SqlCommand
            With m_cmdScriptsINS
                .CommandType = CommandType.StoredProcedure
                .Connection = m_cnn
                .CommandText = "sp_ScriptsINS"
                .Parameters.Add("@Script_Desc", SqlDbType.VarChar, 50, "Script_Desc")
                .Parameters.Add("@Script_Txt", SqlDbType.Text, 255, "Script_Txt")
                .Parameters.Add("@NewScriptID", SqlDbType.BigInt, 0, "ScriptID")
                .Parameters.Item("@NewScriptID").Direction = ParameterDirection.Output
            End With

            m_cmdScriptsUP = New SqlCommand
            With m_cmdScriptsUP
                .CommandType = CommandType.StoredProcedure
                .Connection = m_cnn
                .CommandText = "sp_ScriptsUP"
                .Parameters.Add("@ScriptID", SqlDbType.BigInt, 0, "ScriptID")
                .Parameters.Add("@Script_Desc", SqlDbType.VarChar, 50, "Script_Desc")
                .Parameters.Add("@Script_Txt", SqlDbType.Text, 0, "Script_Txt")
            End With

            m_da = New SqlDataAdapter(m_cmdScriptsSEL))

            m_ds = New DataSet
            With m_da
                .UpdateCommand = m_cmdScriptsUP
                .InsertCommand = m_cmdScriptsINS
                .Fill(m_ds, "tblScripts")
            End With
        End Sub

        Private Sub bindBox()
            m_bnd = New Binding("Text", m_ds, "tblScripts.Script_Desc", True, DataSourceUpdateMode.OnPropertyChanged)
            TextBox1.DataBindings.Add(m_bnd)
        End Sub

        Private Sub UpdateDS()
            If m_ds.HasChanges Then
                m_da.Update(m_ds, "tblScripts")
            End If
        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            UpdateDS()
        End Sub
    End Class































































































  • 11 years ago
    If I check the parameters of m_da.updatecommand they are all null.  Do I need to do anything special when binding a textbox to a dataset and using stored procs in the dataadapter?

  • 11 years ago
    Problem solved.  You need to call the EndEdit of the datarow Stick out tongue [:P]

    i.e m_ds.Tables("tblScripts").Rows(0).EndEdit()



Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe