Error with dropdown list

Error with dropdown list

am 08.01.2008 16:30:52 von JJ297

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.Connection Strings("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

Re: Error with dropdown list

am 08.01.2008 17:53:34 von smhusain1983

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=A0pm, JJ297 wrote:
> I have a dropdown list populated via a database. =A0I edited the
> dropdown list to have "select a topic" to always appear when the drop
> down loads. =A0Now if a user selects a dropdown item it goes into the
> datagrid fine, but if a user selects "select a topic" =A0I get an error
> message...
> Error converting nvarchar to int on adapter.Fill(ds)
>
> Here's the code behind can someone please assist? =A0Thanks!
>
> =A0 Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As
> Object, ByVal e As System.EventArgs) Handles
> DropDownList2.SelectedIndexChanged
>
> =A0 =A0 =A0 =A0 Dim conn As New
> Data.SqlClient.SqlConnection(ConfigurationManager.Connection Strings("Train=
U=ADserConnectionString").ConnectionString)
>
> =A0 =A0 =A0 =A0 Dim cmd As New Data.SqlClient.SqlCommand
>
> =A0 =A0 =A0 =A0 With cmd
> =A0 =A0 =A0 =A0 =A0 =A0 .CommandType =3D Data.CommandType.StoredProcedure
>
> =A0 =A0 =A0 =A0 =A0 =A0 .CommandText =3D "Getclassifications1"
>
> =A0 =A0 =A0 =A0 =A0 =A0 If DropDownList2.SelectedValue =3D "select a topic=
" Then
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Response.Write("Please select a topic")
>
> =A0 =A0 =A0 =A0 =A0 =A0 Else
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .Parameters.AddWithValue("classificationid=
",
> DropDownList2.SelectedValue)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .Connection =3D conn
> =A0 =A0 =A0 =A0 =A0 =A0 End If
> =A0 =A0 =A0 =A0 End With
>
> =A0 =A0 =A0 =A0 Dim adapter As New Data.SqlClient.SqlDataAdapter(cmd)
> =A0 =A0 =A0 =A0 Try
>
> =A0 =A0 =A0 =A0 =A0 =A0 conn.Open()
>
> =A0 =A0 =A0 =A0 =A0 =A0 Dim ds As New Data.DataSet
> =A0 =A0 =A0 =A0 =A0 =A0 adapter.Fill(ds)
>
> =A0 =A0 =A0 =A0 =A0 =A0 GridView1.DataSource =3D ds
>
> =A0 =A0 =A0 =A0 =A0 =A0 GridView1.DataBind()
>
> =A0 =A0 =A0 =A0 Finally
>
> =A0 =A0 =A0 =A0 =A0 =A0 conn.Close()
>
> =A0 =A0 =A0 =A0 End Try

Re: Error with dropdown list

am 08.01.2008 18:04:58 von JJ297

On Jan 8, 11:53=A0am, smhusain1...@gmail.com 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=A0pm, JJ297 wrote:
>
>
>
> > I have a dropdown list populated via a database. =A0I edited the
> > dropdown list to have "select a topic" to always appear when the drop
> > down loads. =A0Now if a user selects a dropdown item it goes into the
> > datagrid fine, but if a user selects "select a topic" =A0I get an error
> > message...
> > Error converting nvarchar to int on adapter.Fill(ds)
>
> > Here's the code behind can someone please assist? =A0Thanks!
>
> > =A0 Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As
> > Object, ByVal e As System.EventArgs) Handles
> > DropDownList2.SelectedIndexChanged
>
> > =A0 =A0 =A0 =A0 Dim conn As New
> > Data.SqlClient.SqlConnection(ConfigurationManager.Connection Strings("Tra=
inU­­serConnectionString").ConnectionString)
>
> > =A0 =A0 =A0 =A0 Dim cmd As New Data.SqlClient.SqlCommand
>
> > =A0 =A0 =A0 =A0 With cmd
> > =A0 =A0 =A0 =A0 =A0 =A0 .CommandType =3D Data.CommandType.StoredProcedur=
e
>
> > =A0 =A0 =A0 =A0 =A0 =A0 .CommandText =3D "Getclassifications1"
>
> > =A0 =A0 =A0 =A0 =A0 =A0 If DropDownList2.SelectedValue =3D "select a top=
ic" Then
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Response.Write("Please select a topic")
>
> > =A0 =A0 =A0 =A0 =A0 =A0 Else
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .Parameters.AddWithValue("classification=
id",
> > DropDownList2.SelectedValue)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .Connection =3D conn
> > =A0 =A0 =A0 =A0 =A0 =A0 End If
> > =A0 =A0 =A0 =A0 End With
>
> > =A0 =A0 =A0 =A0 Dim adapter As New Data.SqlClient.SqlDataAdapter(cmd)
> > =A0 =A0 =A0 =A0 Try
>
> > =A0 =A0 =A0 =A0 =A0 =A0 conn.Open()
>
> > =A0 =A0 =A0 =A0 =A0 =A0 Dim ds As New Data.DataSet
> > =A0 =A0 =A0 =A0 =A0 =A0 adapter.Fill(ds)
>
> > =A0 =A0 =A0 =A0 =A0 =A0 GridView1.DataSource =3D ds
>
> > =A0 =A0 =A0 =A0 =A0 =A0 GridView1.DataBind()
>
> > =A0 =A0 =A0 =A0 Finally
>
> > =A0 =A0 =A0 =A0 =A0 =A0 conn.Close()
>
> > =A0 =A0 =A0 =A0 End Try- Hide quoted text -
>
> - Show quoted text -

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

It works!