Solved

Problem saving Data to database using SQL Server and VB.Net

Posted on 2006-06-20
18
215 Views
Last Modified: 2010-04-23
Can you tell me what is wrong with this and correct it??

It tells me somthing about an update command missing on the update command....

    Public Function Save()

        Dim oRow As Data.DataRow
        Dim oDb As New clsDBAccess(mvarServer, mvarDatabase)
        Dim strSQL As String = "Select * from tblReports Where ReportID = " & mvarRecId
        Dim DB_CONN As String = oDb.GetConnectionString
        Dim objDA As New SqlClient.SqlDataAdapter(strSQL, DB_CONN)
        Dim oDS As New Data.DataSet

        objDA.Fill(oDS)
        oRow = oDS.Tables(0).Rows(0)
        If oRow Is Nothing Then oRow = oDS.Tables(0).NewRow

        PopulateRowFromObject(oRow)
        Try
            objDA.Update(oDS)
        Catch ex As Exception

        End Try



    End Function
0
Comment
Question by:ExtremeFitness
  • 9
  • 6
18 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 16943109
First, since your function does not return a value, make it a Sub:

 Public Sub Save()

        Dim oRow As Data.DataRow
        Dim oDb As New clsDBAccess(mvarServer, mvarDatabase)
        Dim strSQL As String = "Select * from tblReports Where ReportID = " & mvarRecId
        Dim DB_CONN As String = oDb.GetConnectionString
        Dim objDA As New SqlClient.SqlDataAdapter(strSQL, DB_CONN)
        Dim oDS As New Data.DataSet

        objDA.Fill(oDS)
        oRow = oDS.Tables(0).Rows(0)
        If oRow Is Nothing Then oRow = oDS.Tables(0).NewRow

        PopulateRowFromObject(oRow)
        Try
            objDA.Update(oDS)
        Catch ex As Exception

        End Try



    End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16943140
also what does PopulateRowFromObject do?
0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 16943144
Error is :       "Update requires a valid UpdateCommand when passed DataRow collection with modified rows."      
0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 16943156
PopulateRowFromObject  Populates the data row with data from this object...
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16943223
Public Sub Save()

        Dim oRow As Data.DataRow
        Dim oDb As New clsDBAccess(mvarServer, mvarDatabase)
        Dim strSQL As String = "Select * from tblReports Where ReportID = " & mvarRecId
        Dim DB_CONN As String = oDb.GetConnectionString
objDA.SelectCommand=strSQL
        Dim objDA As New SqlClient.SqlDataAdapter(strSQL, DB_CONN)
        Dim oDS As New Data.DataSet

dim updateCmd as SqlCommand
                  
                  
                  ''Create update command for SqlDataAdapter
                  updateCmd = new SqlCommand("UPDATE tblReports SET field1=@value1, field2=@value2.... WHERE ....", objDA.SelectCommand.Connection)
                  
                  //Add parameters for update command
                  updateCmd.Parameters.Add(new SqlParameter("@value1", SqlDbType.VarChar))
                  updateCmd.Parameters("@value1") = .....


                  updateCmd.Parameters.Add(new SqlParameter("@value2", SqlDbType.VarChar))
                  updateCmd.Parameters("@value2")=...
            

                  ''Initialize the UpdateCommand of SqlDataAdapter
                  da.UpdateCommand = updateCmd


        objDA.Fill(oDS)
        oRow = oDS.Tables(0).Rows(0)
        If oRow Is Nothing Then oRow = oDS.Tables(0).NewRow

        PopulateRowFromObject(oRow)
        Try
            objDA.Update(oDS)
        Catch ex As Exception

        End Try

    End Sub

0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 16943398
Not was i was going for....
I was missing " Dim cb As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(objDA)"
Seems the right answer is....

    Public Function Save()

        Dim oRow As Data.DataRow
        Dim oDb As New clsDBAccess(mvarServer, mvarDatabase)
        Dim strSQL As String = "Select * from tblReports Where ReportID = " & mvarRecId
        Dim DB_CONN As String = oDb.GetConnectionString
        Dim objDA As New SqlClient.SqlDataAdapter(strSQL, DB_CONN)
        Dim cb As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(objDA)

        Dim oDS As New Data.DataSet

        objDA.Fill(oDS)
        oRow = oDS.Tables(0).Rows(0)
        If oRow Is Nothing Then oRow = oDS.Tables(0).NewRow

        PopulateRowFromObject(oRow)

        Try
            objDA.Update(oDS)

        Catch ex As Exception
            Console.WriteLine(ex)
        End Try

    End Function
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16943524
well, does it work?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Expert Comment

by:YZlat
ID: 16943529
also as I've said, change Function to Sub
0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 16943571
Ya it works now....  I need it as a function as it will return a boolean success or fail flag...
0
 
LVL 35

Expert Comment

by:YZlat
ID: 16954485
then change it to

 Public Function Save() As Boolean
dim bResult as boolean
        Dim oRow As Data.DataRow
        Dim oDb As New clsDBAccess(mvarServer, mvarDatabase)
        Dim strSQL As String = "Select * from tblReports Where ReportID = " & mvarRecId
        Dim DB_CONN As String = oDb.GetConnectionString
        Dim objDA As New SqlClient.SqlDataAdapter(strSQL, DB_CONN)
        Dim cb As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(objDA)

        Dim oDS As New Data.DataSet

        objDA.Fill(oDS)
        oRow = oDS.Tables(0).Rows(0)
        If oRow Is Nothing Then oRow = oDS.Tables(0).NewRow

        PopulateRowFromObject(oRow)

        Try
            objDA.Update(oDS)
    bResult=True
        Catch ex As Exception
            Console.WriteLine(ex)
         bResult=False
        End Try
    Save=bResult
    End Function

otherwise function will not return anything
0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 17098982
While Im all for giving credit where credit is due...  YZlat didnt solve this problem At all...  He offered a solution that was not at all in the style required....

0
 
LVL 35

Expert Comment

by:YZlat
ID: 17100340
first of all, the function should return a value and yours did not return anything, so either you should change it to Sub or return a boolean value from it
0
 
LVL 2

Author Comment

by:ExtremeFitness
ID: 17100459
Dude that is not an issue even worth thinking about :)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17100580
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 17100619
take a look at this argument as well

http://www.pcreview.co.uk/forums/thread-1393077.php
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now