I need some help, cant figure out the error. - SqlConnection dbConnection = new SqlConnection(xmldb);
-
dbConnection.Open();
-
string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + lblAccNum.Text + "' ";
-
SqlCommand command = new SqlCommand(sql, dbConnection);
-
SqlDataReader reader = command.ExecuteReader();
-
while (reader.Read())
-
{
-
InvoiceStatus = reader[0].ToString();
-
}
-
reader.Close();
-
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
9 24259
Change the Select statement to look like this - string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + Convert.ToInt32(lblAccNum.Text) + "' ";
-
Nathan
Change the Select statement to look like this - string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = '" + Convert.ToInt32(lblAccNum.Text) + "' ";
-
Nathan
Thanks, but when I try that I get this error:
"Input string was not in a correct format."
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 - string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + Convert.ToInt32(lblAccNum.Text);
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: -
string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + lblAccNum.Text;
-
Better yet, learn how to use String.Format" -
string sql = String.Format("SELECT InvStatus FROM SaleReceipts Where AccNum = {0}", lblAccNum.Text);
-
The best way would be to use a parameterized query. -
string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = @AccNum";
-
SqlCommand command = new SqlCommand(sql, dbConnection);
-
command.Parameters.AddWithValue("@AccNum", lblAccNum.Text);
-
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.
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: -
string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = " + lblAccNum.Text;
-
Better yet, learn how to use String.Format" -
string sql = String.Format("SELECT InvStatus FROM SaleReceipts Where AccNum = {0}", lblAccNum.Text);
-
The best way would be to use a parameterized query. -
string sql = "SELECT InvStatus FROM SaleReceipts Where AccNum = @AccNum";
-
SqlCommand command = new SqlCommand(sql, dbConnection);
-
command.Parameters.AddWithValue("@AccNum", lblAccNum.Text);
-
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
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
Good point, I was assuming he had already verified that the text was an int somewhere else.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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()...
|
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()...
|
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
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |