sachin_wal Posted July 7, 2003 Posted July 7, 2003 Hi, I am trying to read from a smallmoney field from the database and assign it to a text box. Here is code. ............. Dim DR As System.Data.SqlClient.SqlDataReader Try Conn1.Open() DR = sqlComm.ExecuteReader If DR.Read Then residentName.Text = DR.Item("ResidentName") residentAge.Text = DR.Item("ResidentAge") ................. Dim wag As SqlTypes.SqlMoney = DR.GetSqlMoney("Wages") Dim pen As SqlTypes.SqlMoney = DR.GetSqlMoney("Pension") where Wages is of type smallmoney. I get the following error. System.InvalidCastException: Cast from string "Wages" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.DoubleType.Parse(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.IntegerType.FromString(String Value). Any pointers would be appreciated. Regards, Sachin. Quote
*Experts* Nerseus Posted July 7, 2003 *Experts* Posted July 7, 2003 Yes, you'll get the same error when using smallint. It's a pain, but if you REALLY want to use the smaller column size, you'll have to convert manually. Use something like: Dim wag As SqlTypes.SqlMoney = System.Convert.ToInt32(DR.GetSqlMoney("Wages")) Or, convince your client/boss that smallmoney isn't going to save you but 4 bytes per row. At 1 million rows that's 4 mil bytes - 4 meg of hard drive.... not worth it if you ask me. We convinced our clients that smallint wasn't worth it using the same calculation (more or less). -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
sachin_wal Posted July 7, 2003 Author Posted July 7, 2003 hi, Thanks for the suggestion. I made the following chnages and it worked fine. employer.Text = DR.Item("Employer") occupation.Text = DR.Item("Occupation") wages.Text = DR.GetSqlMoney(21).ToString Pension.Text = DR.GetSqlMoney(22).ToString I just changed the fieldname("Wages") and changed it to the corresponding field number and it started working fine.Maybe i had done something wrong. Regards, Sachin. Quote
*Experts* Nerseus Posted July 7, 2003 *Experts* Posted July 7, 2003 Well your new code is simply calling ToString on the returned object which will ALWAYS work. In the original code you were assigning a specific variable type to the value, which needed converting. Using the ordinal position versus column name would have no impact on the original error. Using the ordinal is probably slightly faster but less readable. That's about the only difference... -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
sachin_wal Posted July 9, 2003 Author Posted July 9, 2003 Re: Hi, I know that is does not make any difference and the ordinal position is just another way of accessing the fields.But when I used the same Tostring with value accessed Using "Wages" I got the same error message. But if you see the original error message it says : Cast from string "Wages" to type 'Integer' is not valid. So i guess when it tried to convert Wages into a ordinal value before reading from the table( I dont know if SQL does that) it resulted in the above error(Weird). I also double checked the name of the field(Wages). Any idea why it worked that way?. regards, Sachin. Quote
*Experts* Nerseus Posted July 9, 2003 *Experts* Posted July 9, 2003 You'll get the error because .NET doesn't want to convert smallmoney to SqlMoney, apparently. With a conversion function you should be able to get around it. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Corinne Posted July 22, 2004 Posted July 22, 2004 I'm having a similar problem. In my database (sql 2000) the field "GoalValue" is defined as money. .GoalValue is an object I'm trying to fill which when I hover over it is defined as "Public poperty GoalValue() as System.Data.SqlTypes.SQLMoney. My question is how to convert e.Row.Cells("GoalValue").Value to SQLMoney? I can't get the syntax right. Thanks for any help. Code so far: If Not IsDBNull(e.Row.Cells("GoalValue").Value) Then .GoalValue = e.Row.Cells("GoalValue").Value .Month = e.Row.Cells("Month").Value .Description = e.Row.Cells("Description").Value .Year = e.Row.Cells("Year").Value End If Quote
Joe Mamma Posted July 22, 2004 Posted July 22, 2004 I'm having a similar problem. In my database (sql 2000) the field "GoalValue" is defined as money. .GoalValue is an object I'm trying to fill which when I hover over it is defined as "Public poperty GoalValue() as System.Data.SqlTypes.SQLMoney. My question is how to convert e.Row.Cells("GoalValue").Value to SQLMoney? I can't get the syntax right. Thanks for any help. just guessing, but this should work ( C# ) if ( e.Row.Cells("GoalValue").Value != null ) GoalValue = new SQLMoney(Convert.ToDecimal(e.Row.Cells("GoalValue").Value)); Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.