473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Save a DWORD to SQL

I need to save a DWORD to the sql server, the below posts an error, any
suggestions on what I am doing wrong. I have the column in the sql server
defined as an int since unsigned int is not valid. Also trying to avoid
setting it to a bigint in the server. Casting an int to an uint use to work
in C++.
System.Data.SqlClient.SqlConnection cn = new
System.Data.SqlClient.SqlConnection(...);
cn.Open()
System.Data.SqlClient.SqlCommand cmd = new
System.Data.SqlClient.SqlCommand();
cmd.Connection = cn;

cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
cmd.Parameters["@PS"].Value = UInt32.MaxValue;
cmd.CommandText = "UPDATE Directories SET State = @PS WHERE AreaCode = ''
AND Number = '104'";

cmd.ExecuteNonQuery(); // error here !!!!
// An unhandled exception of
type 'System.OverflowException' occurred in system.data.dll
// Additional information: Value
was either too large or too small for an Int32.
cmd.Parameters.RemoveAt("@PS");
cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode = '' AND
Number = '104'";
System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here if I
force the value in
}
dr.Close();
cn.Close();

This works sort of:

UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND Number =
'104'
SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND
Number = '104'

So I know the SQL server is storing the data correctly, the problem is .NET
can not convert it. Also if I look at the debuging code the system shows
this on the return value:

- dr["State"] {0xffffffff} System.Int32
+ System.ValueType {System.Int32} System.ValueType
m_value 0xffffffff int
MaxValue 0x7fffffff int
MinValue 0x80000000 int
Thanks,
John
Nov 15 '05 #1
7 4117

Hi John,

Just as the error message said, "Value was either too large or too small
for an Int32".
The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is 2147483647,
so the value your set is too large.
You should set a value in the range of the Int32

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "John J. Hughes II" <no@invalid.com>
| Subject: Save a DWORD to SQL
| Date: Fri, 17 Oct 2003 15:37:37 -0400
| Lines: 60
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:192183
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I need to save a DWORD to the sql server, the below posts an error, any
| suggestions on what I am doing wrong. I have the column in the sql server
| defined as an int since unsigned int is not valid. Also trying to avoid
| setting it to a bigint in the server. Casting an int to an uint use to
work
| in C++.
|
|
| System.Data.SqlClient.SqlConnection cn = new
| System.Data.SqlClient.SqlConnection(...);
| cn.Open()
| System.Data.SqlClient.SqlCommand cmd = new
| System.Data.SqlClient.SqlCommand();
| cmd.Connection = cn;
|
| cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| cmd.CommandText = "UPDATE Directories SET State = @PS WHERE AreaCode =
''
| AND Number = '104'";
|
| cmd.ExecuteNonQuery(); // error here !!!!
| // An unhandled exception of
| type 'System.OverflowException' occurred in system.data.dll
| // Additional information:
Value
| was either too large or too small for an Int32.
|
|
| cmd.Parameters.RemoveAt("@PS");
| cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode = ''
AND
| Number = '104'";
| System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| if(dr.Read())
| {
| uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here if I
| force the value in
| }
| dr.Close();
| cn.Close();
|
| This works sort of:
|
| UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND Number =
| '104'
| SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND
| Number = '104'
|
| So I know the SQL server is storing the data correctly, the problem is
.NET
| can not convert it. Also if I look at the debuging code the system shows
| this on the return value:
|
| - dr["State"] {0xffffffff} System.Int32
| + System.ValueType {System.Int32} System.ValueType
| m_value 0xffffffff int
| MaxValue 0x7fffffff int
| MinValue 0x80000000 int
|
|
| Thanks,
| John
|
|
|

Nov 15 '05 #2
No it does not help at all! I understood the error, my question was how to
get a DWORD into SQL? SQL does not support unsigned int but this was never
a problem until I started using .NET. In the past I have just forced the
data to int and then back again. Since both an int and a unsigned int are
the same size this should not be a problem. The problem is the .NET runtime
is doing an error check on the value and will not allow me to convert it.

