HOW TO SELECT A TABLE FROM MANY TABLE WHILE INSERTING

ranjunath govind

Total Post:1

Points:7
Posted by  ranjunath govind
 1183  View(s)
Ratings:
Rate this:
I have 4 tables
 
StdntBtech,StdntPg,StdntUg,StdntDip
 
i have to insert values into StdntBtech if the course name is BE or Btech
similarly 
 
i have to insert values into StdntPG if the course name is MCA or MSc
similarly
i have to insert values into StdntUg if the course name is BSc
 
similarly 
i have to insert values into StdntDip if the course name is Diploma

which statement i have to use(IF or Case)
if so how the syntax will be?
how i can use a string instead of table name in insert statement?



  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: How to select a table from many table while inserting

    Here is my code for insert record in a table according to course name from many table while inserting...


    string tblname = "";
            if (txtcourse.Text == "BE" || txtcourse.Text == "Btech")
            {
                tblname = "StdntBtech";
            }
            else if (txtcourse.Text == "MCA" || txtcourse.Text == "MSc")
            {
                tblname = "StdntPg";
            }
            else if (txtcourse.Text == "BSc" || txtcourse.Text == "BA")
            {
                tblname = "StdntUg";
            }
            else if (txtcourse.Text == "Diploma")
            {
                tblname = "StdntDip";
            }
            else
            {
                Response.Write("Please Insert Correct Course Name!!");
            }
            string st = "insert into " + tblname + " values('" + txtName.Text + "','" + txtCity.Text + "','" + txtcourse.Text + "')";
            if (tblname != "")
            {
                cn.Open();
                cmd = new SqlCommand(st, cn);
                int executerow = cmd.ExecuteNonQuery();
                if (executerow > 0)
                {
                    Response.Write(executerow + " Row's affected");
                }
                cn.Close();
            }
            else
            {
                Response.Write("Please Insert Correct Record");
            }

      Modified On Jul-23-2014 11:33:57 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!