Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
CSS
How to save Multiple textbox values in diferrent-2 Rows in Database at a time-

Like-

Textbox1.Text="John";

Textbox1.Text="Marcus";

Textbox1.Text="Megan";

Sql Table-

ID   Name
1     John
2     Marcus
3     Megan

Can Anybody tell me the solution plz...
Posted

You can run a query to insert your values ...

And since you have 3 text boxes you could do it in a for loop or whatever other magical way you decide to,

http://www.w3schools.com/sql/sql_insert.asp[^]
 
Share this answer
 
Hope so it will help you.

class MultiPleValues
    {
        DataTable dt;

        private void Insert()
        {
            try
            {
                dt = GetDataTable();

                //You can use following in loop. or as your requirement.
                DataRow dr = dt.NewRow();
                dr.BeginEdit();
                dr["S_NO"] = "You Values";
                dr["Name"] = "Your Name";
                dr.EndEdit();
                dt.Rows.Add(dr);


                //After your work compeletition.
                //Insert dt in database useing following method:

                string strQuer = "Select * from --Your Table Name Here";
               dt =  InsertQuery(strQuer, dt);      //this will insert your values in db and also select modified values
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private DataTable GetDataTable()
        {
            try
            {
                DataTable dtTemp = new DataTable();
                dtTemp.Columns.Add("S_NO");
                dtTemp.Columns.Add("Name");
                return dtTemp;
            }
            catch (Exception)
            {

                throw;
            }
        }


        public static DataTable InsertQuery(string SelectQuery, DataTable dt)
        {
            try
            {
                SqlConnection sqlconn = new SqlConnection("Your Connection String here");
                //this function will insert changes in DB, also select updated values.
                sqlconn.Open();
                SqlCommand sqlcommand = new SqlCommand(SelectQuery, sqlconn);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlcommand);
                SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
                cmdBuilder.ConflictOption = ConflictOption.OverwriteChanges;
                adapter.Update(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }


Don't hesitate in asking any question?

Thanx
 
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