Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
Hi all,

How to store data in two different tables when we click the button.Using Stored procedure how to do it.

Suppose I have a 2 tables named as customer and product.

In customer table I have 4 fields custid,custname,custaddress,prodid
In Product table I have 3 fields prodid,prodname,price

So when i click on insert button all the data will be inserted to respective tables


Thanks


[Edited]"Pre" tag removed from Normal statement[/Edited]
Posted
Updated 2-Apr-18 22:30pm
v5
Comments
manoharank5 13-Sep-11 0:05am    
Do you want to store the same data in two different tables? Please explain the actual problem
Prerak Patel 13-Sep-11 0:14am    
Make some efforts first.

You can use Transaction. Transaction are implememted in .Net application when you want to Combine multiple tasks together to execute a Single unit of work.

SqlConnection con = new SqlConnection("Your Connection String");
con.Open();
SqlTransatcion tran = con.BeginTransaction();
try
SqlCommand cmd = new SqlCommand("Your Insert command for customer",con,tran);
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand("Your Insert command for Product",con,tran);
cmd1.ExecuteNonQuery();
tran.Commit();
}
catch(SqlException ex)
{
tran.Rollback();
}
 
Share this answer
 
You can store data in as many tables as you want on click event. You just have to run different commands. You may use transaction.
 
Share this answer
 
U can write a Stored Procedure or a simple transaction . if it doesn't meet ur solution ,then please describe your question properly.
 
Share this answer
 
You can use stored procedure, in that you can create queries to fulfill the requirment(insert data into 2 tables) and execute the SP with single command.
 
Share this answer
 
As i have submitted above in that i used 2 different tables to store data.
i have used 2 different stored procedures.
u can call that two stored procedure on that button_click event so data will be saved in database at one button click.

Thanks & Regards.
 
Share this answer
 
You can store data into different different table at a time.
like Customer table, Depaertment Table etc.
first insert data into Customer table
Customer cust=new Customer();
if(cust.CustomerID==0)
{
cust.Name=txtName.Text;
data.InsertOnSubmit(cust);
data.SubmitChanges();--> here data is your data access layer object(LINQ-SQL)
}
Department dept=new Department();
if(dept.DepartmentID==0)
{
dept.CustomerID=cust.CustomerID;(if CustomerID is foreign key in Department)
dept.DeptName=txtdept.Text;
data.InsertOnSubmit(dept);
data.submitchanges();
}
 
Share this answer
 
hi, i have done this done in 3-tier architecture. i have used 2 different store procedure for insert data in two different table.
On save_button click i have passed parameters for that 2 different stored procedure prom .cs page to BAL Class by creating BAL's object.
And from BAL to DAL class.In DAL Class i have created 2 different functions for that two different stored procedure.

Refer This Code --

In .CS Page:-

C#
protected void Button1_Click(object sender, EventArgs e)
     {          
         name = Txt1.Text;
         pname = Txt5.Text;
         pcode = Txt3.Text;
         addr = Txt4.Text;
         pc = Txt3.Text;
         rmname=box1.Text;
         rmbatchno = box2.Text;
         mfg_date = dt.Date;
               
            
            pBAL.insert(name, pname, pcode, addr);            
            pBAL.insert2(pc, rmname, rmbatchno, mfg_date);
            
      }

In BAL:-

public void  insert(string name, string pname, string pcode, string addr)
    {
            pDAL.insert(name, pname, pcode, addr);
    }

    public void insert2(string prcode, string rmname, string rmbatchno, DateTime mfg_date)
    {        
            pDAL.insert2(prcode,rmname,rmbatchno,mfg_date);        
    }


In DAL:-

 public void  insert(string name, string pname, string pcode, string addr)
    {
              
        SqlCommand sqlcmd = new SqlCommand("pfirst", con);
        sqlcmd.CommandType = CommandType.StoredProcedure;
        
            sqlcmd.Parameters.Add("@u", SqlDbType.VarChar).Value = name;
            sqlcmd.Parameters.Add("@n", SqlDbType.VarChar).Value = pname;
            sqlcmd.Parameters.Add("@a", SqlDbType.VarChar).Value = pcode;
            sqlcmd.Parameters.Add("@c", SqlDbType.VarChar).Value = addr;
            con.Open();
            sqlcmd.ExecuteNonQuery();    
            con.Close();
            
        
    }
    public void insert2(string prcode ,string rmname, string rmbatchno,DateTime mfg_date)

    {
        SqlCommand sqlcmd1 = new SqlCommand("psecond", con);
        sqlcmd1.CommandType = CommandType.StoredProcedure;
        
            con.Open();
            sqlcmd1.Parameters.Add("@u", SqlDbType.VarChar).Value = prcode;
            sqlcmd1.Parameters.Add("@n", SqlDbType.VarChar).Value = rmname;
            sqlcmd1.Parameters.Add("@a", SqlDbType.VarChar).Value = rmbatchno;
            sqlcmd1.Parameters.Add("@c", SqlDbType.DateTime).Value = mfg_date;            
            sqlcmd1.ExecuteNonQuery();        
            con.Close();
        
    }
 
Share this answer
 
v2

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