0

This is my code. I have 3 tables named

accounts, WaterDB, ElectricDB and GasDB.

-In the code below i insert information to the acounts table.

  • In my accounts, WaterDB, ElectricDB and GasDB, I have a column called username.

  • Is it possible for me to insert the username into the other tables 'username' column as well?

Code where i insert information to my accounts table:

protected void registerBtn_Click1(object sender, EventArgs e)
    {
        try
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["userDatabaseConnectionString1"].ConnectionString);
            conn.Open();
            string insertQuery = "insert into accounts (username,email,password,postalcode,role) values (@Uname ,@email ,@pswd ,@pstlc,@role)";
            SqlCommand com = new SqlCommand(insertQuery, conn);
            com.Parameters.AddWithValue("@Uname", registerUsernameTB.Text);
            com.Parameters.AddWithValue("@email", registerEmailTB.Text);
            com.Parameters.AddWithValue("@pswd", registerUserPswdTB.Text);
            com.Parameters.AddWithValue("@pstlc", postalCodeTB.Text);
            com.Parameters.AddWithValue("@role", role.Text);

            com.ExecuteNonQuery();
            Response.Redirect("Loginx.aspx");
            Response.Write("Registration is successful");

            conn.Close();

        }
user2864740
  • 60,010
  • 15
  • 145
  • 220

2 Answers2

0

Hope these (accounts, WaterDB, ElectricDB and GasDB) are different tables in same database. Then you can run multiple insert queries in one statement

Ex:

string insertQuery = "insert into accounts (...) values (...); insert into othertable (...) values (...)"

Or you can write trigger to other inserts deletes or whatever.

Or you can write a stored procedure to insert in multiple tables as well

Community
  • 1
  • 1
pravprab
  • 2,301
  • 3
  • 26
  • 43
0

I'm not saying this is how you should be handling your registration system, just answering your question.

Just create 3 connections - 1 for each database - and run the same code that you already have on each one. You can reuse a lot of the code, just change the database connection object for each one.

Frank Coyle
  • 113
  • 6