Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to save same data multiple times which depend on textbox value .

Ex-name ,city want to save, and we give textbox value 3,then it will save three time in database.

anybody help me ,thankyou.
Posted

C#
int i;
if (int.TryParse(myTextBox.Text, out i))
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        while (i > 0)
            {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (NameColumn, CityColumn) VALUES (@NM, @CT)", con))
                {
                cmd.Parameters.AddWithValue("@NM", name);
                cmd.Parameters.AddWithValue("@CT", city);
                cmd.ExecuteNonQuery();
                }
            i--;
            }
        }
    }
 
Share this answer
 
send the give textbox value to database and use loop for number of iterations and inside the loop keep your insert query.
 
Share this answer
 
Try this:
C#
int totalno;
totalno=Convert.ToInt32(TextBox1.Text);
for (int i = 0; i <= totalno; i++)
{
 //insert query
}
 
Share this answer
 
v3

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