Solved

Append one table to another with variables

Posted on 2008-10-22
10
337 Views
Last Modified: 2010-04-21
Hi,
I am creating an audit system asp net website based on one I have in access.
I have a table that stored details about the audit, date, site etc (AuditDates) and another table (AuditQuestions) that stores each separate question related by the AuditID field.
When creating a new audit I need to append all the rows from my master question table (Questions) into the AuditQuestions also including the correct AuditID from the AuditDates table to link the tables (this will have to be done using variable on the insert statement).
I am completely new to asp.net so any help would be greatly appreciated,
Thanks,
Iain
0
Comment
Question by:RBG IT
  • 6
  • 4
10 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774210
Is this a question about SQL insert statement which implemented within ASP.NET or is this ASP.NET only? I was abit confuse about your question.

The way I see it, you are asking how do insert that into the database ?

Can you show us what you have now or is this you ask for an idea? If you ask for an idea: I think you have to first insert the Audit data to Audit table, because this will be the parent table, the AuditID will be there after creation (autogenerated or manually),

then you insert the same number of row in Questions to AuditQuestions with AuditID.

So I gues AuditQuestion will have similar columns as the Questions (standard table) and some another more columns which include AuditID - this refer to the Audit table

Is that waht you ask for?

JINN
0
 

Author Comment

by:RBG IT
ID: 22774267
Hi JINN,
I will have to insert a record into the AuditDates first to gerenate the AuditID.
I then have to append the questions from the Questions table (master question set) into the AuditQuestions table including the AuditID from the AuditDates so the 2 are now related and i have a full set of questions that can now be updated for the audit.
I have included the VBA version I created in Access.
Cheers,
Iain


Do Until rstQuestions.EOF                '
        AuditID = CostCentre & ReportTypeID & YearNumber & WeekNumber
        DoCmd.RunSQL _
                "INSERT INTO AuditQuestions (QuestionNo,AuditID,ReportDateID,ReportTypeID,ReportHeaderID,ReportQuestionsID,Question,Count,PriorityID,NonQuestion) VALUES('" _
                & rstQuestions!Order & "','" & AuditID & "','" & _
                    ReportDateID & "','" & ReportTypeID & "','" & _
                    rstQuestions!ReportHeaderID & "','" & _
                    rstQuestions!ReportQuestionsID & "','" & _
                    rstQuestions!ReportQuestion & "','" & _
                    TotalCount & "','" & rstQuestions!PriorityID _
                    & "','" & rstQuestions!NonQuest & "');"
    rstQuestions.MoveNext
Loop

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774340
Ohhh ok, so you want to have similar idea in ASP.NET right?

Which language do you want it to be ? VB or C#?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:RBG IT
ID: 22774359
Hi Jinn,
I have been trying to create it in ASP.NET without any success!
It would certainly be easier for me in VB as it is more familier,
Cheers,
Iain
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22774506
Check out this code, the StringBuilder part you can continue with that idea.

I can run this code at my computer now, so you might wanna check the syntax, but this is the idea you should follow, I think you have an Access database store somewhere, so give it the right path, incase you work with MSSql you will use Sql..... instead of OleDB...

Hope this helps

JINN

'Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=D:\\Databases\yourdb.mdb"
Dim conn As OleDbConnection = New OleDbConnection(conStr)
'Now you create a data adapter. A data adapter constructor takes two arguments - A SQL string and a connection object.
 
Dim sqlStr As String = "SELECT * FROM Questions"
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)  
 
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, "Questions") 
 
//AuditID, this is where you get your AuditID, shouldnt' it be from database?
'AuditID = CostCentre + ReportTypeID + YearNumber + WeekNumber
Dim querySB AS New  System.Text.StringBuilder
For Each drDSRow In DS.Tables("Questions").Rows()
        querySB.Append("INSERT INTO AuditQuestions (QuestionNo,AuditID,ReportDateID,ReportTypeID,ReportHeaderID,ReportQuestionsID,Question,Count,PriorityID,NonQuestion) VALUES('")
	querySB.Append(drDSRow("Order") + "', '" + AuditID + "', '" ReportDateID + "', '" + ReportTypeID + "', '" + drDSRow("ReportHeaderID"...........)) 'the rest of your sql query
	dim cmd as new OleDbCommand(querySB.ToString(), conn)
	conn.Open()
	cmd.ExecuteNonQuery '<-- this will return no of row that effected, > 0 means success
	conn.Close()
Next

Open in new window

0
 

Author Comment

by:RBG IT
ID: 22775027
Hi Jinn,
Thanks for that it certainly gives me a much better understanding of how it is done.
On you notes you mention the source for the AuditID - it will be taken from a hidden textbox on the page.
I am connecting to SQL Server (the database has been converted from access).
I have setup a table adaptor for the questions table - how would I adapt you code to work with it?
Sorry to ask so much but I am struggling rather a lot!
Cheers,
Iain
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22775085
I will be out for lunch for awhile, I will keep you posted after

brb

JINN
0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22775406
I am back,

Here is the code reference, complete the StringBuilder and test it out, you have to make sure, your connectionString to connect to the SQLServer database is right, that your application has access the the given database.

Hope this helps
JINN

'Creating connection and command sting
'Make sure you have right configuration so you can connect to the database successfully. Check authentication etc...
Dim conStr As String = "Data Source=212.85.123.213;Initial Catalog=MyDataBaseName;Integrated Security=True" 
Dim conn As SqlConnection = New SqlConnection(conStr)
'Now you create a data adapter. A data adapter constructor takes two arguments - A SQL string and a connection object.
 
Dim sqlStr As String = "SELECT * FROM Questions"
' Create data adapter object
Dim da As SqlDataAdapter = New SqlDataAdapter(sqlStr, conn)  
 
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, "Questions") 
 
'AuditID = CostCentre + ReportTypeID + YearNumber + WeekNumber
Dim querySB AS New  System.Text.StringBuilder
For Each drDSRow In DS.Tables("Questions").Rows()
        querySB.Append("INSERT INTO AuditQuestions (QuestionNo,AuditID,ReportDateID,ReportTypeID,ReportHeaderID,ReportQuestionsID,Question,Count,PriorityID,NonQuestion) VALUES('")
        querySB.Append(drDSRow.Item("Order") + "', '" + AuditID + "', '" ReportDateID + "', '" + ReportTypeID + "', '" + drDSRow.Item("ReportHeaderID"...........)) 'the rest of your sql query
        dim cmd as new SqlCommand(querySB.ToString(), conn)
        conn.Open()
        cmd.ExecuteNonQuery '<-- this will return no of row that effected, > 0 means success
        conn.Close()
Next

Open in new window

0
 

Author Closing Comment

by:RBG IT
ID: 31508653
Thanks for that Jinn, I have to play about with field formats a little but it has solved my problem and given me a better understanding,
Cheers,
Iain
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22784549
Glad to help,

Later on you will find it's helpful to separate Data handle class, but this will do for most of cases.

Jinn
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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