Click here to Skip to main content
15,035,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using var type variable in ForEach loop and i am assining it integer arry how can i convert it to int type, below is my code
foreach (var item in this.checkedListBox1.CheckedItems)
                       da.ProcName = "AssignSubjectToStudent";//calling stored procedure
                   da.Parameters("@KeyStudent", int.Parse(txtStudent.SelectedValue.ToString()));
                       da.Parameters("@KeySubject",int.Parse(item) );// it can not be converted
Updated 22-May-14 20:56pm
Bh@gyesh 23-May-14 3:01am
Var type can take any datatype which is assigned it first. here item is var type so it is assigned as checkboxlist object.

You are passing keysubject as argument with int type conversion but item is an object type. so I think it is invalid. You should pass some other value i.e. item.count or item.selectedvalue etc...
DamithSL 23-May-14 3:20am
how you bind checkedListBox1? please update the question with that code

Hello ,
If you are using CheckedListBox then find any item by this way
(string)checkedListBox1.Items[i];//here i is the position of the selcted value

Now put this value into the store procedure
Abdullah Kundi 23-May-14 3:21am
thanks dear your logic is good but actualy i want to send database value member of selected items
in checkedlistbox, your code gives an error that Nvarchar value can'nt be converted to int,
the attribute in my table is of int type
Animesh Datta 23-May-14 4:06am
have your problem solved ?
Animesh Datta 23-May-14 3:34am
please select poper index of the column which you want to convert into Integer
You can use Convert.Int32(item)

May I ask, why do you use var when you're going to convert it to int afterwards? Isn't it more convenient if you declare it as int and not to convert it after?
Abdullah Kundi 23-May-14 3:26am
thanks for reply, but it gives error ,"Specified cast is not valid."
foreach (int item in this.checkedListBox1.CheckedItems)
Pikoh 23-May-14 3:30am
you can't convert item to int. Someone told you up,use int.Parse(item.value).
Abdullah Kundi 23-May-14 3:50am
error object does not contain definition for value.
foreach (var item in this.checkedListBox1.CheckedItems)

da.ProcName = "AssignSubjectToStudent";
da.Parameters("@KeyStudent", int.Parse(txtStudent.SelectedValue.ToString()));
da.Parameters("@KeySubject", int.Parse(item.value));

Pikoh 23-May-14 3:56am
Sorry,didn't notice it was a checkedListBox1 is a list of strings. What do you want exactly to add to @keysubject? the index of the checked item? if so,define an int variable before the foreach,use the foreach of all items,ask if item is checked,if so execute query and finally increment variable.

i'm not too awake as i'd like. use this instead

foreach(int indexChecked in checkedListBox1.CheckedIndices) {

Abdullah Kundi 23-May-14 6:46am
it have a logical error. Sir ji.
var is supposed to be used in areas where you are not really sure of the datatype which is returned.

You intend the value to be converted into a int so I suppose you know the datatype.

You cannot always expect a conversion as it actually depends on the data present in your variable. You can instead try to avoid errors with the following:

int nNumber = -1;

bool bIsConversionDone = Int32.TryParse(item, out nNumber);

    //Conversion failed
    nNumber = -1;

da.Parameters("@KeySubject",int.Parse(nNumber) );// it can not be converted

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