Click here to Skip to main content
15,884,088 members
Home / Discussions / Database
   

Database

 
AnswerRe: get the inserted GUID Pin
Mycroft Holmes30-May-09 23:15
professionalMycroft Holmes30-May-09 23:15 
Questionhow do i use a primary key auto-generated in a table as a foreign key in another table? Pin
ChiSmile29-May-09 21:10
ChiSmile29-May-09 21:10 
AnswerRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
Mycroft Holmes29-May-09 23:40
professionalMycroft Holmes29-May-09 23:40 
GeneralRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
ChiSmile30-May-09 0:29
ChiSmile30-May-09 0:29 
GeneralRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
Mycroft Holmes30-May-09 0:48
professionalMycroft Holmes30-May-09 0:48 
GeneralRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
ChiSmile1-Jun-09 2:09
ChiSmile1-Jun-09 2:09 
GeneralRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
Mycroft Holmes1-Jun-09 3:33
professionalMycroft Holmes1-Jun-09 3:33 
GeneralRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
ChiSmile1-Jun-09 16:49
ChiSmile1-Jun-09 16:49 
Here is the code, Plz dont mind my variable names as u said b4.But anyway, this works perfectly for me.


  Try

            LoadData()  ' function containing connection string

            ' Configure and execute the command.
'the scope_identity() is inserted in the query that will insert the data
'this will retrieve the last auto-generated number from the first table.
            comd = New System.Data.SqlClient.SqlCommand("insert into Data_Modification (Patient_ID, Date_Collected, Wrong_Data_Entry_Date, Data_Field, Provider)" & _
                " values (@PatRep_ID, @DateCol, @WrngDataDate, @Data_Field, @Provider) select scope_identity()", con)

    'Declare and Initialize parameter that is passed to the query strings
            Dim paraPatId As SqlParameter = comd.Parameters.Add("@PatRep_ID", repDatPatID.Text)
            Dim paraDte As SqlParameter = comd.Parameters.Add("@DateCol", repDatColTmePikar2.Value.ToShortDateString)
            Dim paraWrngD As SqlParameter = comd.Parameters.Add("@WrngDataDate", repWDatEDaTmePika.Value.ToShortDateString)
            Dim paraDaFld As SqlParameter = comd.Parameters.Add("@Data_Field", repDatDFldCmBx.Text)
            Dim paraProv As SqlParameter = comd.Parameters.Add("@Provider", repDatProvTbx.Text)
            

 'command object is used to call the ExecuteScalar() and returns may be a 
'decimal value which is converted to integer and assigned to a variable.

            LoadData()
            
            Dim key As Integer = Convert.ToInt32(comd.ExecuteScalar())
            con.Close()


            LoadData()

            ' this is an insertion to the second table, the integer value (key) 
'Obtained above is passed on to the second query as foreign key (data_mod_ID) to the second table

            comd = New System.Data.SqlClient.SqlCommand("insert into Modification_copy (data_mod_ID,Date_Collected, Data_Attribute,Correct_data_Value)" & _
                " values (@DataModIdD,@retDateCol, @DatAtt, @CorDaVal)", con)

            'comd.Connection = con
            Dim paraModID As SqlParameter = comd.Parameters.Add("@DataModIdD", key.ToString)
            Dim paraDteCol As SqlParameter = comd.Parameters.Add("@retDateCol", repDatColTmePikar2.Value.ToShortDateString)
            Dim paraDAtt As SqlParameter = comd.Parameters.Add("@datAtt", repMorCmBx.Text)
            Dim paraCorDVa As SqlParameter = comd.Parameters.Add("@corDaVal", repMorTxb.Text)

            ' Load the DataTable.
            'datadapt = New System.Data.SqlClient.SqlDataAdapter(comd)
            'datadapt.Fill(dSet)
            'dSet.Tables.Add(dTab)

            ' Dim rowAffected As Integer = 0

            ' cmd.Connection = cn

            ' rowAffected = 
            comd.ExecuteNonQuery()
            If MsgBox("Report successful!. Do you want to report more Data?", MsgBoxStyle.YesNo, "Confirmation!!!") _
            = Windows.Forms.DialogResult.Yes Then
                repMorDetPnl.Hide()
                repDatTabPg.Show()

            Else
                repMorDetPnl.Hide()
                vwPatient.Show()
            End If

        Catch ex As SqlException
            MsgBox(ex.Message.ToString())
        Finally
            con.Close()
        End Try 




Public Sub LoadData()
        dSet = New DataSet
        dTab = New DataTable
        con = New System.Data.SqlClient.SqlConnection("Data Source=192.168.3.101,1433;Initial Catalog=DataBaseName;User ID=Doctor;Password=gudone;")
        con.Open()
    End Sub

GeneralRe: how do i use a primary key auto-generated in a table as a foreign key in another table? Pin
Mycroft Holmes1-Jun-09 17:17
professionalMycroft Holmes1-Jun-09 17:17 
Questionhow i can do this Pin
NNR_Noga29-May-09 4:22
NNR_Noga29-May-09 4:22 
AnswerRe: how i can do this Pin
Bassam Saoud29-May-09 4:43
Bassam Saoud29-May-09 4:43 
GeneralRe: how i can do this Pin
NNR_Noga29-May-09 5:15
NNR_Noga29-May-09 5:15 
GeneralRe: how i can do this Pin
Bassam Saoud29-May-09 5:29
Bassam Saoud29-May-09 5:29 
GeneralRe: how i can do this Pin
Mycroft Holmes29-May-09 23:44
professionalMycroft Holmes29-May-09 23:44 
QuestionNHibernate - Good or Bad Idea? Pin
Jacobus0129-May-09 3:00
Jacobus0129-May-09 3:00 
AnswerRe: NHibernate - Good or Bad Idea? Pin
Bassam Saoud29-May-09 4:50
Bassam Saoud29-May-09 4:50 
QuestionEDB Pin
ujjawal kumar27-May-09 23:24
ujjawal kumar27-May-09 23:24 
AnswerRe: EDB Pin
Bassam Saoud29-May-09 4:58
Bassam Saoud29-May-09 4:58 
QuestionEvent Viewer Pin
jonhbt27-May-09 22:51
jonhbt27-May-09 22:51 
AnswerRe: Event Viewer Pin
Garth J Lancaster28-May-09 0:06
professionalGarth J Lancaster28-May-09 0:06 
AnswerRe: Event Viewer Pin
Eddy Vluggen28-May-09 0:13
professionalEddy Vluggen28-May-09 0:13 
QuestionADO .NET and SQL Client Installation Pin
D_Ana27-May-09 7:28
D_Ana27-May-09 7:28 
AnswerRe: ADO .NET and SQL Client Installation Pin
Eddy Vluggen27-May-09 7:56
professionalEddy Vluggen27-May-09 7:56 
GeneralRe: ADO .NET and SQL Client Installation Pin
D_Ana27-May-09 8:00
D_Ana27-May-09 8:00 
AnswerRe: ADO .NET and SQL Client Installation Pin
Vimalsoft(Pty) Ltd27-May-09 20:56
professionalVimalsoft(Pty) Ltd27-May-09 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.