Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert data by clicking button one times where there have a dropdown list selected number and two textboxes. i select dropdown list numbers [ex. 2] and give input data in two textboxes then click insert button one times. data save in the database table multiple rows, how many numbers select from dropdown list. for example-

dropdown-list = 2,3,4 ; // select any number for insert multiple rows in the database table

textbox= "data" ; // input data
textbox= "data" ; // input data


-- how can i solve this problem, please anyone give me a perfect idea???

What I have tried:

C#
protected void ButtonArngeOffpk_Click(object sender, EventArgs e)
{
   SqlCommand insert = new SqlCommand("insert into TableAdvOffPeak(Adv_Name, Duration) values(@Adv_Name, @Duration)", con);
   insert.Parameters.AddWithValue("@Adv_Name", TextBoxCmpName.Text);
   insert.Parameters.AddWithValue("@Duration", TextBoxDrtion.Text);
        try
        {
            con.Open();
            insert.ExecuteNonQuery();

        }
        catch
        {
            con.Close();
        }
        }
Posted
Updated 29-Mar-16 19:43pm
v2

Try this

C#
protected void ButtonArngeOffpk_Click(object sender, EventArgs e)
       {

           string value = DropDownList1.SelectedItem.Text;  // Get the dropdown value
           int count = 0;
           int.TryParse(value, out count);  // cast the value to integer

           for (int i = 0; i < count; i++)  // iterate it for the N times
           {

               SqlCommand insert = new SqlCommand("insert into TableAdvOffPeak(Adv_Name, Duration) values(@Adv_Name, @Duration)", con);
               insert.Parameters.AddWithValue("@Adv_Name", TextBoxCmpName.Text);
               insert.Parameters.AddWithValue("@Duration", TextBoxDrtion.Text);
               try
               {
                   con.Open();
                   insert.ExecuteNonQuery();
                   con.Close();

               }
               catch
               {

               }

           }
       }
 
Share this answer
 
v4
Comments
Karthik_Mahalingam 30-Mar-16 1:38am    
try the updated solution.
Shwrv 30-Mar-16 2:42am    
thnx @KARTHIK for ur code. I've done with some changes. thanx u again :)
Karthik_Mahalingam 30-Mar-16 2:43am    
always welcome :)
Shwrv 30-Apr-16 5:00am    
hllw, thora sa coding help chahiye . can you help me ? @KARTHIK
Karthik_Mahalingam 30-Apr-16 5:02am    
sure shwrv, what help you need?
Simply, put a loop on selected number from dropdownlist
see below snippet

C#
int iCount = convert.ToInt16(dropdownlist1.value);
//loop on counter
for(int i=0; i<icount;>{
   //your insert script goes here 
}
 
Share this answer
 
Comments
koolprasad2003 30-Mar-16 2:24am    
Your code is for insertion, Just copy your code and paste it in for loop in my code.

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