简体   繁体   中英

Extract value from string collection

I have a string collection like this

SetDEL_Stores.Add(DealerCode + "," + ItemIdentityCode.Text + "," + decimal.Parse(Qty.Text) + "," + DateTime.ParseExact(ExpireDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture) + "," + BatchNumber.Text);

InsertDEL_Stores(SetDEL_Stores);

after pass values to the SetDEL_Stores (my string collection), collection is

"S0010M,AZI002M,3,12/26/2013 12:00:00 AM,VDIQ20"

in this case I'm using one value from this collection, as a parameter with SQL query

    private void InsertDEL_Stores(StringCollection SC_PurLinr)
    {
        String strConnString = ConfigurationManager.ConnectionStrings["CBConnectionString"].ConnectionString;
        const String strQuery = "SELECT BatchNumber FROM DEL_Stores WHERE BatchNumber = @BatchNumber";
        SqlConnection conPR = new SqlConnection(strConnString);
        SqlCommand cmdPR = new SqlCommand();
        cmdPR.Parameters.AddWithValue("@BatchNumber", SC_PurLinr.IndexOf("4"));
        cmdPR.CommandType = CommandType.Text;
        cmdPR.CommandText = strQuery;
        cmdPR.Connection = conPR;

            conPR.Open();
            SqlDataReader sdr = cmdPR.ExecuteReader();
            while (sdr.Read())
            {
                TextBox1.Text = sdr["BatchNumber"].ToString();
            }

            conPR.Close();
            conPR.Dispose();
     }

After execution I'm getting an error

Conversion failed when converting the nvarchar value 'VDIQ20' to data type int.

It points to the line

while (sdr.Read())

But if I set the parameter like cmdPR.Parameters.AddWithValue("@BatchNumber", "VDIQ20"); as normal I can take the correct value to expected textbox

What can I do to fix this problem? Please help me ..

EDIT:

I think you have a few more problems going on. You are passing your entire StringCollection to this method when you should be passing a single string. I don't think you need to use a StringCollection in this case.

Something like:

var singleItem = SC_PurLinr[0]; 
InsertDEL_Stores(singleItem);

Change your method to:

private void InsertDEL_Stores(string singleItem)

....

cmdPR.Parameters.AddWithValue("@BatchNumber", singleItem.Split(',')[4]);

SC_PurLinr.IndexOf("4") does not return what I think you think. It returns int, not string.

In case of your example string it doesn't find "4" and it returns -1.

So your query is "SELECT BatchNumber FROM DEL_Stores WHERE BatchNumber = -1"

In order to check if BatchNumber = -1 database converts BatchNumber (nvarchar) to int but it can't and that's why you receive this error.

Instead you should index the StringCollection to get element you want not call IndexOf method.

In addition to Peri's answer. (Edited to correct the index of BatchNumber)

If you are trying to access the 4th string ( StringCollection is a zero indexed collection type). You will need to do something like this.

cmdPR.Parameters.AddWithValue("@BatchNumber", SC_PurLinr[3]);

That will give you the 4th value of SC_PurLinr which looks like it is the batch number you are looking for.

.IndexOf("4")返回一个int ,而不是一个string

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM