Advertisement

07.07.2008 at 07:38AM PDT, ID: 23543494
[x]
Attachment Details

Refering to Items in a List Box that objects

Asked by wallylamego in .Net Editors & IDEs, C# Programming Language, Microsoft Visual C#.Net

Tags: C# List Boxes, Internet Explorer 7, "Unable to cast object of type 'System.String' to type 'Item'"

I can get an error message which says "Unable to cast object of type 'System.String' to type 'Item'" on the following line of code in the code snippet: Item ServiceItem =  (Item) lstBxServices.Items[indexNo]Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
private void btnServiceSave_Click(object sender, EventArgs e)
        {
            //Populate Service Types Combo Box
            string commandString =
                "SELECT AccountNo, ServiceNo,LinkNo, ContractStart, ContractEnd " +
                "FROM dbo.TAS_ServicesTemp " +
                "WHERE AccountNo IS NULL ";
 
 
            // create the data set command object
            // and the DataSet
            DataAdapter = new SqlDataAdapter(commandString, ConnectString);
            DataSet = new DataSet();
            // fill the data set object
            DataAdapter.Fill(DataSet, "TempServices");
            // Get the one table from the DataSet
            DataTable = DataSet.Tables["TempServices"];
 
 
             // create a new row, populate it
            for (int indexNo = 2; indexNo < lstBxServices.Items.Count;  indexNo++)
            {      
                Item ServiceItem =  (Item) lstBxServices.Items[indexNo];
                DataRow newRow = DataTable.NewRow();
                newRow["AccountNo"] = 2;
                newRow["LinkNo"] = 5;
                newRow["ServiceNo"] = ServiceItem.ServiceNo;
                newRow["ContractStart"] = ServiceItem.ContractStart;
                newRow["ContractEnd"] = ServiceItem.ContractEnd;
                DataTable.Rows.Add(newRow);
            }
            
            // update the db
            DataAdapter.Update(DataSet, "TempServices");
            // inform the user and accept the changes
            //lblMessage.Text = DataAdapter.UpdateCommand.CommandText;
            Application.DoEvents();
            DataSet.AcceptChanges();
 
        }
private class Item
        {
 
            public int ServiceNo;
            public string ServiceDescription;
            public string ContractStart;
            public string ContractEnd;
            public string Charge;
            public Item(string Description, int No, string Start, string End, string Charge)
            {
                ServiceNo = No;
                ServiceDescription = Description;
                ContractStart = Start;
                ContractEnd = End;
                this.Charge = Charge;
            }
            public override string ToString()
            {
                return ServiceDescription;
            }
            public string ToString(Boolean Full)
            {
                if (Full)
                    return ServiceNo + "  " + ServiceDescription + "  " + ContractStart + "  " + ContractEnd + "  " + "R" +Charge;
                else
                    return ServiceDescription;
            }
        }
[+][-]07.07.2008 at 08:13AM PDT, ID: 21945751

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.07.2008 at 08:27AM PDT, ID: 21945903

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2008 at 08:28AM PDT, ID: 21945914

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.07.2008 at 08:41AM PDT, ID: 21946023

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .Net Editors & IDEs, C# Programming Language, Microsoft Visual C#.Net
Tags: C# List Boxes, Internet Explorer 7, "Unable to cast object of type 'System.String' to type 'Item'"
Sign Up Now!
Solution Provided By: DarkoLord
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628