Regards,
John
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:r0**************@cpmsftngxa06.phx.gbl...

Hi John,

Just as the error message said, "Value was either too large or too small
for an Int32".
The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is 2147483647,
so the value your set is too large.
You should set a value in the range of the Int32

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "John J. Hughes II" <no@invalid.com>
| Subject: Save a DWORD to SQL
| Date: Fri, 17 Oct 2003 15:37:37 -0400
| Lines: 60
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:192183 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I need to save a DWORD to the sql server, the below posts an error, any
| suggestions on what I am doing wrong. I have the column in the sql server | defined as an int since unsigned int is not valid. Also trying to avoid
| setting it to a bigint in the server. Casting an int to an uint use to
work
| in C++.
|
|
| System.Data.SqlClient.SqlConnection cn = new
| System.Data.SqlClient.SqlConnection(...);
| cn.Open()
| System.Data.SqlClient.SqlCommand cmd = new
| System.Data.SqlClient.SqlCommand();
| cmd.Connection = cn;
|
| cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| cmd.CommandText = "UPDATE Directories SET State = @PS WHERE AreaCode = ''
| AND Number = '104'";
|
| cmd.ExecuteNonQuery(); // error here !!!!
| // An unhandled exception of
| type 'System.OverflowException' occurred in system.data.dll
| // Additional information:
Value
| was either too large or too small for an Int32.
|
|
| cmd.Parameters.RemoveAt("@PS");
| cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode = ''
AND
| Number = '104'";
| System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| if(dr.Read())
| {
| uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here if I | force the value in
| }
| dr.Close();
| cn.Close();
|
| This works sort of:
|
| UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND Number = | '104'
| SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND
| Number = '104'
|
| So I know the SQL server is storing the data correctly, the problem is
NET
| can not convert it. Also if I look at the debuging code the system shows | this on the return value:
|
| - dr["State"] {0xffffffff} System.Int32
| + System.ValueType {System.Int32} System.ValueType
| m_value 0xffffffff int
| MaxValue 0x7fffffff int
| MinValue 0x80000000 int
|
|
| Thanks,
| John
|
|
|

Nov 15 '05 #3
John J. Hughes II wrote:
No it does not help at all! I understood the error, my question was how to
get a DWORD into SQL? SQL does not support unsigned int but this was never
a problem until I started using .NET. In the past I have just forced the
data to int and then back again. Since both an int and a unsigned int are
the same size this should not be a problem. The problem is the .NET runtime
is doing an error check on the value and will not allow me to convert it.

Regards,
John
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:r0**************@cpmsftngxa06.phx.gbl...
Hi John,

Just as the error message said, "Value was either too large or too small
for an Int32".
The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is 2147483647,
so the value your set is too large.
You should set a value in the range of the Int32

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "John J. Hughes II" <no@invalid.com>
| Subject: Save a DWORD to SQL
| Date: Fri, 17 Oct 2003 15:37:37 -0400
| Lines: 60
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl


microsoft.public.dotnet.languages.csharp:192183
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I need to save a DWORD to the sql server, the below posts an error, any
| suggestions on what I am doing wrong. I have the column in the sql


server
| defined as an int since unsigned int is not valid. Also trying to avoid
| setting it to a bigint in the server. Casting an int to an uint use to
work
| in C++.
|
|
| System.Data.SqlClient.SqlConnection cn = new
| System.Data.SqlClient.SqlConnection(...);
| cn.Open()
| System.Data.SqlClient.SqlCommand cmd = new
| System.Data.SqlClient.SqlCommand();
| cmd.Connection = cn;
|
| cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| cmd.CommandText = "UPDATE Directories SET State = @PS WHERE AreaCode


