jeudi 17 avril 2014

Chaîne c# MySQL d'entrée n'était pas au format correct - Stack Overflow


I am trying to make C# WinForm application which is connected to my MySQL database and I am displaying content from my table "knjiga" into my datagridview so user can enter new data etc. When I am pressing CTRL+F5 to run my program my form is running perfectly and I have no problems. However, when I go in bin/Debug directory and I run .exe file with doubleclick I am getting error message on start: "Input string was not in correct format" and after I click OK, another error message is showing "Index was out of range. Must be non-negative and less than the size of collection. Parameter name: index." When I click on error details its showing me that problem is with my datagridview and some weird messages on line 154 and method that I have deleted and that doesnt exist anymore in my code and that method is DisableTable(). I don't know why ?


Here is my code and how I am filling my datagridview with data from MySQL table:


private string[] kolone_knjiga = new string[] { "ID knjige", "Autor knjige", "Naziv knjige", "Žanr", "Jezik", "Donator", "Datum upisa", "Format", "Broj stranica", "Izdavač", "Godina izdavanja", "Vrijednost knjige (kn)", "Polica odlaganja", "Rashod/Otpis", "Broj knjiga" };
private void showTable(string novi_query)
{
DataTable tabela = new DataTable();
foreach (string x in kolone_knjiga) { tabela.Columns.Add(x); }
MySqlConnection conDataBase = new MySqlConnection(conString);
MySqlCommand cmdDataBase = new MySqlCommand(novi_query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
DataRow red = tabela.NewRow();
red[0] = myReader.GetString("id_knjige");
red[1] = castAutor(myReader.GetInt32("autor_knjige"));
red[2] = myReader.GetString("naziv_knjige");
red[3] = castZanr(myReader.GetInt32("zanr"));
red[4] = castJezik(myReader.GetInt32("jezik"));
red[5] = castDonator(myReader.GetInt32("donator_knjige"));
red[6] = myReader.GetDateTime("datum_upisa");
red[7] = castFormat(myReader.GetInt32("format"));
red[8] = myReader.GetInt32("broj_stranica");
red[9] = castIzdavac(myReader.GetInt32("izdavac"));
red[10] = myReader.GetInt32("godina_izdavanja");
red[11] = myReader.GetString("vrijednost_knjige");
red[12] = myReader.GetString("polica_odlaganja");
if (myReader.IsDBNull(13))
{
red[13] = "";
}
else
{
red[13] = myReader.GetString("rashod_otpis");
}
red[14] = myReader.GetInt32("inventarni_broj");
tabela.Rows.Add(red);
}
myReader.Close();
BindingSource bSource = new BindingSource();
bSource.DataSource = tabela;
dataGridView1.DataSource = bSource;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally { conDataBase.Close(); }
}

Those castXYZ methods are there because I want to show real names of writers for example, and not their ID's, they are working fine.


Here is picture of my table in MySQL: http://i.gyazo.com/0effbd5b3f55f4f64483dea911214894.png
Here are screenshoots of my error messages: http://i.gyazo.com/322869385c86f19e4bf661f8c5c02b02.png http://i.gyazo.com/d011ae9d94c7fb8e8edc82a9f3b553ae.png


I am getting really desperate because I can not find way to fix my error since my program is working fine in Visual Studio and when I debug it I can't get any mistakes. If u r asking why I am opening my program from bin/Debug directory, I need to install my program on other computer via Advanced Installer. I just need to get rid of this annoying error. If here is some good soul, please help me.



I am trying to make C# WinForm application which is connected to my MySQL database and I am displaying content from my table "knjiga" into my datagridview so user can enter new data etc. When I am pressing CTRL+F5 to run my program my form is running perfectly and I have no problems. However, when I go in bin/Debug directory and I run .exe file with doubleclick I am getting error message on start: "Input string was not in correct format" and after I click OK, another error message is showing "Index was out of range. Must be non-negative and less than the size of collection. Parameter name: index." When I click on error details its showing me that problem is with my datagridview and some weird messages on line 154 and method that I have deleted and that doesnt exist anymore in my code and that method is DisableTable(). I don't know why ?


Here is my code and how I am filling my datagridview with data from MySQL table:


private string[] kolone_knjiga = new string[] { "ID knjige", "Autor knjige", "Naziv knjige", "Žanr", "Jezik", "Donator", "Datum upisa", "Format", "Broj stranica", "Izdavač", "Godina izdavanja", "Vrijednost knjige (kn)", "Polica odlaganja", "Rashod/Otpis", "Broj knjiga" };
private void showTable(string novi_query)
{
DataTable tabela = new DataTable();
foreach (string x in kolone_knjiga) { tabela.Columns.Add(x); }
MySqlConnection conDataBase = new MySqlConnection(conString);
MySqlCommand cmdDataBase = new MySqlCommand(novi_query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
DataRow red = tabela.NewRow();
red[0] = myReader.GetString("id_knjige");
red[1] = castAutor(myReader.GetInt32("autor_knjige"));
red[2] = myReader.GetString("naziv_knjige");
red[3] = castZanr(myReader.GetInt32("zanr"));
red[4] = castJezik(myReader.GetInt32("jezik"));
red[5] = castDonator(myReader.GetInt32("donator_knjige"));
red[6] = myReader.GetDateTime("datum_upisa");
red[7] = castFormat(myReader.GetInt32("format"));
red[8] = myReader.GetInt32("broj_stranica");
red[9] = castIzdavac(myReader.GetInt32("izdavac"));
red[10] = myReader.GetInt32("godina_izdavanja");
red[11] = myReader.GetString("vrijednost_knjige");
red[12] = myReader.GetString("polica_odlaganja");
if (myReader.IsDBNull(13))
{
red[13] = "";
}
else
{
red[13] = myReader.GetString("rashod_otpis");
}
red[14] = myReader.GetInt32("inventarni_broj");
tabela.Rows.Add(red);
}
myReader.Close();
BindingSource bSource = new BindingSource();
bSource.DataSource = tabela;
dataGridView1.DataSource = bSource;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally { conDataBase.Close(); }
}

Those castXYZ methods are there because I want to show real names of writers for example, and not their ID's, they are working fine.


Here is picture of my table in MySQL: http://i.gyazo.com/0effbd5b3f55f4f64483dea911214894.png
Here are screenshoots of my error messages: http://i.gyazo.com/322869385c86f19e4bf661f8c5c02b02.png http://i.gyazo.com/d011ae9d94c7fb8e8edc82a9f3b553ae.png


I am getting really desperate because I can not find way to fix my error since my program is working fine in Visual Studio and when I debug it I can't get any mistakes. If u r asking why I am opening my program from bin/Debug directory, I need to install my program on other computer via Advanced Installer. I just need to get rid of this annoying error. If here is some good soul, please help me.


0 commentaires:

Enregistrer un commentaire