Error with dropdown list

Discussion in 'ASP .Net' started by JJ297, Jan 8, 2008.

  1. JJ297

    JJ297 Guest

    I have a dropdown list populated via a database. I edited the
    dropdown list to have "select a topic" to always appear when the drop
    down loads. Now if a user selects a dropdown item it goes into the
    datagrid fine, but if a user selects "select a topic" I get an error
    message...
    Error converting nvarchar to int on adapter.Fill(ds)

    Here's the code behind can someone please assist? Thanks!

    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As
    Object, ByVal e As System.EventArgs) Handles
    DropDownList2.SelectedIndexChanged


    Dim conn As New
    Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("TrainUserConnectionString").ConnectionString)

    Dim cmd As New Data.SqlClient.SqlCommand

    With cmd
    .CommandType = Data.CommandType.StoredProcedure

    .CommandText = "Getclassifications1"


    If DropDownList2.SelectedValue = "select a topic" Then

    Response.Write("Please select a topic")

    Else
    .Parameters.AddWithValue("classificationid",
    DropDownList2.SelectedValue)
    .Connection = conn
    End If
    End With

    Dim adapter As New Data.SqlClient.SqlDataAdapter(cmd)
    Try

    conn.Open()

    Dim ds As New Data.DataSet
    adapter.Fill(ds)

    GridView1.DataSource = ds

    GridView1.DataBind()

    Finally

    conn.Close()

    End Try
    JJ297, Jan 8, 2008
    #1
    1. Advertising

  2. JJ297

    Guest

    Hi,
    I m a c# developer..dont have idea abt vb.net syntax but
    the problem seems to be in If Condition..
    seems like every time control passing in else condition n so passing
    nvarchar value "select a topic" to ur procedure
    which takes only int value for ID "classificationid" n so passing the
    error messg..

    just a guess..



    On Jan 8, 8:30 pm, JJ297 <> wrote:
    > I have a dropdown list populated via a database.  I edited the
    > dropdown list to have "select a topic" to always appear when the drop
    > down loads.  Now if a user selects a dropdown item it goes into the
    > datagrid fine, but if a user selects "select a topic"  I get an error
    > message...
    > Error converting nvarchar to int on adapter.Fill(ds)
    >
    > Here's the code behind can someone please assist?  Thanks!
    >
    >   Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As
    > Object, ByVal e As System.EventArgs) Handles
    > DropDownList2.SelectedIndexChanged
    >
    >         Dim conn As New
    > Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("TrainU­serConnectionString").ConnectionString)
    >
    >         Dim cmd As New Data.SqlClient.SqlCommand
    >
    >         With cmd
    >             .CommandType = Data.CommandType.StoredProcedure
    >
    >             .CommandText = "Getclassifications1"
    >
    >             If DropDownList2.SelectedValue = "select a topic" Then
    >
    >                 Response.Write("Please select a topic")
    >
    >             Else
    >                 .Parameters.AddWithValue("classificationid",
    > DropDownList2.SelectedValue)
    >                 .Connection = conn
    >             End If
    >         End With
    >
    >         Dim adapter As New Data.SqlClient.SqlDataAdapter(cmd)
    >         Try
    >
    >             conn.Open()
    >
    >             Dim ds As New Data.DataSet
    >             adapter.Fill(ds)
    >
    >             GridView1.DataSource = ds
    >
    >             GridView1.DataBind()
    >
    >         Finally
    >
    >             conn.Close()
    >
    >         End Try
    , Jan 8, 2008
    #2
    1. Advertising

  3. JJ297

    JJ297 Guest

    On Jan 8, 11:53 am, wrote:
    > Hi,
    > I m a c# developer..dont have idea abt vb.net syntax but
    > the problem seems to be in If Condition..
    > seems like every time control passing in else condition n so passing
    > nvarchar value "select a topic" to ur procedure
    > which takes only int value for ID "classificationid" n so passing the
    > error messg..
    >
    > just a guess..
    >
    > On Jan 8, 8:30 pm, JJ297 <> wrote:
    >
    >
    >
    > > I have a dropdown list populated via a database.  I edited the
    > > dropdown list to have "select a topic" to always appear when the drop
    > > down loads.  Now if a user selects a dropdown item it goes into the
    > > datagrid fine, but if a user selects "select a topic"  I get an error
    > > message...
    > > Error converting nvarchar to int on adapter.Fill(ds)

    >
    > > Here's the code behind can someone please assist?  Thanks!

    >
    > >   Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As
    > > Object, ByVal e As System.EventArgs) Handles
    > > DropDownList2.SelectedIndexChanged

    >
    > >         Dim conn As New
    > > Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("TrainU­­serConnectionString").ConnectionString)

    >
    > >         Dim cmd As New Data.SqlClient.SqlCommand

    >
    > >         With cmd
    > >             .CommandType = Data.CommandType.StoredProcedure

    >
    > >             .CommandText = "Getclassifications1"

    >
    > >             If DropDownList2.SelectedValue = "select a topic" Then

    >
    > >                 Response.Write("Please select a topic")

    >
    > >             Else
    > >                 .Parameters.AddWithValue("classificationid",
    > > DropDownList2.SelectedValue)
    > >                 .Connection = conn
    > >             End If
    > >         End With

    >
    > >         Dim adapter As New Data.SqlClient.SqlDataAdapter(cmd)
    > >         Try

    >
    > >             conn.Open()

    >
    > >             Dim ds As New Data.DataSet
    > >             adapter.Fill(ds)

    >
    > >             GridView1.DataSource = ds

    >
    > >             GridView1.DataBind()

    >
    > >         Finally

    >
    > >             conn.Close()

    >
    > >         End Try- Hide quoted text -

    >
    > - Show quoted text -


    Actually I solved it by adding List Item value = 0 on the asp page.
    Then on the aspx page I added
    If DropDownList2.SelectedItem.Value = "0" Then
    DD2.Text = ("select a topic")
    End If

    It works!
    JJ297, Jan 8, 2008
    #3
    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. vishnu
    Replies:
    1
    Views:
    1,203
    Patrick.O.Ige
    Mar 25, 2006
  2. Mike
    Replies:
    1
    Views:
    803
  3. acadam
    Replies:
    0
    Views:
    755
    acadam
    Dec 27, 2006
  4. Replies:
    2
    Views:
    432
  5. Steve Myers
    Replies:
    2
    Views:
    272
    Steve Myers
    Nov 20, 2003
Loading...

Share This Page