Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27472

Update and insert into SQL database from a dataset

$
0
0
Hi

Warning, this is a long post by a newbie VB2010 but experienced VB6 programmer, please by pass it if you don't have any time, or feel that a sarcastic and/or non-helpful answer is appropriate. Thanks.

I have looked through my books and on the internet, but I can't find the answer to this problem.
I have a form where Telephone issues are logged and resolved or re-assigned to other people to resolve.

I create a DataTable(should I be using a Dataset?) to hold my fields..in a similar fashion to to a recordset in vb6.

I pass the DataTable to a class function for processing into two tables...but how do I access the data in the DataTable from the function?


In vb6 I do this AFTER defining and filling my rsNewIssue field on a windows form:


In a function:

Public Function Save_Issue_Details(rsNewIssue As ADODB.Recordset, ByVal Issue As Integer, strTask As String, strResolution As String, strComments As String) As String
'A query to save issues
Dim rsIssue As New ADODB.Recordset
On Error GoTo BadSend

With cnNEQAS
.Open
.CursorLocation = adUseClient

rsIssue.CursorType = adOpenKeyset
rsIssue.LockType = adLockOptimistic

strSQL = "SELECT " & strTask & ".TaskID, " & strResolution & ".TaskRID, " & strTask & ".ActionID, " & strTask & ".ActionedToID, " & strTask & ".ADate, " _
& "" & strTask & ".TimesAssigned, " & strResolution & ".RComments " _
& "FROM " & strResolution & " RIGHT JOIN (" & strComments & " RIGHT JOIN " & strTask & " ON " & strComments & ".TaskID = " & strTask & ".TaskID) " _
& "ON " & strResolution & ".TaskRID = " & strTask & ".TaskID " _
& "WHERE " & strTask & ".TaskID = " & Issue & ""

rsIssue.Open strSQL, cnNEQAS, , , adCmdText
strSQL = ""

If Len(Trim$(rsNewIssue!RComments)) Then 'It's a resolved issue
With rsIssue
!ActionID = 1 'Change the Action to Resolved
!ADate = rsNewIssue!ADate 'Enter the resolved date
.Update
.AddNew 'Generate a new entry in TaskResolutions table
!TaskRID = (Issue) 'Enter the Task ID
!RComments = (rsNewIssue!RComments) 'Enter a new Resolution comment in TaskResolution Table
.Update
End With

Else 'It's a deferred issue

If rsNewIssue!ActionedTo > 0 Then 'If a new person has been actioned to deal with this
With rsIssue
!ActionedToID = (rsNewIssue!ActionedTo) 'Change the designated person
!ActionID = 2 'Keep the ActionID as 2, unresolved
!TimesAssigned = !TimesAssigned + 1 'Increment the number of time that this task has been assigned
.Update
End With

End If
End If

Set rsIssue = Nothing
strSQL = ""
.Close
End With

Exit Function

BadSend:
Save_Issue_Details = "Error Entering Issues" & " - " & Err.Description
MsgBox (Save_Issue_Details), vbOKOnly + vbCritical, Progname

Set rsIssue = Nothing

cnNEQAS.Close

End Function


How can I do this in VB 2010? My DataTable name is Tasks("Resolution")


Here is how I create my Datatable on my form:

....................
...............
'*Define the DataTable Name
Dim Tasks As DataTable = New DataTable("Resolution")

'*Define the column names
Dim workCol As DataColumn = Tasks.Columns.Add( _
"TaskID", Type.GetType("System.Int32"))
workCol.AllowDBNull = False
workCol.Unique = False

Tasks.Columns.Add("RComments", Type.GetType("System.String"))
Tasks.Columns.Add("ActionedToID", Type.GetType("System.Int32"))
Tasks.Columns.Add("ActionID", Type.GetType("System.Int32"))
Tasks.Columns.Add("TimesAssigned", Type.GetType("System.Int32"))
Tasks.Columns.Add("ADate", Type.GetType("System.DateTime"))

'*Define the Row
Dim workRow As DataRow = Tasks.NewRow()

'*Fill the Row

'*Decide if the issue was resolved or not, and if it has been re-assigned
If Len(LTrim$(TextBox2.Text)) > 0 Then
Edit = True
workRow("TaskID") = TaskID
workRow("RComments") = LTrim$(TextBox2.Text)
workRow("ActionID") = 1 '*Issue was Resolved
workRow("ADate") = Now()
Else
If ActionedToID > 0 Then
Edit = False
workRow("ActionedToID") = ActionedToID
workRow("TimesAssigned") = (TimesAssigned + 1) '*Increment the Times Assigned counter
workRow("ActionID") = 2 '*Issue was Un-Resolved
End If

End If

.....
...

And it is passed to my function like this:

intSave = UserDetails.Save_Issue_Details(Edit, Tasks, TaskID, "Tasks", "TaskResolutions", "TaskComments") 'Send the recordsets to the clsUser for saving


My function line looks like this: (The code is probably not even near to where I want to be..please be patient).

Public Function Save_Issue_Details(ByVal Edit As Boolean, ByVal Tasks As DataTable, ByVal TaskID As Integer, ByVal strTask As String, ByVal strResolution As String, ByVal strComments As String) As Integer

'*Open the connection
cnNEQAS.Open()

'If Edit = True Then '*Record had been resolved, and not reassigned

'strSql = "UPDATE " & strTask & ".ActionID, " & strTask & ".ADate " _ '*No Idea how to even start this bit
' & "FROM " & strTask & " " _
' & "WHERE " & strTask & ".TaskID = " & TaskID & ""

'*Add the new Resolution record into the Resolution table
strSql = "INSERT INTO " & strResolution & "(TaskRID, RComments) " _
& "Values (@TaskID, @RComment)" '*How do I get the @TaskID and @RComment form the Tasks Datatable?

Dim cmdnon As SqlCommand = New SqlCommand(strSql, cnNEQAS)

Try
'*Execute nonquery to inset an RComment
cmdnon.ExecuteNonQuery()

Catch ex As Exception
MsgBox(ex.ToString)
Finally
cnNEQAS.Close()
MsgBox("Data updated ! ")
End Try

End Function
The Edit variable was added because, as I believe, I can't update one table and add a new entry (Insert) into another table at the same time.

All of the examples that I have seen use form-based sql statements etc, and they are easy to follow, but none of them use functions that have Datasets/Datatables passed into them.

I want to do the same as my vb6 code..even if I have to split the Update and Insert New bits into 2 separate sections. However, to be plain, I haven't got the faintest clue how to even start this. Believe me, I have spent hours trawling through Google searches and my books..to no avail.

For various reasons, I do not want to use Stored Proceedures at all...that is why I do all of this in Functions.

Thank you for any help that you may offer. When I have got the gist of inserting and updating my database tables via my class functions..then I think that I will be well on my way to understanding .net, which I am really beginning to like!

Regards

DJ

Viewing all articles
Browse latest Browse all 27472

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>