I want to assign a value parameter dynamic through an array. For example, the array arr_param
will contain two value [0] = "abc"
, [1] = "ZYX"
.Now, I will assign them .The problem here is that i must specify the correct elements [0]
, [1]
after run foreach, I tried do it but failed convert to int
string[] arr_param = array_parameter.Split(';');
foreach (DataRow parmRow in parmsDataTable.Rows)
{
string parmName = parmRow[parmNameDataColumn].ToString();
cmd.Parameters.AddWithValue(parmName, arr_param[Convert.ToInt32(parmsDataTable.Rows)]);
}
I get error Unable to cast object of type 'System.Data.DataRowCollection' to type 'System.IConvertible'
.
parmsDataTable.Rows
is a DataRowCollection
and it can't be converted to int
.
A Simple Solution:
string[] arr_param = array_parameter.Split(';');
int counter = 0;
foreach (DataRow parmRow in parmsDataTable.Rows)
{
string parmName = parmRow[parmNameDataColumn].ToString();
cmd.Parameters.AddWithValue(parmName, arr_param[counter++)]);
}
Or:
string[] arr_param = array_parameter.Split(';');
foreach (DataRow parmRow in parmsDataTable.Rows)
{
string parmName = parmRow[parmNameDataColumn].ToString();
cmd.Parameters.AddWithValue(parmName,arr_param[parmsDataTable.Rows.IndexOf(parmRow))]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.