none
saving data in to different database table RRS feed

  • Question

  • is it possible to save data in different table? for example I want to save the patient info and medical history but they have different database table
    Saturday, September 29, 2018 2:25 AM

Answers

  • Yes you can as this is what I'd call a master/detail relationship.

    Save the patient information by opening a connection and command object where the connection object connection string points to the proper database, save data. Using the same connection, close it, change the connection string. For the command best to use another one with the SQL UPDATE or INSERT statement pointing to the table for medical history and execute.

    Simplified example, If you are updating multiple patients at one time, that would not be the proper way to go, instead one patient at a time. For one patient, multiple history records that also would be unrealistic, instead save each record as they change or are added. If that is not to you liken then the code below becomes more complex, we need to get into the state of each DataRow, check for it's RowState and act upon it.

    Using cn As New SqlConnection With {.ConnectionString = "For patient database connection"}
        Using cmdPatient As New SqlCommand With {.Connection = cn, .CommandText = ""}
            ' add parameters for command text
            cn.Open()
            cmdPatient.ExecuteNonQuery()
            cn.Close()
        End Using
    
        cn.ConnectionString = "Medical history database connection"
    
        Using cmdHistory As New SqlCommand With {.Connection = cn, .CommandText = ""}
            ' add parameters for command text
            cn.Open()
            cmdHistory.ExecuteNonQuery()
        End Using
    End Using


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cherkaoui.Mouad Sunday, September 30, 2018 12:02 PM
    • Marked as answer by no[one] Tuesday, October 9, 2018 12:21 PM
    Saturday, September 29, 2018 9:58 AM
    Moderator

All replies

  • Yes you can as this is what I'd call a master/detail relationship.

    Save the patient information by opening a connection and command object where the connection object connection string points to the proper database, save data. Using the same connection, close it, change the connection string. For the command best to use another one with the SQL UPDATE or INSERT statement pointing to the table for medical history and execute.

    Simplified example, If you are updating multiple patients at one time, that would not be the proper way to go, instead one patient at a time. For one patient, multiple history records that also would be unrealistic, instead save each record as they change or are added. If that is not to you liken then the code below becomes more complex, we need to get into the state of each DataRow, check for it's RowState and act upon it.

    Using cn As New SqlConnection With {.ConnectionString = "For patient database connection"}
        Using cmdPatient As New SqlCommand With {.Connection = cn, .CommandText = ""}
            ' add parameters for command text
            cn.Open()
            cmdPatient.ExecuteNonQuery()
            cn.Close()
        End Using
    
        cn.ConnectionString = "Medical history database connection"
    
        Using cmdHistory As New SqlCommand With {.Connection = cn, .CommandText = ""}
            ' add parameters for command text
            cn.Open()
            cmdHistory.ExecuteNonQuery()
        End Using
    End Using


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cherkaoui.Mouad Sunday, September 30, 2018 12:02 PM
    • Marked as answer by no[one] Tuesday, October 9, 2018 12:21 PM
    Saturday, September 29, 2018 9:58 AM
    Moderator
  • hi can you help show how to calculate subtotals and grandtotals in datagridview VB.NET 

    I have tried for 3 days but ended getting incorrect results which is not correct.

    i tried this code but they seem work incorrectly

    Public Class Contract_Variation_Details
        Private SQL As New SQLContol
        Public index, kz As Integer
        Private Sub GetRunningTotal()
            For Each ROWSWITHDATA As DataGridViewRow In DgvVariationDetails.Rows
                Dim SUM As Decimal
    
                If ROWSWITHDATA.Cells(0).Value = "Sub-Total" Then
                    'store the index of that cell to a variable named 'index'
                    index = ROWSWITHDATA.Cells(12).RowIndex
    
                ElseIf ROWSWITHDATA.Cells(0).Value <> "Sub-Total" Then
                    'store the index of that cell to a variable named 'kz'
                    kz = ROWSWITHDATA.Cells(12).RowIndex
                End If
                For cell As Integer = 0 To DgvVariationDetails.Rows.Count - 2 Step +1
    
                    'this is the formula to get sum of column value where its cell(0) doesnot contain the word 'Sub-Total' 
                    SUM += CDec(DgvVariationDetails.Rows(kz).Cells(12).Value)
    
                Next
                DgvVariationDetails.Rows(index).Cells(12).Value = SUM
            Next
        End Sub

    and this below is my datagridview with wrong results in column A and no result in column B

    Tuesday, October 9, 2018 1:36 PM
  • Please start a new thread for this, once you do I will provide a good starter project which will be very different than what you have now meaning it works from data sources, not the actual DataGridView


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 9, 2018 1:41 PM
    Moderator