Specified cast is not valid error

Discussion in 'ASP .Net' started by .Net Sports, Oct 5, 2006.

  1. .Net Sports

    .Net Sports Guest

    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(8).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
     
    .Net Sports, Oct 5, 2006
    #1
    1. Advertising

  2. 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:...
    >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(8).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
    >
     
    Cowboy \(Gregory A. Beamer\), Oct 5, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew
    Replies:
    0
    Views:
    669
    Andrew
    Aug 12, 2003
  2. Stefan Landgraf

    Re: 'Specified Cast Is Not Valid' Error!

    Stefan Landgraf, Aug 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    413
    Stefan Landgraf
    Aug 19, 2003
  3. Miguel Angel Dinelli
    Replies:
    1
    Views:
    448
    Chris Jackson
    Nov 13, 2003
  4. Justin

    Error-- Specified cast is not valid

    Justin, Dec 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    434
    Kevin Spencer
    Dec 3, 2003
  5. Alan Silver
    Replies:
    5
    Views:
    2,594
    Kevin Spencer
    Feb 15, 2005
Loading...

Share This Page