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

Conversion failed when converting the varchar value 'AccNum' to data type int.

I need some help, cant figure out the error.
Expand|Select|Wrap|Line Numbers
  1. SqlConnection dbConnection = new SqlConnection(xmldb);
  2. dbConnection.Open();
  3. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + lblAccNum.Text + "' ";
  4. SqlCommand command = new SqlCommand(sql, dbConnection);
  5. SqlDataReader reader = command.ExecuteReader();
  6. while (reader.Read())
  7. {
  8.          InvoiceStatus = reader[0].ToString();                 
  9. }
  10. reader.Close();            
  11. dbConnection.Close();
I get this error:
Conversion failed when converting the varchar value 'AccNum' to data type int.

I have checked in the database and AccNum is datatype int, and InvStatus datatype varchar, If there's something I'm missing or something else I should check i'd appreciate the help. Thanks
Oct 22 '08 #1
9 24259
nateraaaa
663 Expert 512MB
Change the Select statement to look like this

Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + Convert.ToInt32(lblAccNum.Text) + "' ";
  2.  
Nathan
Oct 22 '08 #2
Change the Select statement to look like this

Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + Convert.ToInt32(lblAccNum.Text) + "' ";
  2.  
Nathan
Thanks, but when I try that I get this error:
"Input string was not in a correct format."
Oct 22 '08 #3
nateraaaa
663 Expert 512MB
Thanks, but when I try that I get this error:
"Input string was not in a correct format."
What is the value of AccNum.Text? Is the value numeric?

If so try this
Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + Convert.ToInt32(lblAccNum.Text);
Oct 22 '08 #4
Curtis Rutland
3,256 Expert 2GB
The problem is that you are surrounding the param with single quotes. In SQL, that represents a varchar value, a string. Change your select statement to this:
Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + lblAccNum.Text;
  2.  
Better yet, learn how to use String.Format"
Expand|Select|Wrap|Line Numbers
  1. string sql = String.Format("SELECT InvStatus FROM SaleReceipts Where AccNum = {0}", lblAccNum.Text);
  2.  
The best way would be to use a parameterized query.
Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = @AccNum";
  2. SqlCommand command = new SqlCommand(sql, dbConnection);
  3. command.Parameters.AddWithValue("@AccNum", lblAccNum.Text);
  4.  
This one is the most secure, and the easiest to understand just by looking at it.

@nateraaaa,
You wouldn't have to convert the text value to an int to put it back into a string. The original error was coming from the DB engine.
Oct 22 '08 #5
The problem is that you are surrounding the param with single quotes. In SQL, that represents a varchar value, a string. Change your select statement to this:
Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + lblAccNum.Text;
  2.  
Better yet, learn how to use String.Format"
Expand|Select|Wrap|Line Numbers
  1. string sql = String.Format("SELECT InvStatus FROM SaleReceipts Where AccNum = {0}", lblAccNum.Text);
  2.  
The best way would be to use a parameterized query.
Expand|Select|Wrap|Line Numbers
  1. string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = @AccNum";
  2. SqlCommand command = new SqlCommand(sql, dbConnection);
  3. command.Parameters.AddWithValue("@AccNum", lblAccNum.Text);
  4.  
This one is the most secure, and the easiest to understand just by looking at it.

@nateraaaa,
You wouldn't have to convert the text value to an int to put it back into a string. The original error was coming from the DB engine.
Yeah it's much easier to understand. It worked perfect, thanks a lot for ur input! Appreciate it!

--George
Oct 22 '08 #6
Curtis Rutland
3,256 Expert 2GB
Glad to be of service =D
Oct 22 '08 #7
nateraaaa
663 Expert 512MB
If the DB expects AccNum to be an int then the value set for the @AccNum must be an int. That is why I suggested using the Convert.ToInt32 on AccNum.Text. There is also built in error handling here because if AccNum.Text is not a valid int the Convert.ToInt32 operator will throw an exception.

Nathan
Oct 22 '08 #8
Curtis Rutland
3,256 Expert 2GB
Good point, I was assuming he had already verified that the text was an int somewhere else.
Oct 22 '08 #9
Good point, I was assuming he had already verified that the text was an int somewhere else.
Yeah I did, when I said AccNum is dataType int in the database and was therefore viewing it as that on lblAccNum.text. Cheers!
Oct 22 '08 #10

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

Similar topics

1
by: Amir | last post by:
Hi all, I have a table called PTRANS with few columns (see create script below). I have created a view on top that this table VwTransaction (See below) I can now run this query without a...
8
by: Jimbo | last post by:
I have a form in access 97 that is populated by a query against some sql server tables....when ever the query pulls up only one record im fine..however if it pulls up more than one record i have...
1
by: simon | last post by:
I have: par1 = New SqlParameter("@mediaId", SqlDbType.VarChar, 10) par1.Value = "text" myParams.Add(par1) par2 = New SqlParameter("@datumStart", SqlDbType.DateTime) par2.Value = Now()...
0
by: simon | last post by:
I have: par1 = New SqlParameter("@mediaId", SqlDbType.VarChar, 10) par1.Value = "text" myParams.Add(par1) par2 = New SqlParameter("@datumStart", SqlDbType.DateTime) par2.Value = Now()...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
0
by: =?Utf-8?B?RGVuaXMgU29oZXQ=?= | last post by:
Hi all, I've created a new project to test ObjectDataSource component. I've just added a new objectdatasource, configured it (linked to a table) etc ... It works fine on localhost, i can...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
1
Manikgisl
by: Manikgisl | last post by:
But the problem is we have dates in Varchar instead Datetime While Converting Varchar To Datetime All four formats are unable to Convert ie select Convert(Datetime,'18-11-2008 2:35:19...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.