=
''
| AND Number = '104'";
|
| cmd.ExecuteNonQuery(); // error here !!!!
| // An unhandled exception of
| type 'System.OverflowException' occurred in system.data.dll
| // Additional information:
Value
| was either too large or too small for an Int32.
|
|
| cmd.Parameters.RemoveAt("@PS");
| cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode = ''
AND
| Number = '104'";
| System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| if(dr.Read())
| {
| uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here if


I
| force the value in
| }
| dr.Close();
| cn.Close();
|
| This works sort of:
|
| UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND Number


=
| '104'
| SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND
| Number = '104'
|
| So I know the SQL server is storing the data correctly, the problem is
NET
| can not convert it. Also if I look at the debuging code the system


shows
| this on the return value:
|
| - dr["State"] {0xffffffff} System.Int32
| + System.ValueType {System.Int32} System.ValueType
| m_value 0xffffffff int
| MaxValue 0x7fffffff int
| MinValue 0x80000000 int
|
|
| Thanks,
| John
|
|
|



Can you try either a SqlDbType.BigInt or SqlDbType.Variant?

--
Girish Bharadwaj

Nov 15 '05 #4

Hi John,

Thanks for your feedback.
In C++, the code is unmanged, you can do the memory operation yourself, but
it is easy for crash and overflow.
In .Net, all codes are managed, and it will check the type, so you can not
use the Int type's sign bit as UInt's data bit.
This is the feature of .Net Framework.

I think you should use the bigint in SqlServer. As you said, you want to
avoid use bigint, what is your concern?
Does this affect your design?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "John J. Hughes II" <no@invalid.com>
| References: <eP**************@TK2MSFTNGP09.phx.gbl>
<r0**************@cpmsftngxa06.phx.gbl>
| Subject: Re: Save a DWORD to SQL
| Date: Mon, 20 Oct 2003 09:23:05 -0400
| Lines: 122
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#q**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-184-105.asm.bellsouth.net 66.20.184.105
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:192591
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| No it does not help at all! I understood the error, my question was how
to
| get a DWORD into SQL? SQL does not support unsigned int but this was
never
| a problem until I started using .NET. In the past I have just forced the
| data to int and then back again. Since both an int and a unsigned int are
| the same size this should not be a problem. The problem is the .NET
runtime
| is doing an error check on the value and will not allow me to convert it.
|
| Regards,
| John
|
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:r0**************@cpmsftngxa06.phx.gbl...
| >
| > Hi John,
| >
| > Just as the error message said, "Value was either too large or too small
| > for an Int32".
| > The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is
2147483647,
| > so the value your set is too large.
| > You should set a value in the range of the Int32
| >
| > Hope this helps,
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "John J. Hughes II" <no@invalid.com>
| > | Subject: Save a DWORD to SQL
| > | Date: Fri, 17 Oct 2003 15:37:37 -0400
| > | Lines: 60
| > | Organization: Function International
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:192183
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I need to save a DWORD to the sql server, the below posts an error,
any
| > | suggestions on what I am doing wrong. I have the column in the sql
| server
| > | defined as an int since unsigned int is not valid. Also trying to
avoid
| > | setting it to a bigint in the server. Casting an int to an uint use
to
| > work
| > | in C++.
| > |
| > |
| > | System.Data.SqlClient.SqlConnection cn = new
| > | System.Data.SqlClient.SqlConnection(...);
| > | cn.Open()
| > | System.Data.SqlClient.SqlCommand cmd = new
| > | System.Data.SqlClient.SqlCommand();
| > | cmd.Connection = cn;
| > |
| > | cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| > | cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| > | cmd.CommandText = "UPDATE Directories SET State = @PS WHERE
AreaCode
| =
| > ''
| > | AND Number = '104'";
| > |
| > | cmd.ExecuteNonQuery(); // error here !!!!
| > | // An unhandled exception
of
| > | type 'System.OverflowException' occurred in system.data.dll
| > | // Additional information:
| > Value
| > | was either too large or too small for an Int32.
| > |
| > |
| > | cmd.Parameters.RemoveAt("@PS");
| > | cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode =
''
| > AND
| > | Number = '104'";
| > | System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| > | if(dr.Read())
| > | {
| > | uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here
if
| I
| > | force the value in
| > | }
| > | dr.Close();
| > | cn.Close();
| > |
| > | This works sort of:
| > |
| > | UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND
Number
| =
| > | '104'
| > | SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND
| > | Number = '104'
| > |
| > | So I know the SQL server is storing the data correctly, the problem is
| > NET
| > | can not convert it. Also if I look at the debuging code the system
| shows
| > | this on the return value:
| > |
| > | - dr["State"] {0xffffffff} System.Int32
| > | + System.ValueType {System.Int32} System.ValueType
| > | m_value 0xffffffff int
| > | MaxValue 0x7fffffff int
| > | MinValue 0x80000000 int
| > |
| > |
| > | Thanks,
| > | John
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #5
Thanks of your response. I am trying to avoid big int mostly because I have
some rather large systems installed and don't want to run a convert on the
records. The second reason would have to deal with trying to avoid the
rather large system becoming even larger. int 4 bytes vis bit int 8
bytes... I have tables with more then a few so the records would almost
double in size. And lastly I receive the data as a DWORD and send it that
way which is not really a problem but it adds to the mess.

