473,386 Members | 1,819 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,386 software developers and data experts.

when i write this code then it shows error...

Error:- Failed to convert parameter value from a String to a Int32.

code--------------------
Expand|Select|Wrap|Line Numbers
  1. SqlConnection conn;
  2.         SqlCommand cmd;
  3.         SqlDataReader reader;
  4.         string connectionString = ConfigurationManager.ConnectionStrings["Dorknozzle"].ConnectionString;
  5.         conn = new SqlConnection(connectionString);
  6.         cmd = new SqlCommand("select Username, Address, City, State, " + "HomePhone, Extention, MobilePhone from employees" + "where Name=@Name", conn);
  7.         cmd.Parameters.Add("@Name",System.Data.SqlDbType.Int);
  8.         cmd.Parameters["@Name"].Value= DropDownList1.SelectedItem.Value.ToString();
  9.         try
  10.         {
  11.             conn.Open();
  12.       reader=cmd.ExecuteReader();
  13.                     {
  14.                 nametextbox.Text = reader["Name"].ToString();
  15.                 usernameTextBox.Text = reader["Username"].ToString();
  16.                 addressTextBox.Text = reader["Address"].ToString();
  17.                 cityTextBox.Text = reader["City"].ToString();
  18.                 stateTextBox.Text = reader["State"].ToString();
  19.                 //zipTextBox.Text = reader["Zip"].ToString();
  20.                 homephoneTextBox.Text = reader["HomePhone"].ToString();
  21.                 extentionTextBox.Text = reader["Extention"].ToString();
  22.                 mobileTextBox.Text = reader["MobilePhone"].ToString();
  23.             }
  24.             reader.Close();
  25.             updateButton.Enabled = true;
  26.         }
  27.         /*catch
  28.         {
  29.             Label1.Text = "Error!!!!!!";
  30.         }*/
  31.         finally
  32.         {
  33.  
  34.             conn.Close();
  35.         }
Aug 11 '10 #1
2 1007
Frinavale
9,735 Expert Mod 8TB
On line 7 & 8 in the above posted code you have the following:
Expand|Select|Wrap|Line Numbers
  1. cmd.Parameters.Add("@Name",System.Data.SqlDbType.Int);
  2. cmd.Parameters["@Name"].Value= DropDownList1.SelectedItem.Value.ToString()
You are stating that the @Name parameter should be an "SqlDbType.Int" but then you try to supply a String as a value.

Change the "SqlDbType.Int" to an "SqlDbType.VarChar" if your database is expecting a String. If your database is expecting an Int value for the name then provide an integer (not a String) as a parameter.

-Frinny
Aug 11 '10 #2
If I'm correct, you are not converting the value retrieved from the dropdownlist into an int. @Name is an Int32 and the value retrieved from DropDownList1 is a string try this
Expand|Select|Wrap|Line Numbers
  1. cmd.Parameters["@Name"].Value = Convert.ToInt32(DropDownList1.SelectedItem.Value);
  2.  
Aug 12 '10 #3

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

Similar topics

4
by: Max Harvey | last post by:
Hi, I have looked at the example called "Open Parameter queries from code" from the site http://www.mvps.org/access/queries/qry0003.htm I made up a test which I though looked pretty close...
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
1
by: Stephen | last post by:
I have the following asp:checkbox as a row in my datagrid. and im trying to write code on the onCheckedChanged event of it. The code im trying to write is incorrect as im getting build errors....
4
by: Alpha | last post by:
I have a window Application. this.close(); doesn't stop and close my application when I get an error. Instead it continues trying to execute the following codes. What code do I need to the...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
0
by: Greg | last post by:
My app makes a call to a remote server (about which I know relatively little) using Webclient.UploadData. This is done once every 51/2 mins and is called by code within a worker thread. 95% of the...
1
by: Greg | last post by:
My app makes a call to a remote server (about which I know relatively little) using Webclient.DownloadData. This is done once every 51/2 mins and is called by code within a worker thread. 95% of...
0
by: MaxN | last post by:
I'm writing a program to go to a website, extract the source code, parse the code to get the data in the tables, sort the data in a particular manner, write it to a .txt, and send it to our database...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.