Cast from type 'Byte()' to type 'String' is not valid.

Hello - I need to use the id from one query to throw into another query.

Thing is the id in the first database is in the format:
0x00000000000227F1
but when it shows in a data grid. it just shows:
Byte[] Array
I need to get the value of Byte[] Array as a string in the format:
0x00000000000227F1

When I query it in query analyzer, it shows as: 0x00000000000227F1
but, like I say, in my data grid , it just shows as Byte[] Array
So when I try to use it in a query I get the error:
Cast from type 'Byte()' to type 'String' is not valid.

Any ideas?
Thanks



http://www.developdesign.co.uk/
0
loydall
12/7/2004 4:27:43 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

5 Replies
883 Views

Similar Articles

[PageSpeed] 9

string str = System.Text.Encoding.ASCII.GetString(buf);


Have a closer look at the System.Text.Encoding
Hope this helps
0
alexdg
12/7/2004 4:42:59 PM
Add some .NET code to format it (I got the code from here):


'you could do (from string to byte array):
Dim b() As Byte
Dim utf As New System.Text.UTF7Encoding()
b = utf.GetBytes("hello world")
'and (from byte array to string):
Dim s As String = utf.GetString(b)
Console.WriteLine("and string is {0}", s)
Darrell Norton, MVP
Darrell Norton's Blog


Please mark this post as answered if it helped you!
0
DarrellNorton
12/7/2004 4:48:38 PM
I've already tried that thanks. It doesn't work...

This is part of my code:

For i As Integer = 0 To (rowCounter - 1)
Dim utf As New System.Text.UTF7Encoding

curId = utf.GetString(delDs.Tables(0).Rows(i).Item("Members_Id"))

curPp = delDs.Tables(0).Rows(i).Item("Passport_Number")
misCmd.CommandText = "select * from members where MemberID = '" & curId & "'"



And then, just to test, I return the command text as a string and I get:
select * from members where MemberID = '
Note how there's only one '

It's as if throwing the value of whatever
utf.GetString(delDs.Tables(0).Rows(i).Item("Members_Id"))

gives me, terminates the sql statement
I've also tried:
Dim utf As New System.Text.UnicodeEncoding

and
Dim utf As New System.Text.UTF8Encoding

both of these seem to terminate my sql as well..
It's so annoying because if I run the query in query analyzer, I can see the values as strings but it wont have it when I actually want to that field value in another query.....


http://www.developdesign.co.uk/
0
loydall
12/7/2004 5:09:54 PM
the fact that your sql is terminated means that you get a null character (not NULL pointer) in for your string. 

Also, the System.Text.Encoding provides static methods and you don't have to create new encoding objects. That might be issue number one:
string result = System.Text.Encoding.UTF8.GetString(buffer);
The second thing I recommend checking is what you are actually getting in your byte[] array. if the first byte is 0, that would be your null terminated string right there.
0
alexdg
12/8/2004 12:04:08 AM
Thanks - all of the values for the byte array are something like:

0x00000000000227F1
So yes, it will always be a zero at the start of it.
How would I get around this then? It works if I hard code the value into sql, it's the conversion to a string that I'm stuck with.
I don't understand why it's so difficult as query analyzer just returns the values as i want them...
Any ideas?
Anyone?
http://www.developdesign.co.uk/
0
loydall
12/8/2004 10:22:22 AM
Reply:

Similar Artilces:

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

Cast from type 'HttpCookie' to type 'String' is not valid
Hi. I am currently storing data in a cookie (an ID from a database) which i then want to retrieve and use to generate a file name for an uploaded file later on, which will be stored on disk. I get the error "Cast from type 'HttpCookie' to type 'String' is not valid". I assume i need to find a way to convert the data to string? A little help pleeeze, code examples too ;-) Sounds like you may have missed specifing the value property. If you're trying to get the value into a string variable, for instance, something like this: (VB) Dim var As String var = R...

Cast from type 'DBNull' to type 'String' is not valid
I REALLY need some help, so here goes. The following function does work as long as the "mail" field in the database isn't NULL. As you can see I have tried a couple of ways to check for a NULL value but none of them seem to work. What's supposed to happen is the function checks to see whether or not a user has an email address in the database. If so, then the email address gets returned, if no email address then I need something back from the function so I can go about another way to get the info to the user. Public Function GetReqByEmail(ByVal reqby As String) As String...

