0

I want to insert MySqlDataReader read value into an array.but I get the exception "Index was outside the bounds of the array". Here is my code,

        string[] a = new string[1000];

        string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
        MySqlConnection mycon = new MySqlConnection(myconstring);
        string sql = "SELECT flag FROM sms_data_bankasia group by flag";
        MySqlCommand comd = mycon.CreateCommand();
        comd.CommandText = sql;
        mycon.Open();
        MySqlDataReader dtr = comd.ExecuteReader();
        count = 0;


        int i = 0;
            while (dtr.Read())
            {

                a[i] = dtr.GetValue(i).ToString();
                i++;

            }

What can I do.Any one can help me?

4
  • Go to your profile by clicking on your name, then you'll see all the questions that you have asked. Then open question by clicking on it and accept answer that was helpful by clicking on the tick on the left of the answer. Commented Sep 25, 2011 at 8:49
  • @evilone now I accept some answers,so I think you must help me. Commented Sep 25, 2011 at 9:11
  • @sumona Great! But I see that Jon already helped you and you already accept his answer :) Commented Sep 25, 2011 at 9:22
  • 2 answers are acceptable so what to do? Commented Sep 25, 2011 at 9:53

2 Answers 2

2

Try cleaning your code a little and use a dynamically resizing List<T> to which you can add elements:

var result = new List<string>();
var myconstring = "SERVER=localhost;DATABASE=alicosms;UID=root;PASSWORD=;";
using (var con = new MySqlConnection(myconstring))
using (var cmd = con.CreateCommand())
{
    con.Open();
    cmd.CommandText = "SELECT flag FROM sms_data_bankasia group by flag";
    using (var reader = cmd.ExecuteReader())
    {
        while (reader.Read())
        {
            result.Add(reader.GetString(reader.GetOrdinal("flag")));
        }
    }
}

string[] a = result.ToArray();
Sign up to request clarification or add additional context in comments.

10 Comments

if I want to use only array then what I do @darin?
@sumona, look at the last line of my code sample. It converts the List<T> into an array.
@sumona, var is a keyword introduced in C# 3.0 allowing you to declare variables with implicit type: msdn.microsoft.com/en-us/library/bb383973.aspx If you are using an older .NET version which doesn't support this simply replace var with their respective type: MySqlConnection, MySqlCommand, MySqlReader, ...
It only provide count.But i need value.So What to do @darin?
@sumona, I don't understand what are you talking about. Count of what? Value of what?
|
1

This looks suspicious to me:

a[i] = dtr.GetValue(i).ToString();

That means you're fetching column 0 of row 0, column 1 of row 1, column 2 of row 2 etc... but you've only got a single column ("flag").

I suspect you meant:

a[i] = dtr.GetValue(0).ToString();

That will still fail if there are more than 1000 rows though - it would be better to use a List<string>:

List<string> data = new List<string>();
while (dtr.Read())
{
    data.Add(dtr.GetValue(0).ToString()); // Or call GetString
}

4 Comments

if I want to use array then what to do @Jon?
@sumona: Well, I would question why you want to use arrays first... but you could always call ToArray() on the list afterwards. The important thing is that an array always has a fixed size after you've created it - but you don't know how many rows will be in your result set.
If list are int type then what i write instead of "data.Add(dtr.GetValue(0).ToString());" @jon
@sumona: What do you mean by "if list are int type"? If you want to create a List<int> you probably want dtr.GetInt32.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.