开发者

ASP.NET and SQL - inserting data into more than one table?

开发者 https://www.devze.com 2023-01-30 14:17 出处:网络
So I know similar questions have been asked before, but I couldn\'t find a clear answer for my specific situation.I\'m using ASP.NET (in Visual Web Developer) and I need to insert data from one form i

So I know similar questions have been asked before, but I couldn't find a clear answer for my specific situation. I'm using ASP.NET (in Visual Web Developer) and I need to insert data from one form into two separate tables. This is my data source:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/courseinfo.mdb" 
    SelectCommand="SELECT * FROM [tableCourse], [tableFaculty]"
    InsertCommand="INSERT INTO [tableCourse] 
    ([department], [name_first], [name_last], [prefix], 
    [course_number], [credits], [title], [description])
    VALUES (?, ?, ?, ?, ?, ?, ?, ?); INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email])
    VALUES (?, ?, ?, ?)">

So you see I've tried using two insert statements, and it just comes back with an error saying there are extra characters after the SQL statement. I've tried taking out the semi-colon and then it says I'm missing a semi-colon. Is it possible to insert to two tables at once using this control? And if not, how do I work around this?

UPDATE:

Okay, tried it in the codebehind, but I don't think I did it right, now it's giving me this error:

Server Error in '/CCC' Application. Index or primary key cannot contain a Null value. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Index or primary key cannot contain a Null value.

Source Error:

Line 87: 
Line 88:         AccessDataSource1.InsertCommand = "INSERT INTO [tableCourse] ([department], [name_first], [name_last], [prefix], [course_number], [credits], [title], [description]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
Line 89:         AccessDataSource1.Insert()
Line 90: 
Line 91:         AccessDataSource1.InsertCommand = "INSERT INTO [tableFaculty] ([name_first], [name_last], [phone], [email]) VALUES (?, ?, ?, ?)"

Line 89 is the one that's highlighted. So I'm thinking it's attempting to insert but for some reason the values are null, it isn't taking the values from the text boxes. I probably left som开发者_如何学JAVAething obvious out, I don't know, I'm really new at this.


Why don't you give it a try at the codebehind?

AccessDataSource1.InsertCommand = "First INSERT Statement";
AccessDataSource1.Insert();

AccessDataSource1.InsertCommand = "Second INSERT Statement";
AccessDataSource1.Insert();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号