469,964 Members | 1,681 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

Error with dropdown list

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").Con nectionString)

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 '08 #1
2 1256
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 <nc...@yahoo.comwrote:
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").Co nnectionString)

* * * * 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 '08 #2
On Jan 8, 11:53*am, 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*pm, JJ297 <nc...@yahoo.comwrote:
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").C onnectionString)
* * * * 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!
Jan 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike Collins | last post: by
5 posts views Thread by jung_h_park | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.