Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
1.09/5 (3 votes)
See more:
C#
private void nbtnAddStaff_Click(object sender, EventArgs e)
        {
            try
            {
                string username = txtUserName.Text;
                string password = txtPassword.Text;
                string name = txtName.Text;
                string telephone = txtTelephone.Text;
                string phoneNumber = txtPhno.Text;
                string Address = txtAddress.Text;
                string role = comboBox1.SelectedItem.ToString();
                string eid = txtEid.Text;
                string officeAddress= txtOfficeAddress.Text;
                SqlConnection con = new SqlConnection("Data Source=AISASQITSVC02;Initial Catalog=DApp20140408;Integrated Security=True");
                string qr = "Insert into Equip_Person (Name,Address,Telephone_Number) values(@name,@address,@telephone_number)";
                string query="Insert into Equip_Employee(EID,OfficeAddress,PhoneExtNo,UserName,Password,Role) values(@EID,@OfficeAddress,@phoneNumber,@username,@password,@role)";
                SqlCommand cmd = new SqlCommand(qr, con);
                SqlCommand cmnd= new SqlCommand(query,con);
                cmd.Parameters.AddWithValue(@name, txtName.Text);
                cmd.Parameters.AddWithValue(@Address, txtAddress.Text);
                cmd.Parameters.AddWithValue(@telephone, txtTelephone.Text);
                cmnd.Parameters.AddWithValue(@eid, txtEid.Text);
                cmnd.Parameters.AddWithValue(@officeAddress, txtOfficeAddress.Text);
                cmnd.Parameters.AddWithValue(@username, txtUserName.Text);
                cmnd.Parameters.AddWithValue(@password, txtPassword.Text);
                cmnd.Parameters.AddWithValue(@phoneNumber, txtPhno.Text);
                cmnd.Parameters.AddWithValue(@role, comboBox1.SelectedItem.ToString());
                con.Open();
                int i = cmd.ExecuteNonQuery();

                if (i != 0)
                {
                    MessageBox.Show("Staff information added successfully");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
Posted
Updated 21-Jul-14 19:39pm
v2
Comments
SRS(The Coder) 22-Jul-14 1:39am    
can you please elaborate a bit regarding your issue ?
Maciej Los 22-Jul-14 1:43am    
We can't read in your mind or direct from your screen. COuld you, please, be more specific?

1 solution

Well...you could add one more line:
C#
int i = cmd.ExecuteNonQuery();
int j = cmnd.ExecuteNonQuery();

But - you really do need to think about what you are doing here.
You need to look up about SqlTransactions - so that if the second insert fails the first one can be undone: otherwise your DB is left in a undetermined state and subsequent code will fail.
You also need to stop storing passwords in clear text - it is a major security risk! Have a look here: Password Storage: How to do it.[^]

But you don't need two queries to do this - you can do it all in one. Before you get to that though, I think you need to have a good think about your database design: it's clear from the code that these two tables are related in some way - they both contain information related to the same individual. But there is no way in the data you are saving to "connect" the two table rows together. So when you try to retrieve them later, it doesn't work! You need to think about how you are going to fetch the data in later code and redesign your DB so that you can get all related info easily. Or you are going to make a lot of work for yourself later!
 
Share this answer
 

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