Now I had though about casting it to a big int but the value still comes in
a -1 which is not correct or useful.

I have the following code which works but as you can imagine it does add
some overhead and does not work with default .NET functions.

static public Int32 ToInt32(UInt32 v)
{
byte[] tmp = new byte[4];

tmp[0] = (byte)(v & 0x000000ff);
tmp[1] = (byte)(v & 0x0000ff00 >> 8);
tmp[2] = (byte)(v & 0x00ff0000 >> 16);
tmp[3] = (byte)(v & 0xff000000 >> 24);

return BitConverter.ToInt32(tmp, 0);
}

static public UInt32 ToUInt32(Int32 v)
{
byte[] tmp = new byte[4];

tmp[0] = (byte)(v & 0x000000ff);
tmp[1] = (byte)(v & 0x0000ff00 >> 8);
tmp[2] = (byte)(v & 0x00ff0000 >> 16);
tmp[3] = (byte)(v & 0xff000000 >> 24);

return BitConverter.ToUInt32(tmp, 0);
}
Regards,
John
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:%2****************@cpmsftngxa06.phx.gbl...

Hi John,

Thanks for your feedback.
In C++, the code is unmanged, you can do the memory operation yourself, but it is easy for crash and overflow.
In .Net, all codes are managed, and it will check the type, so you can not
use the Int type's sign bit as UInt's data bit.
This is the feature of .Net Framework.

