sir, i have created a global variable on one button click within for loop this variable has many records i want each record to be inserted on another event can it be possible.
as

string str;
button click()
{
foreach(datarow dr in dt.rows)
{



    str=dr["field"].tostring();
    }


  } 
    submit button()
    {

    insert into table()values(str);
    }


i have to send each value of str how it can be possible?

Hi

Your post is a little confusing. As it stands, your str variable will only contain the last field obtained within the for loop and therefore you will only be writing one value back to the table. Is this what you want to achieve?

Or do you want to take all field values from the database and then insert many new rows back to the table? If this is the case then I would suggest that you use a List<string> and for each row, add the field to the list. Then you can enumerate the list and insert each individual string as a new record to the table.

HTH

yes , i need as you say, please give me an example briefly

Hi

To add each row to a list of strings you can do something like:

List<string> stringValues = new List<string>();

foreach (DataRow dr in table.Rows)
{
    stringValues.Add(dr["field"].ToString());
}

Then to add all values in the list you could do something similar to the following:

foreach (string stringValue in stringValues)
{
    string query = "INSERT INTO YourTable (ColumnName) VALUES (@StringValue)";

    // Setup your data access code to connect to the database and issue a command
    using (SqlCommand command = new SqlCommand(query))
    {
        command.Connection = yourConnectionObject; //Modify this
        command.Parameters.AddWithValue("@StringValue", stringValue);
        command.ExecuteNonQuery();
    }
}

Note, the above is not tested and is not the full solution, just a pointer.

HTH

thanks i am testing it

here stringvalue holds count only;i want to use its all vallues to be inserted on another button click.....can it be possible?

What do you mean?

Can you post the code that you are using now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.