Vijaya Vijaya - 1 year ago 60
ASP.NET (C#) Question

Insert the date into 2 tables

I have 2 tables like Item and Image, first table tbl_Item has 2 fields those are item code,item name and second table tbl_Image has 2 fields like item code and item image. I have fields like item code, item name, item image and one button. When i click the submit button those records are inserted into both two tables. I want to insert the data into two tables How it is possible? Can any body suggest me?

cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ToString());
cmd.CommandText = "insert into tbl_item (@itemcode,@itemname) values(ItemCode,ItemName)";
cmd.CommandText = "insert into tbl_image (@itemcode,@itemimage) values(ItemCode,ItemImage)";

Answer Source

if you want to insert both record in single command execution then join query with ";".

such as

cmd.CommandText = "insert into tbl_item (@itemcode,@itemname) values(ItemCode,ItemName);insert into  tbl_image (@itemcode,@itemimage) values(ItemCode,ItemImage)";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download