Solved

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

Posted on 2011-09-25
3
165 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
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 250 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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now