Error with dropdown list

J

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

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

JJ297

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




















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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top