Can you help with ? 'DBNull' to type 'String' is not valid

If have a 'Null' value in the 'accommtype' field I get an error: 

System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid
I have tried using: If IsDBNull(value) Then Return "" without success.
Does anyone know how this can be handled?
<%@ Page Language="vb" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SQLClient" %>
<script runat="server">
Dim intWdth, intcustomerid as integer
Dim straccomname1, straccommtype, strTitle as string
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
BindData
End If
End Sub
Sub BindData()
Dim strConn as string = "server='localhost';Uid=sa;Pwd=sa ; Database='testdb'"
Dim sql as string = "Select customerid, accomname1, accommtype from Customerwebsite Where customerid = " & Session("ClientID").ToString()
Dim conn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(sql, conn)
Dim objDR as SQLDataReader
conn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
While objDR.Read()
intcustomerid=objDR("customerid")
straccomname1=objDR("accomname1")
straccommtype=objDR("accommtype")
End While
page.databind()
conn.Close()
End Sub
Sub btnUpdate(sender As Object, e As EventArgs)
Dim strConn as string = "server='localhost';Uid=sa;Pwd=sa ; Database='testdb'"
Dim sql as string = "Select customerid, accomname1, accommtype from Customerwebsite Where customerid = " & Session("ClientID").ToString()
Dim Conn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(MySQL, Conn)
cmd.Parameters.Add(New SQLParameter("@customerid", lblcustomerid.text))
cmd.Parameters.Add(New SQLParameter("@accomname1", frmaccomname1.text))
cmd.Parameters.Add(New SQLParameter("@accommtype", frmaccommtype.text))
Conn.Open()
cmd.ExecuteNonQuery
label1.text = "It's Done!"
label1.text="Successfully updated -- - "
End Sub
</script>
<html>
<head>
<title>Choosing & Updating a Record</title>
</head>
<body>
<form name="form1" runat="server">
<table width="100%" border="1">
<tbody>
<tr>
<td valign="top" align="middle" width="145">
<br />
<br />
</td>
<td valign="top" align="left">
ID : <asp:Label id="lblcustomerid" runat="server" text="<%# intcustomerid %>"></asp:Label>
<br />
FirstName :
<asp:textbox id="frmaccomname1" runat="server" Text="<%# straccomname1 %>"></asp:textbox>
<br />
LastName :
<asp:textbox id="frmaccommtype" runat="server" Text="<%# straccommtype%>"></asp:textbox>
<br />
<br />
<br />
<asp:button id="btnUpdater" onclick="btnUpdate" runat="server" Text="Update"></asp:button>
<asp:Label id="label1" runat="server" forecolor="red"></asp:Label>
<br />
<asp:Label id="label2" runat="server"></asp:Label></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
0
Groatsie
12/10/2003 1:05:23 PM
asp.net.object-datasource 16182 articles. 0 followers. Follow

1 Replies
469 Views

Similar Articles

[PageSpeed] 30

Well, in C# I use this syntax, 

string test= (dr[0] != DBNull.Value ? dr.GetString(0) : string.Empty);
I'd guess the VB version could be fairly similar...
0
Scott
12/10/2003 1:30:24 PM
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 'DBNull' to type 'String' is not valid.
I have a problem when I try to read from a table "Ads" in my database and the value of the field is <NULL>. (I assume that I will come across a field that will be blank.) I’m not referencing any field in particular. I am learning .Net through I test project. When I delete a field in a record, for example "phone" I get following error: Cast from type 'DBNull' to type 'String' is not valid. Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid. I would like to not show the label at all if it's null. Fo...

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

'DBNull' to type 'String' is not valid
 I am using asp.net 3.5 with ajax. Here is my code behind the webpage:     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click        'Connection string        Dim connStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("_database/AttorneyApp.mdb"))        'Establish connection        Dim conn As OleDbConnection = New ...

'DBNull' to type 'String' is not valid
Can anyone help me solve this error?  I think I am getting this because there is a NULL value in the database for the field.  Here is my code: While objRdr.Read() txtPhone2.Text=objRdr("SecondaryPhone")  I also tried: While objRdr.Read() txtPhone2.Text=CStr(objRdr("SecondaryPhone")) I am not using a repeater or datalist because I am only pulling a single record for the purpose of editing it. I am still relatively new, and woulld appreciate as much detail as you can provide.  I am using SQL Server and VB.net. Hi There, try following: Whil...

Conversion from type 'DBNull' to type 'String' is not valid.
On line 65 of Events_Calendar.aspx- <div style="padding: 3px;"> <asp:HyperLink ID="HyperLink1" runat="server" Text='<%#Eval("title") %>' NavigateUrl='<%# "Events_view.aspx?Eventid=" &CStr( Eval("ID"))%>' ToolTip='<%# truncate(CStr(Eval("description"))) %>' /></div> The debugger tells me "InvalidCastException unhandled by user code" I found out that the data entry form for events allows the user to leave the description field empty.  So this translates to NULL in the SQL Server 2005 database.  the NULL value in the description field...

Conversion from type 'DBNull' to type 'String' is not valid
I get this error when I try to add an event:Conversion from type 'DBNull' to type 'String' is not validI've been able to add events before, but all of the sudden I'm getting the error...Here is the output of the error:Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.Source Error: Line 115: </h3> Line 116: <p> Line 117: <asp:Label ID="descriptionLabel" runat="server" Text='<%# truncate(CStr(Eval("description"))) %>' />...

Conversion from type 'DBNull' to type 'String' is not valid
I get an error (Conversion from type 'DBNull' to type 'String' is not valid.) when I try to assign a field value to a text box using the datareader. Is there code that will fix this error?  wrap an IF check around the retrieval and check for DBNull you'll need to check for null first, before you assign it - - Something like: if Not dr("Yourfield") is System.DbNull.Value then...........David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper - easily control pr...

Conversion from type 'DBNull' to type 'String' is not valid
Hello Friends            How are you?? Friends i am getting problem in SQL Server 2005. I am deployng web application on production server as well as Databse also. In production server i inserted new field in all tables which is rowguid and its type is uniqueidentifier. The default binding for this field is newsequentialid(). In some pages it works ok but in some places it generates error like 'Conversion from type 'DBNull' to type 'String' is not valid'. Can anybody help me to solve this problem. Its urgent so plz re...

Conversion from type 'DBNull' to type 'String' is not valid.
 Hello Experts,I am reading data into a datagrid and am checking for nullsHere is the code in the datagrid which checks a function in the code-behind:<ItemTemplate>    <asp:Label id="Label2" runat="server" text='<%# GetPickUpDate(container.dataitem("ActualPickUpDate"))%>'></asp:Label></ItemTemplate> Here is the funtion:Function GetPickUpDate(ByVal DateVal As Date) As String            If Not IsDBNull(DateVal) Then     &nb...

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.
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
 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...

Conversion from type 'DBNull' to type 'String' is not valid.
Hi,it's me again...I'm having error withDim Accesstring As String = ("SELECT     FirstName, LastName, Email, StreetAddress, City, Country, State, Zip, Worknum, googlecheckout, paypal, csftcc, confirmationcode FROM dbo.reservation WHERE (confirmationcode = @concode)")            Dim cmd3 As New SqlCommand(Accesstring, icnnn)            cmd3.Parameters.Add("@concode", SqlDbType.NChar).Value = onlineconfirmationcode.Text   &...

Web resources about - Can you help with ? 'DBNull' to type 'String' is not valid - asp.net.object-datasource

Resources last updated: 12/1/2015 2:58:48 AM