[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2006-06-20
18
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 2000 total points
ID: 17100619
take a look at this argument as well

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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 …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 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