I think you should use the bigint in SqlServer. As you said, you want to
avoid use bigint, what is your concern?
Does this affect your design?

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "John J. Hughes II" <no@invalid.com>
| References: <eP**************@TK2MSFTNGP09.phx.gbl>
<r0**************@cpmsftngxa06.phx.gbl>
| Subject: Re: Save a DWORD to SQL
| Date: Mon, 20 Oct 2003 09:23:05 -0400
| Lines: 122
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#q**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-184-105.asm.bellsouth.net 66.20.184.105
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:192591 | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| No it does not help at all! I understood the error, my question was how
to
| get a DWORD into SQL? SQL does not support unsigned int but this was
never
| a problem until I started using .NET. In the past I have just forced the | data to int and then back again. Since both an int and a unsigned int are | the same size this should not be a problem. The problem is the .NET
runtime
| is doing an error check on the value and will not allow me to convert it. |
| Regards,
| John
|
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:r0**************@cpmsftngxa06.phx.gbl...
| >
| > Hi John,
| >
| > Just as the error message said, "Value was either too large or too small | > for an Int32".
| > The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is
2147483647,
| > so the value your set is too large.
| > You should set a value in the range of the Int32
| >
| > Hope this helps,
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "John J. Hughes II" <no@invalid.com>
| > | Subject: Save a DWORD to SQL
| > | Date: Fri, 17 Oct 2003 15:37:37 -0400
| > | Lines: 60
| > | Organization: Function International
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:192183
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | I need to save a DWORD to the sql server, the below posts an error,
any
| > | suggestions on what I am doing wrong. I have the column in the sql
| server
| > | defined as an int since unsigned int is not valid. Also trying to
avoid
| > | setting it to a bigint in the server. Casting an int to an uint use
to
| > work
| > | in C++.
| > |
| > |
| > | System.Data.SqlClient.SqlConnection cn = new
| > | System.Data.SqlClient.SqlConnection(...);
| > | cn.Open()
| > | System.Data.SqlClient.SqlCommand cmd = new
| > | System.Data.SqlClient.SqlCommand();
| > | cmd.Connection = cn;
| > |
| > | cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| > | cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| > | cmd.CommandText = "UPDATE Directories SET State = @PS WHERE
AreaCode
| =
| > ''
| > | AND Number = '104'";
| > |
| > | cmd.ExecuteNonQuery(); // error here !!!!
| > | // An unhandled exception of
| > | type 'System.OverflowException' occurred in system.data.dll
| > | // Additional information: | > Value
| > | was either too large or too small for an Int32.
| > |
| > |
| > | cmd.Parameters.RemoveAt("@PS");
| > | cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode =
''
| > AND
| > | Number = '104'";
| > | System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| > | if(dr.Read())
| > | {
| > | uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here if
| I
| > | force the value in
| > | }
| > | dr.Close();
| > | cn.Close();
| > |
| > | This works sort of:
| > |
| > | UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND
Number
| =
| > | '104'
| > | SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND | > | Number = '104'
| > |
| > | So I know the SQL server is storing the data correctly, the problem is | > NET
| > | can not convert it. Also if I look at the debuging code the system
| shows
| > | this on the return value:
| > |
| > | - dr["State"] {0xffffffff} System.Int32
| > | + System.ValueType {System.Int32} System.ValueType
| > | m_value 0xffffffff int
| > | MaxValue 0x7fffffff int
| > | MinValue 0x80000000 int
| > |
| > |
| > | Thanks,
| > | John
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #6

"Girish Bharadwaj" <girishb@nowhere> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
John J. Hughes II wrote:
No it does not help at all! I understood the error, my question was how to get a DWORD into SQL? SQL does not support unsigned int but this was never a problem until I started using .NET. In the past I have just forced the data to int and then back again. Since both an int and a unsigned int are the same size this should not be a problem. The problem is the .NET runtime is doing an error check on the value and will not allow me to convert it.
Regards,
John
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:r0**************@cpmsftngxa06.phx.gbl...
Hi John,

Just as the error message said, "Value was either too large or too small
for an Int32".
The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is 2147483647,so the value your set is too large.
You should set a value in the range of the Int32

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "John J. Hughes II" <no@invalid.com>
| Subject: Save a DWORD to SQL
| Date: Fri, 17 Oct 2003 15:37:37 -0400
| Lines: 60
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl


microsoft.public.dotnet.languages.csharp:192183
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I need to save a DWORD to the sql server, the below posts an error, any| suggestions on what I am doing wrong. I have the column in the sql


server
| defined as an int since unsigned int is not valid. Also trying to avoid| setting it to a bigint in the server. Casting an int to an uint use towork
| in C++.
|
|
| System.Data.SqlClient.SqlConnection cn = new
| System.Data.SqlClient.SqlConnection(...);
| cn.Open()
| System.Data.SqlClient.SqlCommand cmd = new
| System.Data.SqlClient.SqlCommand();
| cmd.Connection = cn;
|
| cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| cmd.CommandText = "UPDATE Directories SET State = @PS WHERE AreaCode

