Solved

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

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

827 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