Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Specified cast is not valid error

Reply
Thread Tools

Specified cast is not valid error

 
 
.Net Sports
Guest
Posts: n/a
 
      10-05-2006
I have a datagrid script where I modify data in an sql dbase in
asp.net, when i hit the "update" button, I get a Specified cast is not
valid error on my 'descript' declaration, whereas 'descript' is a
multiline text box and a varchar datatype (everything else is either a
char or a datetime datatype). Am I assigning 'descript' a wrong
datatype? Tried making it a nvarchar, still get same result

'''''''''''''''''''''''''''''''

Sub MyDataGrid_UpdateCommand(s As Object, e As DataGridCommandEventArgs
)
Dim conn As SQLConnection
Dim MyCommand As SQLCommand
Dim strConn as string = "Server=sql.mydomain.com;Initial
Catalog=mydb;User ID=DBxxx;Password=xxxxx;"
Dim company As textbox = E.Item.cells(2).Controls(0)
Dim address As textbox = E.Item.cells(3).Controls(0)
Dim city As textbox = E.Item.Cells(4).Controls(0)
Dim state As textbox = E.Item.cells(5).Controls(0)
Dim county As textbox = E.Item.cells(6).Controls(0)
Dim zip As textbox = E.Item.cells(7).Controls(0)
Dim phone As textbox = E.Item.cells(.Controls(0)
'''''' the following line declaring the descript var is the line of the
error
Dim descript As textbox = E.Item.cells(9).Controls(0)
Dim web As textbox = E.Item.cells(10).Controls(0)
Dim email As textbox = E.Item.cells(11).Controls(0)
Dim datesold As textbox = E.Item.cells(12).Controls(0)
Dim dateexpire As textbox = E.Item.cells(13).Controls(0)

Dim strUpdateStmt As String
strUpdateStmt =" UPDATE CPAs SET" & _
" company = @company, address = @address, city = @city, state =
@state, " & _
"county = @county, zip = @zip, phone = @phone, descript =@
descript, " & _
"web = @web, email = @email, datesold = @datesold, dateexpire =
@dateexpire" & _
" WHERE cpaID = @cpaID"
conn = New SqlConnection(strConn)
MyCommand = New SqlCommand(strUpdateStmt, conn)
MyCommand.Parameters.Add(New SqlParameter("@company", company.text))
MyCommand.Parameters.Add(New SqlParameter("@address", address.text))
MyCommand.Parameters.Add(New SqlParameter("@city", city.text))
MyCommand.Parameters.Add(New SqlParameter("@state", state.text))
MyCommand.Parameters.Add(New SqlParameter("@county", county.text))
MyCommand.Parameters.Add(New SqlParameter("@zip", zip.text))
MyCommand.Parameters.Add(New SqlParameter("@phone", phone.text))
MyCommand.Parameters.Add(New SqlParameter("@descript", descript.text))
MyCommand.Parameters.Add(New SqlParameter("@web", web.text))
MyCommand.Parameters.Add(New SqlParameter("@email", email.text))
MyCommand.Parameters.Add(New SqlParameter("@datesold", datesold.text))
'', dateexpire =@dateexpire
MyCommand.Parameters.Add(New SqlParameter("@dateexpire",
dateexpire.text))

MyCommand.Parameters.Add(New SqlParameter("@cpaID",
e.Item.Cells(1).Text ))
conn.Open()
MyCommand.ExecuteNonQuery()
MyDataGrid.EditItemIndex = -1
conn.close
BindData
End Sub

'''''''''''''''''''''''''''''

netsports

 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      10-05-2006
How long is the text in the box? Is it constrained? If not you might have to
cast to a text type and give it a length which is long enough to hold the
amount of data that can be shoved into field. I also think there may be some
magic for a multi line text box, instead of placing the contents directly
into a parameter, try casting out as a string first and see if you get the
same issue.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
".Net Sports" <> wrote in message
news: ups.com...
>I have a datagrid script where I modify data in an sql dbase in
> asp.net, when i hit the "update" button, I get a Specified cast is not
> valid error on my 'descript' declaration, whereas 'descript' is a
> multiline text box and a varchar datatype (everything else is either a
> char or a datetime datatype). Am I assigning 'descript' a wrong
> datatype? Tried making it a nvarchar, still get same result
>
> '''''''''''''''''''''''''''''''
>
> Sub MyDataGrid_UpdateCommand(s As Object, e As DataGridCommandEventArgs
> )
> Dim conn As SQLConnection
> Dim MyCommand As SQLCommand
> Dim strConn as string = "Server=sql.mydomain.com;Initial
> Catalog=mydb;User ID=DBxxx;Password=xxxxx;"
> Dim company As textbox = E.Item.cells(2).Controls(0)
> Dim address As textbox = E.Item.cells(3).Controls(0)
> Dim city As textbox = E.Item.Cells(4).Controls(0)
> Dim state As textbox = E.Item.cells(5).Controls(0)
> Dim county As textbox = E.Item.cells(6).Controls(0)
> Dim zip As textbox = E.Item.cells(7).Controls(0)
> Dim phone As textbox = E.Item.cells(.Controls(0)
> '''''' the following line declaring the descript var is the line of the
> error
> Dim descript As textbox = E.Item.cells(9).Controls(0)
> Dim web As textbox = E.Item.cells(10).Controls(0)
> Dim email As textbox = E.Item.cells(11).Controls(0)
> Dim datesold As textbox = E.Item.cells(12).Controls(0)
> Dim dateexpire As textbox = E.Item.cells(13).Controls(0)
>
> Dim strUpdateStmt As String
> strUpdateStmt =" UPDATE CPAs SET" & _
> " company = @company, address = @address, city = @city, state =
> @state, " & _
> "county = @county, zip = @zip, phone = @phone, descript =@
> descript, " & _
> "web = @web, email = @email, datesold = @datesold, dateexpire =
> @dateexpire" & _
> " WHERE cpaID = @cpaID"
> conn = New SqlConnection(strConn)
> MyCommand = New SqlCommand(strUpdateStmt, conn)
> MyCommand.Parameters.Add(New SqlParameter("@company", company.text))
> MyCommand.Parameters.Add(New SqlParameter("@address", address.text))
> MyCommand.Parameters.Add(New SqlParameter("@city", city.text))
> MyCommand.Parameters.Add(New SqlParameter("@state", state.text))
> MyCommand.Parameters.Add(New SqlParameter("@county", county.text))
> MyCommand.Parameters.Add(New SqlParameter("@zip", zip.text))
> MyCommand.Parameters.Add(New SqlParameter("@phone", phone.text))
> MyCommand.Parameters.Add(New SqlParameter("@descript", descript.text))
> MyCommand.Parameters.Add(New SqlParameter("@web", web.text))
> MyCommand.Parameters.Add(New SqlParameter("@email", email.text))
> MyCommand.Parameters.Add(New SqlParameter("@datesold", datesold.text))
> '', dateexpire =@dateexpire
> MyCommand.Parameters.Add(New SqlParameter("@dateexpire",
> dateexpire.text))
>
> MyCommand.Parameters.Add(New SqlParameter("@cpaID",
> e.Item.Cells(1).Text ))
> conn.Open()
> MyCommand.ExecuteNonQuery()
> MyDataGrid.EditItemIndex = -1
> conn.close
> BindData
> End Sub
>
> '''''''''''''''''''''''''''''
>
> netsports
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: specified cast is not valid. Why not? Alan Silver ASP .Net 5 02-15-2005 08:08 PM
Error-- Specified cast is not valid Justin ASP .Net 1 12-03-2003 06:02 PM
PRB: Error Message: System.InvalidCastException: Specified Cast Is Not Valid Miguel Angel Dinelli ASP .Net 1 11-13-2003 07:56 PM
Re: 'Specified Cast Is Not Valid' Error! Stefan Landgraf ASP .Net 0 08-19-2003 02:04 PM
I get the following error ( Specified cast is not valid ) with Server.Transfer Andrew ASP .Net 0 08-12-2003 08:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57