When running the code below I got error when casting from a number, the value must be a number less than infinity:
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow))
{
if (reader.Read())
{
dokument = new Dokument(reader.GetInt32(0),
reader.GetDateTime(1),
reader.GetString(2),
reader.GetString(3),
reader.GetDecimal(4),
reader.GetString(5)[0],
reader.GetDecimal(6),
reader.GetDecimal(7),
reader.GetInt32(8),
reader.GetString(9),
reader.GetString(10));
}
}
Can anybody help me please where I go wrong?
Thanks
Loading

VulpesRank #55 · 29.2K reputationPosted Aug 8, 2011, 5:05 AM EDT
Are you able to pinpoint with the debugger on which line the error is occurring?
NelRank #2,060 · 702 reputationPosted Aug 8, 2011, 11:05 PM EDT
dokument = new Dokument(reader.GetInt32(0),
reader.GetDateTime(1),
reader.GetString(2),
reader.GetString(3),
reader.GetDecimal(4),
reader.GetString(5)[0],
reader.GetDecimal(6),
reader.GetDecimal(7),
reader.GetInt32(8),
reader.GetString(9),
reader.GetString(10));
VulpesRank #55 · 29.2K reputationPosted Aug 9, 2011, 12:01 AM EDT
Rather than use GetInt32 or GetDecimal to read that column you'll then need to use GetDouble and adjust the parameter types to the Dokument constructor accordingly, to avoid the error.
NelRank #2,060 · 702 reputationPosted Aug 9, 2011, 2:58 AM EDT
NelRank #2,060 · 702 reputationPosted Aug 9, 2011, 3:35 AM EDT
public Dokument GetDokument(int brdok)
{
Dokument dokument = null;
SqlConnection connection = null;
try
{
// Open a database connection.
connection = GetConnection();
connection.Open();
// Create a command.
SqlCommand command = new SqlCommand("prikDokument", connection);
command.CommandType = CommandType.StoredProcedure;
// Add parameters to command.
command.Parameters.AddWithValue("@brdok", brdok);
// Execute the command.
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow))
{
if (reader.Read())
{
dokument = new Dokument(reader.GetInt32(0),
reader.GetDateTime(1),
reader.GetString(2),
reader.GetString(3),
reader.GetDecimal(4),
reader.GetString(5)[0],
reader.GetDecimal(6),
reader.GetDecimal(7),
reader.GetInt32(8),
reader.GetString(9),
reader.GetString(10));
}
}
}
finally
{
if (connection != null)
{
connection.Close();
}
}
return dokument;
}
and it is beeing called by this procedure:
private void btnPrikazi_Click(object sender, EventArgs e)
{
ClearDokumentForm();
string brdok = this.txtBrdok.Text;
if (String.IsNullOrEmpty(brdok))
{
MessageBox.Show("msg1.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
try
{
alatkiDAC dac = new alatkiDAC();
Dokument dokument = dac.GetDokument(int.Parse(brdok));
if (dokument == null)
{
MessageBox.Show("msg.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
SetFormForExistingDokument();
txtVP.Text = dokument.vp;
txtImeprez.Text = dokument.imeprezime;
txtIznos.Text = Convert.ToString(dokument.iznos);
txtIznDen.Text = Convert.ToString (dokument.iznosden);
txtValutakurs.Text =Convert.ToString(dokument.valuta_kurs);
txtBrkiz.Text = Convert.ToString(dokument.br_kas_izvod);
txtPatsmet.Text = dokument.br_pat_smetka;
txttip.Text = dokument.tip;
datePicker.Value = dokument.datum;
if (dokument.valuta == 'E')
radioButton1.Checked=true;
else
radioButton2.Checked=true;
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
The bold part of the code is highlited.
Does anybody have an idea what is the problem?
Thanks
VulpesRank #55 · 29.2K reputationPosted Aug 9, 2011, 7:01 AM EDT
NelRank #2,060 · 702 reputationPosted Aug 9, 2011, 10:38 PM EDT
2. The date doesn't seem to be null, and I also tried the code that you've posted, but I got the same error on the same place in the code again.
So it doesn't work.
VulpesRank #55 · 29.2K reputationPosted Aug 10, 2011, 12:56 AM EDT
Are you sure that there are no other columns in that row which are NULL?
If you are sure, then can you post the values of each column in that row, please, together with their types.
NelRank #2,060 · 702 reputationPosted Aug 10, 2011, 2:06 AM EDT
brdok vp valuta valuta_kurs imeprezime iznos iznosden
1 sdfs E 36.80 szfdszdf 25.40 934.72
2 dfgxdg E 40.20 dfgdxfx 50.60 2034.12
datum br_kas_izvod br_pat_smetka tip
2011-08-10 4 fgghfx fxghg
2011-08-10 4 sdgsz gzs
and the types are:
brdok int identity,
vp nvarchar(50),
valuta char(3),
valuta_kurs decimal(4,2),
imeprezime nvarchar(60),
iznos decimal(10,2),
iznosden decimal(10,2),
datum date,
br_kas_izvod int,
br_pat_smetka nvarchar(60),
tip nvarchar(30)
VulpesRank #55 · 29.2K reputationPosted Aug 10, 2011, 2:37 AM EDT
The list you've just posted suggests that the 'datum' column is the 8th but your code suggests that it's the 2nd.
If that's not the problem, then I'd try and eliminate the date from consideration by seeing if you still get the error when you don't read it:
NelRank #2,060 · 702 reputationPosted Aug 10, 2011, 3:14 AM EDT
I also tried your code, but I got the same error again.
VulpesRank #55 · 29.2K reputationPosted Aug 10, 2011, 6:37 AM EDT
Although it shouldn't make any difference, you could also try omitting CommandBehavior.SingleRow as an argument to the ExecuteReader() method.
NelRank #2,060 · 702 reputationPosted Aug 10, 2011, 10:32 PM EDT
Specified cast is not valid.
at this row:
Application.Run(new Blag());
in the Program.cs. And Blag is my main windows form.
Whatever else I change now (I have returned the CommandBehavior.SingleRow) I got the same error like described above.
Satish BhatRank #2,496 · 563 reputationPosted Aug 10, 2011, 11:07 PM EDT
NelRank #2,060 · 702 reputationPosted Aug 10, 2011, 11:42 PM EDT
Satish BhatRank #2,496 · 563 reputationPosted Aug 11, 2011, 12:59 AM EDT
It is an arithmetic overflow exception [A source value cannot be infinity or Not a Number].
Try converting all GetInt32() / GetDecimal() methods of your datareader to GetString() and run the application.
If you don't get an error then, you can view the values in debug mode and find out which value is not a number.
If you still unable to figure out then
Convert one by one of the GetString() methods to respective GetInt32() / GetDecimal() methods and test step by step. Hope you will be able to find out the exact value which is giving error. One of the values you are trying to convert into numeric value is not Not a Number.
Hope it does not throw the same error in spite of converting all GetInt32() / GetDecimal() methods to GetString() :(
NelRank #2,060 · 702 reputationPosted Aug 11, 2011, 1:49 AM EDT
Specified cast is not valid.
in the Program.cs
and whichever conversion further I try, I get the same error:(
NelRank #2,060 · 702 reputationPosted Aug 11, 2011, 2:01 AM EDT
When I try to insert the decimal data, if I try to enter them with comma, it is ok, but if I insert them with dot, I got that error.
But, when I enter the data with the comma separator, when entering decimal types, when I open the database, I can see that in SQL management they are designated with dots, not commas, eventhough I inserted them with comma.
So that's why when I try to retrieve the data from the database in the text boxes maybe it is the reason I got that error, because it is the same as when trying to insert them with dot.
But now, why does that conversion in the database happen?
And what should I do now?
Can anybody help me please?
Satish BhatRank #2,496 · 563 reputationPosted Aug 11, 2011, 2:08 AM EDT
European nations use the comma as the decimal separator. American which use the dot as the decimal separator.
Ref: http://en.wikipedia.org/wiki/Decimal_separator
VulpesRank #55 · 29.2K reputationPosted Aug 11, 2011, 2:13 AM EDT
However, I'd change your code which reads the columns into the following so we can see which column (or which first column) is causing the problem:
NelRank #2,060 · 702 reputationPosted Aug 11, 2011, 4:19 AM EDT
pomoAlatki.dll!pomoAlatki.alatkiDAC.GetDokument(int brdok = 2) Line 55 C#
Bl_JT.exe!Bl_JT.Blagajna.btnPrikazi_Click(object sender = {Text = "Prikazi"}, System.EventArgs e = {X = 50 Y = 17 Button = Left}) Line 41 + 0x1e bytes C#
[External Code]
Bl_JT.exe!Bl_JT.Program.Main() Line 18 + 0x1d bytes C#
and all the values: a,b,c...are iter 0 or null, depending on the type.
VulpesRank #55 · 29.2K reputationPosted Aug 11, 2011, 5:08 AM EDT
NelRank #2,060 · 702 reputationPosted Aug 11, 2011, 10:11 PM EDT
"Specified cast is not valid."
I got in this row:
DateTime b = reader.GetDateTime(1);
I got 01.01.0001 00:00:00
the type of this field is only date not datetime in my sql database. That is how Idefined it at the beginning.
And the the parameter types of the Dokument constructor are:
public class Dokument
{
private int _brdok;
public int brdok
{
get { return _brdok; }
set { _brdok = value; }
}
private string _vp;
public string vp
{
get { return _vp; }
set { _vp = value; }
}
private string _imeprezime;
public string imeprezime
{
get { return _imeprezime; }
set { _imeprezime = value; }
}
private decimal _iznos;
public decimal iznos
{
get { return _iznos; }
set { _iznos = value; }
}
private decimal _iznosden;
public decimal iznosden
{
get { return _iznosden; }
set { _iznosden = value; }
}
private decimal _valuta_kurs;
public decimal valuta_kurs
{
get { return _valuta_kurs; }
set { _valuta_kurs = value; }
}
private char _valuta;
public char valuta
{
get { return _valuta; }
set { _valuta = value; }
}
private DateTime _datum;
public DateTime datum
{
get { return _datum; }
set { _datum = value; }
}
private int _br_kas_izvod;
public int br_kas_izvod
{
get { return _br_kas_izvod; }
set { _br_kas_izvod = value; }
}
private string _br_pat_smetka;
public string br_pat_smetka
{
get { return _br_pat_smetka; }
set { _br_pat_smetka = value; }
}
private string _tip;
public string tip
{
get { return _tip; }
set { _tip = value; }
}
public Dokument(int brdok, DateTime datum, string vp, string imeprezime, decimal iznos,char valuta,decimal valuta_kurs,decimal iznosden, int br_kas_izvod, string br_pat_smetka, string tip)
{
_brdok = brdok;
_datum = datum;
_vp = vp;
_imeprezime = imeprezime;
_iznos = iznos;
_valuta = valuta;
_valuta_kurs = valuta_kurs;
_iznosden = iznosden;
_br_kas_izvod = br_kas_izvod;
_br_pat_smetka = br_pat_smetka;
_tip = tip;
}
and now I changed the type date in datetime in the sql database
Satish BhatRank #2,496 · 563 reputationPosted Aug 11, 2011, 11:03 PM EDT
Date or Datetime datatype in SQL server should not be a problem. The value coming from a SQL Server 2008 table that uses a date column, will be mapped to DateTime in .NET.
Again I feel the problem lies in the Date seperator. Where did u get the date '01.01.0001 00:00:00'? What is the actual value of the in the Database?
NelRank #2,060 · 702 reputationPosted Aug 11, 2011, 11:35 PM EDT
Satish BhatRank #2,496 · 563 reputationPosted Aug 12, 2011, 12:57 AM EDT
SELECT FIELD1, CONVERT(VARCHAR(10),DATE_FIELD_NAME,103),FIELD_N FROM TABLE_NAME
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 2:05 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Aug 12, 2011, 2:58 AM EDT
Change DATUM to VARCHAR datatype in Stored Procedure...
Now change reader.GetDateTime(1), to reader.GetString(1),
NelRank #2,060 · 702 reputationPosted Aug 14, 2011, 10:57 PM EDT
Does anybody have another idea how to call the data from one row in the table, and all textboxes in the form to get the values from the row accordingly when the click_button event occurs?
VulpesRank #55 · 29.2K reputationPosted Aug 15, 2011, 7:31 AM EDT