Solved

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

Posted on 2011-09-25
3
164 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the id from URL? 19 46
Exception in Log4Net 1 17
Hide Tab Page 3 18
Convert to Hour/minute textbox and compare 3 35
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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