?
Solved

C#.net - errors in code....

Posted on 2011-09-25
3
Medium Priority
?
171 Views
Last Modified: 2013-12-17
I have this code and there are several errors including the arraylist....


the code basically pulls items from a database and binds to a dropdown list
if (flag == "Array")
        {

            try
            {
                DropDownList MyDropDown = default(DropDownList);
                string TC_ID = null;
                ArrayList myArray = new ArrayList();
                SqlDataReader dataReader = default(SqlDataReader);
                int x = 0;
                string sqlConnection = null;

                TC_ID = Session["SessionTC_ID"];
                MyDropDown = DropDownList1;


                using (SqlConnection sqlConn = new SqlConnection())
                {
                    sqlConnection = "Data Source=xx.xx.xx.xx;Initial Catalog=sddasdas;Trusted_connection=true;";
                    sqlConn.ConnectionString = sqlConnection;
                    string SQLstring = "SELECT distinct(campaign_name) FROM campaign WHERE TC_ID = '" + TC_ID + "'";

                    sqlConn.Open();
                    using (SqlCommand command = new SqlCommand(SQLstring, sqlConn))
                    {
                        dataReader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())
                            {
                                myArray.Add(dataReader("camp_name").ToString());
                            }

                        }
                        dataReader.Close();
                        sqlConn.Close();
                    }
                }

                myArray.Sort();

                MyDropDown.DataSource = myArray;
                MyDropDown.DataBind();
            }
            catch (Exception ex)
            {

            }
        }

Open in new window

0
Comment
Question by:GlobaLevel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Author Comment

by:GlobaLevel
ID: 36596341
here is one error:

  while (dataReader.Read())
                            {
                                myArray.Add(dataReader("camp_name").ToString());
                            }

>>>datareader is a variable being used as a method

====================================

 TC_ID = Session["SessionTC_ID"];

>>>>casting error?
CANNOT IMPLICITLY CAST 'OBJECT' TO 'STRING'
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 36596810
You are in C#. Brackets instead of parenthesis when you want to reference an element of an array or a collection.

myArray.Add(dataReader["camp_name"].ToString())

-----

TC-ID is a String, Session["SessionTC_ID"] returns an Object. You need to cast the Object to a String:

TC_ID = (string) Session["SessionTC_ID"];

0
 
LVL 2

Assisted Solution

by:yogsoft
yogsoft earned 1000 total points
ID: 36708928
Partially agree to JamesBurger that in C# bracket should be used instead of parenthesis. Also following line could potentially throw null reference exception.

myArray.Add(dataReader["camp_name"].ToString())

Use myArray.Add(Convert.ToString(dataReader["camp_name"]));

 


0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question