Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

Please advice me on how to insert or add records to multiple tables in sql 2008 using c sharp.

Table1 is EMPLOYEES
Table2 is BIRTHDAY

The record gets added to table1 perfectly, I want it to be added to table2 simultaneously.
below is the code which I am using
C#
private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection DB_Connection = SQLConnect.Instance;
            SqlDataAdapter da = new SqlDataAdapter();            
            da.InsertCommand = new SqlCommand("INSERT INTO EMPLOYEES VALUES(@EMPID, @NAME, @DOB)", DB_Connection);
            
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("Please Fill in the Employee ID");
                textBox1.Focus();
                da.InsertCommand.Parameters.Add("@EMPID", SqlDbType.NVarChar).Value = DBNull.Value;
            }
            else
            {
                da.InsertCommand.Parameters.Add("@EMPID", SqlDbType.NVarChar).Value = textBox1.Text;
            }
           
           if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Please Fill in the Employee Name");
                textBox2.Focus();
                DB_Connection.Close();
                //da.InsertCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = DBNull.Value;                
            }
            else
            {
                da.InsertCommand.Parameters.Add("@NAME", SqlDbType.NVarChar).Value = textBox2.Text;
            }
		
	    if (string.IsNullOrEmpty(dateTimePicker1.Text))
            {
                da.InsertCommand.Parameters.Add("@DOB", SqlDbType.Int).Value = DBNull.Value;
            }
            else
            {
                da.InsertCommand.Parameters.Add("@DOB", SqlDbType.Date).Value = dateTimePicker1.Value;
            }
System.Windows.Forms.TextBox sj = new TextBox();
            try
            {
                if (sj.Text.Length >= 0)
                {
                    MessageBox.Show("yes proceed");
                    DB_Connection.Open();
                    int ins = da.InsertCommand.ExecuteNonQuery();                    
                    MessageBox.Show(ins.ToString() + " Record Inserted to DB");
                    ClearForm(this);                    
                    DB_Connection.Close();
                }
                else
                {
                    MessageBox.Show("Fill in all the Details");
                }
            }
            catch (Exception yu)
            {
                MessageBox.Show(yu.Message);
            }
Posted
Updated 8-Mar-11 21:11pm
v2
Comments
Sandeep Mewara 9-Mar-11 3:11am    
Next time onwards, please use PRE tags to format code part. It makes the question readable.

1 solution

Dear Friend

If the two operations need to be completed simultaniously then i would suggest you to use stored procedures
pass values as argument and execute it


da.InsertCommand = new SqlCommand("EmployeeInfoInsert(@EMPID, @NAME, @DOB)", DB_Connection);
:
:
:
-------Stored Procedure ---------

createprocedure EmployeeInfoInsert(@EMPID varchar(20), @NAME varchar(20), @DOB datetime)
as
begin
   insert statement to EMPLOYEE table
   insert satatement to BIRTHDAY table 
end
 
Share this answer
 
v2
Comments
jaipe 9-Mar-11 22:20pm    
Thank you, i had it with trigger.... but i will start from stored procedures, coz there are many more to be inserted :)

Cheers

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