Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two table
one is invoicedetails and another one is productdetails

invoicedetails have (invoiceid,invoiceno,firmname and invoice date)field in table and invoice id is primary key


and productdetails have (productid,name,description,qty,price and invoiceid)field where productid is primary key and invoiceid is foriegn key with respect to invoicetable

now i want to insert multiple product details in productdetails table using single invoice id how could i achieve that can any one help me
Posted

1 solution

Just use INSERT commands to add multiple rows, each with the same InvoiceId reference.
Then you just pull the appropriate records using a JOIN or WHILE clause.
 
Share this answer
 
Comments
Member 10891595 17-Aug-15 5:14am    
sorry sir i didnot get you can you pls tell me in details because i am new for sql.
OriginalGriff 17-Aug-15 5:39am    
What have you tried?
Member 10891595 17-Aug-15 5:45am    
on save button click this code i have written


int lastid;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand(strConnString,con);

cmd.CommandText = "sp_insertinvoice";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();


//cmd.Parameters
cmd.Parameters.AddWithValue("@invoice_date", txtDate.Text);
cmd.Parameters.AddWithValue("@Firm_name", txt_frmname.Text.Trim());
cmd.Parameters.AddWithValue("@Regis_id", txt_regno.Text.Trim());
cmd.Parameters.Add("@idcategory", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue("@productname", ((TextBox)gridviewproduct.Rows[0].FindControl("grd_txt_productname")).Text);
cmd.Parameters.AddWithValue("@description", ((TextBox)gridviewproduct.Rows[0].FindControl("grd_txt_description")).Text);
cmd.Parameters.AddWithValue("@qty", ((TextBox)gridviewproduct.Rows[0].FindControl("grd_txt_price")).Text);
cmd.Parameters.AddWithValue("@price", ((TextBox)gridviewproduct.Rows[0].FindControl("grd_txt_price")).Text);



con.Open();
cmd.ExecuteNonQuery();
lastid = Convert.ToInt32(cmd.Parameters["@idcategory"].Value.ToString());




and procedure that i have created is this

ALTER PROCEDURE [dbo].[sp_insertinvoice] (
@invoice_date date,
@firm_name varchar,
@Regis_id varchar,
@idcategory int output,
@productname varchar,
@description varchar,
@qty int,
@price int
)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO tblInvoiceDetails(invoice_date,Firm_Name,Resigtration_no) VALUES
(@invoice_date,@firm_name,@Regis_id)
SET @idcategory=SCOPE_IDENTITY()
insert into tbl_ProductDetails values(@productname,@description,@qty,@price,@idcategory)
RETURN @idcategory
END
Member 10891595 17-Aug-15 5:46am    
can you pls tell me where i am doing mistake because this code only insert one value at a time

please suggest me to right way to do this
i want to create invoice receipt form
OriginalGriff 17-Aug-15 6:07am    
Start by looking at your SP: if you are updating two tables at once, you really, really need a transaction.

But I wouldn't do this via an SP, I'd do it by creating a transaction in the C# code, insert the record in the first table and retrieve the ID value (here an SP makes sense).
Then I'd create a DataTable to hold each invoice line, and do bulk inserts via a DataAdapter using that table.
Then commit the transaction.

Have a go - it's not difficult, it just sounds like it - and you will learn a heck of a lot better doing it yourself than if I give you the code! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900