8 Replies - 1063 Views - Last Post: 11 May 2016 - 05:36 AM Rate Topic: -----

#1 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Input String Was Not In a Correct Format Error

Posted 08 May 2016 - 04:41 AM

Hello All,
This one has me perplexed!

ok, so I have a MySql Database full of data that I am attempting to add into a DataGridView. I need to do a lot of data manipulating throughout the program so I decided against connecting it directly.
ok so here I pull the data from the database and store it in a multidirectional array:

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

                    h[i, 0] = myReader.GetString("GameID");
                    h[i, 1] = myReader.GetString("TeamName");
                    h[i, 2] = myReader.GetString("Round1");
                    h[i, 3] = myReader.GetString("Round2");
                    h[i, 4] = myReader.GetString("Round3");
                    h[i, 5] = myReader.GetString("Round4");
                    h[i, 6] = myReader.GetString("Round5");
                    h[i, 7] = myReader.GetString("BestTeamName");
                    h[i, 8] = myReader.GetString("QuickHits");
                    h[i, 9] = myReader.GetString("Bonus");
                    h[i, 10] = myReader.GetString("Theme");
                    h[i, 11] = myReader.GetString("FInalBet");
                    h[i, 12] = myReader.GetString("Correct");
                    h[i, 13] = myReader.GetString("Total");
                    h[i, 14] = myReader.GetString("Completed");
                    h[i, 15] = myReader.GetString("ID");


                    i++;
                }



I figure it easiest to just have it all as Strings, then I can convert back and forth as I go..

so Here I add it into the DataGridView:

            for (int row = 0; row < ScoreData.GetLength(0); row++)
            {

                if (ScoreData[row, 0] != "" && ScoreData[row, 0] != null) {
                    DataGridViewRow rw = (DataGridViewRow)SBGrid.Rows[0].Clone();
                    rw.Cells[1].Value = ScoreData[row, 1].ToString();
                    rw.Cells[2].Value = ScoreData[row, 2].ToString();
                    rw.Cells[3].Value = ScoreData[row, 3].ToString();
                    rw.Cells[4].Value = ScoreData[row, 4].ToString();
                    rw.Cells[5].Value = ScoreData[row, 5].ToString();
                    rw.Cells[6].Value = ScoreData[row, 6].ToString();
                    rw.Cells[7].Value = ScoreData[row, 7].ToString();
                    rw.Cells[8].Value = ScoreData[row, 8].ToString();
                    rw.Cells[9].Value = ScoreData[row, 9].ToString();
                    rw.Cells[10].Value = ScoreData[row, 10].ToString();
                    rw.Cells[13].Value = ScoreData[row, 13].ToString();
                    rw.Cells[14].Value = ScoreData[row, 0].ToString();

                    PlayerCount++;
                    rw.Cells[0].Value = PlayerCount.ToString();
                    SBGrid.Rows.Add(rw);
                } 

            }



now, it loads to this point, and the data fills the DataGridView, however; as soon as I enter into a cell, the code gives me the error "Input String was not in a correct format" here:


        private void SBGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.ColumnIndex != 11) {
                MessageBox.Show("test " + (String)SBGrid.Rows[e.RowIndex].Cells[1].Value);
                if (String.IsNullOrWhiteSpace((String)SBGrid.Rows[e.RowIndex].Cells[1].Value))
                {
                    MessageBox.Show("test");


I left the 2 MessageBox in there because thats how I test to find the errors in my code, the first one pops up and shows the text in the cell, but it fails right before "test" MessageBox...

any idea as to what I am doing wrong?

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Input String Was Not In a Correct Format Error

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Input String Was Not In a Correct Format Error

Posted 08 May 2016 - 06:42 AM

it is probably worth printing your cell values to the console or debug so you can check they are what you expected

This post has been edited by horace: 08 May 2016 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6452
  • View blog
  • Posts: 26,096
  • Joined: 12-December 12

Re: Input String Was Not In a Correct Format Error

Posted 08 May 2016 - 06:52 AM

Why aren't you just binding the DGV to the data source?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5963
  • View blog
  • Posts: 20,433
  • Joined: 05-May 12

Re: Input String Was Not In a Correct Format Error

Posted 08 May 2016 - 09:26 AM

The more interesting question is why did you choose a 2 dimensional array of strings? Are we going back to the 70's style programming?
Was This Post Helpful? 0
  • +
  • -

#5 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: Input String Was Not In a Correct Format Error

Posted 08 May 2016 - 04:27 PM

View PostSkydiver, on 08 May 2016 - 11:26 AM, said:

The more interesting question is why did you choose a 2 dimensional array of strings? Are we going back to the 70's style programming?


Well, I learned how to program back in 1997 learning Larry Wall's Perl. and back them the only multi dimensional arrays were done similar.
I am new to C# and since I never had any formal training, I only learn new things as I find out about them. I was familiar with the Multi Dimensional arrays so hence.. I went with them.

Sorry if it offended you...
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5963
  • View blog
  • Posts: 20,433
  • Joined: 05-May 12

Re: Input String Was Not In a Correct Format Error

Posted 08 May 2016 - 06:48 PM

I was not offended.

Every language has its idioms and philosophies. In C#, strong typing and use of objects is the philosophy, and one of its idioms is making use of collections like lists. So the correct approach in C# for this type of problem is to create a class that represents each row where each property exposed by the class is the appropriate type for the data. You then create a list of these objects and bind them to the datagrid.
Was This Post Helpful? 0
  • +
  • -

#7 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: Input String Was Not In a Correct Format Error

Posted 09 May 2016 - 04:55 AM

View PostSkydiver, on 08 May 2016 - 08:48 PM, said:

I was not offended.

Every language has its idioms and philosophies. In C#, strong typing and use of objects is the philosophy, and one of its idioms is making use of collections like lists. So the correct approach in C# for this type of problem is to create a class that represents each row where each property exposed by the class is the appropriate type for the data. You then create a list of these objects and bind them to the datagrid.

I'm sorry, I'm not really understanding how that would be done, is there any tutorials that you may be aware of that would teach me this?
Thats how I usually learn everything.. Through online tutorials...
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5963
  • View blog
  • Posts: 20,433
  • Joined: 05-May 12

Re: Input String Was Not In a Correct Format Error

Posted 09 May 2016 - 05:56 AM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

Although the tutorial is specifically targeted at learning ASP.NET MVC, I feel that NerdDinner tutorial does a pretty good end-to-end example of getting data from database to the screen (and back again). You can probably stop reading it by step 4, but I highly recommend going through the rest of it.

Why? Because it teaches the modern approach used in WPF and Xamarin where you should keep your UI (aka views) separate from your data model, and business logic.
Was This Post Helpful? 2
  • +
  • -

#9 Randor  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 147
  • Joined: 12-October 10

Re: Input String Was Not In a Correct Format Error

Posted 11 May 2016 - 05:36 AM

Thank you! I learned quite a bit, all multi dimensional arrays have been removed.. Lol Thank you for your patience and willingness to share your knowledge..

And btw.. Once I connected directly to the MySql database, all those errors went away...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1