=
''
| AND Number = '104'";
|
| cmd.ExecuteNonQuery(); // error here !!!!
| // An unhandled exception
of| type 'System.OverflowException' occurred in system.data.dll
| // Additional information:
Value
| was either too large or too small for an Int32.
|
|
| cmd.Parameters.RemoveAt("@PS");
| cmd.CommandText = "SELECT State FROM Directories WHERE AreaCode = ''AND
| Number = '104'";
| System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| if(dr.Read())
| {
| uint val = Convert.ToUInt32(dr["PhoneState"]); // same error here

if
I
| force the value in
| }
| dr.Close();
| cn.Close();
|
| This works sort of:
|
| UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND
Number
=
| '104'
| SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = '' AND
| Number = '104'
|
| So I know the SQL server is storing the data correctly, the problem is
NET
| can not convert it. Also if I look at the debuging code the system


shows
| this on the return value:
|
| - dr["State"] {0xffffffff} System.Int32
| + System.ValueType {System.Int32} System.ValueType
| m_value 0xffffffff int
| MaxValue 0x7fffffff int
| MinValue 0x80000000 int
|
|
| Thanks,
| John
|
|
|



Can you try either a SqlDbType.BigInt or SqlDbType.Variant?

--
Girish Bharadwaj


I was trying not to convert existing databases or make them larger by using
big int... I might look at variant, since I have never used it before I am
not sure if it's an option.

Thanks,
John
Nov 15 '05 #7

Hi John,

I think your workaround of converting the UInt32 to Int32 programmatic is a
good solution, but it will make your application complex.
While using the bigint, you program logic is simple, but the database is
bigger.
So I think you should count the cost between these 2 ways.

