saving data in two different tables

Posted by Sumana2net under ASP.NET on 9/4/2009 | Views : 1829 | Status : [Member] | Replies : 2
hey experts,,,,

i have three different tables in SQL 2005 database. one table for personal details and second table for official details entered by the user in .aspx page...

i have all controls in only one .aspx page .... after entering all the details ,if i click save then all the data must be stored in two differnt tables...



I made different stored procedures for those two tables tables seperately .... in "Data Access layer" i wrote the following code




sqlcommand com=new sqlcommand("SP_perconaldetails;SP_officialdetails",con);

com.commandtype=commandtype.storedprocedure;

///here code to insert detals using stored procedure


com.executenonquery();






so please give me idea for the problem.... Pleae make it in C# language





than xin advance




Responses

Posted by: Vuyiswamb on: 9/6/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good day

What you are currently doing is not possible in a sense that you want a command object to execute two stored Procedure. Because today is Sunday, i cant be online for more than 30 MIN, i will look at your post tomorow. Why you can do is to use nested stored procedure, and if the Parameters are the same then its easy like that.

Thank you for posting at dotnetfunda

Were looking forward for your reply

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Sumana2net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 9/6/2009 [Member] Starter

Up
0
Down
hi,
there is no need of 2 storedprocedure

create 2 table
personal
id-int primary key
age-int
name -varchar
and another table
official
id -int primary key
dept -varchar
sal -int


stored procedure

create procedure testtables
@id int,
@name varchar(50),
@age int,
@dept varchar(50),
@sal int
as
insert into personal(id,[name],age)values(@id,@name,@age)
insert into official(id,dept,sal)values(@id,@dept,@sal)



next i am writing the code in button's click event
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=BAIJU;Initial Catalog=baiju;Integrated Security=True");
SqlCommand cmd = new SqlCommand("testtables", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@age", TextBox3.Text);
cmd.Parameters.AddWithValue("@dept", TextBox4.Text);
cmd.Parameters.AddWithValue("@sal", TextBox5.Text);
try
{
con.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Added successfully";
}
catch (Exception ex)
{

Label1.Text = ex.Message;
}
finally
{
con.close();
}

}

this is working fine
any way if it work or any problem
write to me
KLBAIJU@YAHOO.CO.IN
Regards
K L BAIJU


Sumana2net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response