0

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?
2
Contributors
6
Replies
18
Views
2 Years
Discussion Span
Last Post by djjeavons
0

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

0

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

0

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

Edited by ramkishor: before test

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.