Thanks for your feedback.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "John J. Hughes II" <no@invalid.com>
| References: <eP**************@TK2MSFTNGP09.phx.gbl>
<r0**************@cpmsftngxa06.phx.gbl>
<#q**************@TK2MSFTNGP10.phx.gbl>
<#C**************@cpmsftngxa06.phx.gbl>
| Subject: Re: Save a DWORD to SQL
| Date: Tue, 21 Oct 2003 11:54:13 -0400
| Lines: 236
| Organization: Function International
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <OW**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: adsl-20-177-139.asm.bellsouth.net 66.20.177.139
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:192911
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks of your response. I am trying to avoid big int mostly because I
have
| some rather large systems installed and don't want to run a convert on the
| records. The second reason would have to deal with trying to avoid the
| rather large system becoming even larger. int 4 bytes vis bit int 8
| bytes... I have tables with more then a few so the records would almost
| double in size. And lastly I receive the data as a DWORD and send it that
| way which is not really a problem but it adds to the mess.
|
| Now I had though about casting it to a big int but the value still comes
in
| a -1 which is not correct or useful.
|
| I have the following code which works but as you can imagine it does add
| some overhead and does not work with default .NET functions.
|
| static public Int32 ToInt32(UInt32 v)
| {
| byte[] tmp = new byte[4];
|
| tmp[0] = (byte)(v & 0x000000ff);
| tmp[1] = (byte)(v & 0x0000ff00 >> 8);
| tmp[2] = (byte)(v & 0x00ff0000 >> 16);
| tmp[3] = (byte)(v & 0xff000000 >> 24);
|
| return BitConverter.ToInt32(tmp, 0);
| }
|
| static public UInt32 ToUInt32(Int32 v)
| {
| byte[] tmp = new byte[4];
|
| tmp[0] = (byte)(v & 0x000000ff);
| tmp[1] = (byte)(v & 0x0000ff00 >> 8);
| tmp[2] = (byte)(v & 0x00ff0000 >> 16);
| tmp[3] = (byte)(v & 0xff000000 >> 24);
|
| return BitConverter.ToUInt32(tmp, 0);
| }
|
|
| Regards,
| John
|
|
| ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| news:%2****************@cpmsftngxa06.phx.gbl...
| >
| > Hi John,
| >
| > Thanks for your feedback.
| > In C++, the code is unmanged, you can do the memory operation yourself,
| but
| > it is easy for crash and overflow.
| > In .Net, all codes are managed, and it will check the type, so you can
not
| > use the Int type's sign bit as UInt's data bit.
| > This is the feature of .Net Framework.
| >
| > I think you should use the bigint in SqlServer. As you said, you want to
| > avoid use bigint, what is your concern?
| > Does this affect your design?
| >
| > Thanks
| >
| > Best regards,
| > Jeffrey Tan
| > Microsoft Online Partner Support
| > Get Secure! - www.microsoft.com/security
| > This posting is provided "as is" with no warranties and confers no
rights.
| >
| > --------------------
| > | From: "John J. Hughes II" <no@invalid.com>
| > | References: <eP**************@TK2MSFTNGP09.phx.gbl>
| > <r0**************@cpmsftngxa06.phx.gbl>
| > | Subject: Re: Save a DWORD to SQL
| > | Date: Mon, 20 Oct 2003 09:23:05 -0400
| > | Lines: 122
| > | Organization: Function International
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | Message-ID: <#q**************@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | NNTP-Posting-Host: adsl-20-184-105.asm.bellsouth.net 66.20.184.105
| > | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| > | Xref: cpmsftngxa06.phx.gbl
| microsoft.public.dotnet.languages.csharp:192591
| > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > |
| > | No it does not help at all! I understood the error, my question was
how
| > to
| > | get a DWORD into SQL? SQL does not support unsigned int but this was
| > never
| > | a problem until I started using .NET. In the past I have just forced
| the
| > | data to int and then back again. Since both an int and a unsigned int
| are
| > | the same size this should not be a problem. The problem is the .NET
| > runtime
| > | is doing an error check on the value and will not allow me to convert
| it.
| > |
| > | Regards,
| > | John
| > |
| > |
| > | ""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
| > | news:r0**************@cpmsftngxa06.phx.gbl...
| > | >
| > | > Hi John,
| > | >
| > | > Just as the error message said, "Value was either too large or too
| small
| > | > for an Int32".
| > | > The UInt32.MaxValue is 4294967295, while the Int32.MaxValue is
| > 2147483647,
| > | > so the value your set is too large.
| > | > You should set a value in the range of the Int32
| > | >
| > | > Hope this helps,
| > | >
| > | > Best regards,
| > | > Jeffrey Tan
| > | > Microsoft Online Partner Support
| > | > Get Secure! - www.microsoft.com/security
| > | > This posting is provided "as is" with no warranties and confers no
| > rights.
| > | >
| > | > --------------------
| > | > | From: "John J. Hughes II" <no@invalid.com>
| > | > | Subject: Save a DWORD to SQL
| > | > | Date: Fri, 17 Oct 2003 15:37:37 -0400
| > | > | Lines: 60
| > | > | Organization: Function International
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| > | > | Message-ID: <eP**************@TK2MSFTNGP09.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.languages.csharp
| > | > | NNTP-Posting-Host: adsl-20-184-22.asm.bellsouth.net 66.20.184.22
| > | > | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP09.phx.gbl
| > | > | Xref: cpmsftngxa06.phx.gbl
| > | microsoft.public.dotnet.languages.csharp:192183
| > | > | X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
| > | > |
| > | > | I need to save a DWORD to the sql server, the below posts an
error,
| > any
| > | > | suggestions on what I am doing wrong. I have the column in the
sql
| > | server
| > | > | defined as an int since unsigned int is not valid. Also trying to
| > avoid
| > | > | setting it to a bigint in the server. Casting an int to an uint
use
| > to
| > | > work
| > | > | in C++.
| > | > |
| > | > |
| > | > | System.Data.SqlClient.SqlConnection cn = new
| > | > | System.Data.SqlClient.SqlConnection(...);
| > | > | cn.Open()
| > | > | System.Data.SqlClient.SqlCommand cmd = new
| > | > | System.Data.SqlClient.SqlCommand();
| > | > | cmd.Connection = cn;
| > | > |
| > | > | cmd.Parameters.Add("@PS", System.Data.SqlDbType.Int);
| > | > | cmd.Parameters["@PS"].Value = UInt32.MaxValue;
| > | > | cmd.CommandText = "UPDATE Directories SET State = @PS WHERE
| > AreaCode
| > | =
| > | > ''
| > | > | AND Number = '104'";
| > | > |
| > | > | cmd.ExecuteNonQuery(); // error here !!!!
| > | > | // An unhandled
| exception
| > of
| > | > | type 'System.OverflowException' occurred in system.data.dll
| > | > | // Additional
| information:
| > | > Value
| > | > | was either too large or too small for an Int32.
| > | > |
| > | > |
| > | > | cmd.Parameters.RemoveAt("@PS");
| > | > | cmd.CommandText = "SELECT State FROM Directories WHERE
AreaCode =
| > ''
| > | > AND
| > | > | Number = '104'";
| > | > | System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
| > | > | if(dr.Read())
| > | > | {
| > | > | uint val = Convert.ToUInt32(dr["PhoneState"]); // same error
| here
| > if
| > | I
| > | > | force the value in
| > | > | }
| > | > | dr.Close();
| > | > | cn.Close();
| > | > |
| > | > | This works sort of:
| > | > |
| > | > | UPDATE Directories SET State = 0xffffffff WHERE AreaCode = '' AND
| > Number
| > | =
| > | > | '104'
| > | > | SELECT CONVERT(Binary, State) FROM Directories WHERE AreaCode = ''
| AND
| > | > | Number = '104'
| > | > |
| > | > | So I know the SQL server is storing the data correctly, the
problem
| is
| > | > NET
| > | > | can not convert it. Also if I look at the debuging code the
system
| > | shows
| > | > | this on the return value:
| > | > |
| > | > | - dr["State"] {0xffffffff} System.Int32
| > | > | + System.ValueType {System.Int32} System.ValueType
| > | > | m_value 0xffffffff int
| > | > | MaxValue 0x7fffffff int
| > | > | MinValue 0x80000000 int
| > | > |
| > | > |
| > | > | Thanks,
| > | > | John
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lee K | last post by:
This is not strictly a C++ issue but if anyone will know the answer its you guys! I am trying to figure out a date format stored in the registry by a piece of software (I am trying to write...
7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
7
by: ±èº´¼®\( Kevin Kim \) | last post by:
Hi all __gc class Test { }; void Temp( void ) { Test *pTest = new Test;
2
by: Vladimir_petter | last post by:
Hello All, I've fount that if I compile the same program using gcc and vc 2003 the same class E (see complete source bellow) has different size (on vc it is 4 bytes bigger). Digging into this...
1
by: Gabest | last post by:
Running this piece of code while having the sse optimization turned on (vcnet2003), something really strange happens I cannot explain. Without sse it is giving me the right results. float f =...
0
by: prakash | last post by:
Dear Friends I am new guy to Visual C++.NET I've program to save website as a image vc++.net . It have a function "SaveSnapshot" to save the webpage as an image On that function ifor saving...
7
by: monkeydragon | last post by:
how to would you transfer a DWORD variable from inside of a function to the caller ex. InvokeProcessData(LPDWORD prtDW) { ... // we have created, initialized and processed // dword variable...
4
by: Virajitha Sarma | last post by:
Hi, I have a code in C which i am rewritting it in C#. I am facing problem with the following two lines : char *cipher; (DWORD*)cipher(C) and (uint*)cipher(C#) are giving different values...
2
by: kubrinsky | last post by:
Hi all ! I have a small trouble with iterators... Can you help me please. The problem: typedef std::map<int, intM; M m; M::iterator i = m.begin();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.