ExtremeFitness
asked on
Problem saving Data to database using SQL Server and VB.Net
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(s trSQL, 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
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(s
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
also what does PopulateRowFromObject do?
ASKER
Error is : "Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
ASKER
PopulateRowFromObject Populates the data row with data from this object...
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(s trSQL, 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.Connec tion)
//Add parameters for update command
updateCmd.Parameters.Add(n ew SqlParameter("@value1", SqlDbType.VarChar))
updateCmd.Parameters("@val ue1") = .....
updateCmd.Parameters.Add(n ew SqlParameter("@value2", SqlDbType.VarChar))
updateCmd.Parameters("@val ue2")=...
''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
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(s
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.Connec
//Add parameters for update command
updateCmd.Parameters.Add(n
updateCmd.Parameters("@val
updateCmd.Parameters.Add(n
updateCmd.Parameters("@val
''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
ASKER
Not was i was going for....
I was missing " Dim cb As SqlClient.SqlCommandBuilde r = New SqlClient.SqlCommandBuilde r(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(s trSQL, DB_CONN)
Dim cb As SqlClient.SqlCommandBuilde r = New SqlClient.SqlCommandBuilde r(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
I was missing " Dim cb As SqlClient.SqlCommandBuilde
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(s
Dim cb As SqlClient.SqlCommandBuilde
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
well, does it work?
also as I've said, change Function to Sub
ASKER
Ya it works now.... I need it as a function as it will return a boolean success or fail flag...
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(s trSQL, DB_CONN)
Dim cb As SqlClient.SqlCommandBuilde r = New SqlClient.SqlCommandBuilde r(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
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(s
Dim cb As SqlClient.SqlCommandBuilde
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
ASKER
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....
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
ASKER
Dude that is not an issue even worth thinking about :)
Here, I think you will benifit from this article:
http://getdotnetco.web101.discountasp.net/GdncStore/free/Articles/Sub%20Statement%20vs%20Function%20Statement.htm
http://getdotnetco.web101.discountasp.net/GdncStore/free/Articles/Sub%20Statement%20vs%20Function%20Statement.htm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(s
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