Cast String to Byte (sqlsr2k tinyint)

D

dbuchanan

How do I explicitly cast a string to Byte?

Here are my circumstances;
The string is from a textbox and could be empty.
The Byte is a tinyint in SQL Server 2000
I am using the AddRow method as shown below and get an
InvalidCastException.
"Cast from string "" to type integer is not valid"

Me.DataSet.tblCustomer.AddtblCustomerRow(txtCustomerName.Text.ToString,
CByte(CInt(txtJobType.Text)), )
 
G

Guest

I don't think you can cast a string to a single byte, but you can convert a
string into an array of bytes by calling

System.Text.ASCIIEncoding.ASCII.GetBytes(myString)
 
T

Tom Shelton

How do I explicitly cast a string to Byte?

Here are my circumstances;
The string is from a textbox and could be empty.
The Byte is a tinyint in SQL Server 2000
I am using the AddRow method as shown below and get an
InvalidCastException.
"Cast from string "" to type integer is not valid"

Me.DataSet.tblCustomer.AddtblCustomerRow(txtCustomerName.Text.ToString,
CByte(CInt(txtJobType.Text)), )

You might look at byte.parse. It will throw an exception if the string
is empty or if it overflows a byte value.
 
S

stand__sure

it looks like you are casting an empty string to an integer. In 2.0, there
is a nice new function, Int32.TryParse (it exists for other types too). In
1.1, use Parse and catch the three possible exceptions --
ArgumentNullException, FormatException and OverflowException
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top