473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Error on selecting initial value in Displaying gridview based on drop down list selec

When user selects the initial value Please Select I get an exception which is.
Conversion failed when converting the nvarchar value 'Please Select' to data type int.
The Initial value has to be added programmatically
How do I fix the error,below is my code which works correctly except if the initial value is selected.
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         if(!Page.IsPostBack)
  4.         {
  5.         string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
  6.         using(SqlConnection connection=new SqlConnection(cn))
  7.         {
  8.  
  9.             SqlCommand command = new SqlCommand("select * from Employees",connection);
  10.             connection.Open();
  11.             //command.Parameters.AddWithValue("@id",DropDownList1.SelectedValue);
  12.             SqlDataReader dr = command.ExecuteReader();
  13.             DropDownList1.DataTextField = "name";
  14.             DropDownList1.DataValueField = "id";
  15.             DropDownList1.DataSource = dr;
  16.             DropDownList1.DataBind();
  17.             ListItem li = new ListItem("Please Select");
  18.             DropDownList1.Items.Insert(0, li); 
  19.         }
  20.     }
  21.     }
  22.     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  23.     {
  24.         string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
  25.         using (SqlConnection connection = new SqlConnection(cn))
  26.         {
  27.             SqlCommand command = new SqlCommand("select * from Employees where id=@id", connection);
  28.             connection.Open();
  29.             command.Parameters.AddWithValue("@id",DropDownList1.SelectedValue);
  30.             //if (DropDownList1.SelectedValue == "Please Select")
  31.             //{
  32.             //    Response.Write("No Record Selected");
  33.  
  34.             //}
  35.             //command.Parameters.AddWithValue("@id",DropDownList1.SelectedValue);
  36.             SqlDataReader dr = command.ExecuteReader();
  37.             GridView1.DataSource = dr;
  38.             GridView1.DataBind();
  39.  
  40.         }
  41.     }
  42.  
  43.  
  44.  
Jun 17 '13 #1
0 1028

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Stephen Adam | last post by:
Hi there, I'm sure i'm missing something really simple here, all i want to do is get the value of the selected item in a list box. Even after much fiddling about last night I still could not get...
1
by: Bishop | last post by:
I made a GridView with a Drop Down List using the example here: http://www.orcsweb.com/articles/dotnet_control_templates.aspx It appears to work except that it dosn't actually save the updated...
3
by: KatMagic | last post by:
How do you force a drop down list to default to a particular item based on its value? I have a drop down list with options Yes, No, Maybe, All, and I would like it to default to "All" This is...
1
by: mrjoka | last post by:
hi, i have a label and i have two drop down list and i want to assign a value to this label when a value has been selected from drop down list 1(this one already working) now i want the label to...
1
by: prashu | last post by:
how to retrieve a value selected in a drop down list box using jsp
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
5
by: mrtr33 | last post by:
I have a class table where I want to access the names of courses and the description of each course. The names of the courses go in a drop down list. Based on the user selection, the description...
3
by: vimal | last post by:
hi all, i am still a learner in javascript i have a select tag with options as follows: <select> <option py:for="mb in range(32,100)">${mb}</option> </select>
2
by: axapta | last post by:
Hi Group, How can I limit a second drop-down list box based on a selected value in the first drop down list box. Using vb.net Regards
4
by: hdhouse3rd | last post by:
Greetings: A newbie question. I have a form that is completed and every time we save the form the value of the drop down list is reset to default. I presume you need to do a query of the database...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.