Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-25
3
Medium Priority
?
173 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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