Cast from type 'ListItem' to type 'String' is not valid
Hi I am getting this error message when I try to view the sql of my insert command in the Oldbcommand1.commandtext statement. can somebody help me. The insert statement is as follows: (Most of the seesions are strings and the others come from a list item value (listitem.selecteditem.value) OleDbCommand1.CommandText = "insert into wlc_mvts values ('" & trs & "','" & Session("payment_mode") & "','" & Session("freight") _ & "','" & Session("red_clause") & "','&...

Cast from type 'DBNull' to type 'String' is not valid.
Hi, I have a page with several textboxes...it's a "change your account" page. One of the parameters is presented as a label instead since it is not changed ON THIS PAGE. It needs to be NULL when the account is created and entered later. But when the page loads, I get the above error. The account row is loaded from a business object LoadFromId(). How can I load the page even though this label data is NULL to start with? relevent code below. <asp:Label ID="lblDDAccount" Runat="server"></asp:Label>------If currentUser.DDAccount = "" Then       &...

Cast from type 'DBNull' to type 'String' is not valid.
Ok, that is the error... "Cast from type 'DBNull' to type 'String' is not valid. " I am filling a DataSet from the db and some fields are empty. Reading from it and placing the info into a label. Code below.... Sub getProductInfo() Dim DSprod2 As New DataSet() Dim MyCommand As OleDbDataAdapter sql = "Select * from tblProducts Where fldNum = " & intfldNum MyCommand = New OleDbDataAdapter(sql, conn) MyCommand.Fill(DSprod2, "tblProducts") lblProd.Text = DSprod2.Tables("...

Cast from type 'DBNull' to type 'String' is not valid
 Hi all,Sorry to be posting here on what is probably a common enough error but I am at my wits end with this one. Quite simply I call an Update stored procedure in my code, passing in a number of paramaters. However, when I try to run the code, the following exception is thrown:Cast from type 'DBNull' to type 'String' is not validI'm really not sure what the problem is here. I have tried hardcoding values for the string values with no success also, as well as using the IsNothing function, but no joy there eitherI would really appreciate someones help as I spent 2 hou...

Cast from type 'DBNull' to type 'String' is not valid.
On the viewproduct page if any of the db fields referenced in the SP are empty you get the above error, the error originates in the productsDB.vb file as follows myProductDetails.ModelNumber = CStr(parameterModelNumber.Value) myProductDetails.ModelName = CStr(parameterModelName.Value) myProductDetails.ProductImage = CStr(parameterProductImage.Value).Trim() myProductDetails.UnitCost = CType(parameterUnitCost.Value, Decimal) myProductDetails.Description = CStr(parameterDescription.Value).Trim() OK I can make ...

Cast from type 'DBNull' to type 'String' is not valid
Hi,  I have an old website and old database (SQL 2000).  I add new column "ImageURL' to database, so all data (several thousand items) are "NULL" (actually is dbNull) under this column.  In searchResult.aspx page, inside the datagrid, I add a colummn call "Image" <code> <asp:datagrid id="dgSearchResult" runat="server"><Columns><asp:TemplateColumn HeaderText="Image" ItemStyle-Width="55px" ItemStyle-HorizontalAlign=Center> <ItemTemplate> <asp:Image ID=ThumbImage Wi...

Cast from type 'Field' to type 'String' is not valid
I am getting the error msg on this code...Response.Write(objRSCount("iCount") & " records for " & frmDispOption & " '" & frmText & "'. They are listed below:")I have tried converting the frmDispOption and frmText variables to strings using CStr after getting the values from the DB but still get the same msg.  Can someone please help me out, thanks.David. What is "objRSCount"? ie what type is it? You also need to make sure that objRSCount("iCount") is string.ThanksMark post(s) as "Answer" that helped youElectronic ScrewWebsite||Blog||Dub@i.net Hi, i f...

Cast from type 'DBNull' to type 'String' is not valid.
How would I check to make sure my SQL db is not pulling a null value? I'm using the following code and I get the 'Cast from type 'DBNull' to type 'String' is not valid.' error. If (dtrUserDetails("D_Address2") Is System.DBNull.Value) Then _Address2 = "" Else _Address2 = dtrUserDetails("D_Address2") End If Any help would be much appreciated. Change your line to _Address2=dtrUserDetails("D_Address2") & "" The empty string in the end will convert the filed to a string ...

Cast from type 'DBNull' to type 'String' is not valid #2
G'day, Puzzled by this. The error occurs on the field "Mobile" yet its format is exactly the same as the other fields. In otherwords I can remove the line of code dealing with "Mobile" and it will function. Can anyone explain? The error is : Cast from type 'DBNull' to type 'String' is not valid. <%#ifPresent("Phone:",Container.DataItem("Phone"))%> <%#ifPresent("Fax:",Container.DataItem("Fax"))%> <%#ifPresent("Mobile:",Container.DataItem("Mobile"))%> <%#ifPre...

Cast from type 'DBNull' to type 'String' is not valid. #2
i have an login page which use e-mail address and password to login , once the system comfirm this is valid login it get customer details and in my login page i have the following code Dim loginsystem As onlineshop.Customers = New onlineshop.Customers() Dim customerId As String = loginsystem.customer_login(login.email_address, login.password) If customerId <> "" Then Dim customerDetails As onlineshop.customer_Detail = loginsystem.Get_Customer_Details(customerid) and at the onlineshop.customer_detail function i have the following ...

Cast from type 'DBNull' to type 'String' is not valid. Error
Hi, Can anyone help me with an error I am receiving "Cast from type 'DBNull' to type 'String' is not valid."  I am creating an aspx page in VB.NET, please find attached code causing error.objCmd = New SqlCommand("USP_DisplayEmbarkDetails", objConn)  objCmd.CommandType = CommandType.StoredProcedure  objCmd.Parameters.Add("@BranchID", lblGetBranchID.Text)  objConn.Open()  objRdr = objCmd.ExecuteReader()    While objRdr.Read     lblBranchName.Text=objRdr("BranchName")    lblBranchID.Text=objRd...

Web resources about - Cast from type 'Byte()' to type 'String' is not valid. - asp.net.getting-started

Resources last updated: 12/4/2015 5:04:23 AM