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

Problem comparing database values with textbox values

Expand|Select|Wrap|Line Numbers
  1. SqlConnection cn = new SqlConnection();
  2. cn.ConnectionString = "Data Source= ;Database= ;uid= ;pwd= ";
  3. cn.Open();
  4. string sqt = "Select status from tracking where no=@o";
  5. SqlCommand cmr = new SqlCommand(sqt, cn);
  6. cmr.Parameters.AddWithValue("@o", Convert.ToInt32(textBox1.Text));
  7. status = Convert.ToString(cmr.ExecuteScalar());
  8. if (status == "abcd")
  9.     MessageBox.Show("Record updated");
  10. else
  11.      MessageBox.Show("Record not updated");
  12. cn.Close();
I hav one table named tracking with no,status as its feild.the data type of status is varchar.
I want that when status is abcd then it should show the mess. record updated else the other one mess.
But when i m debugging it,even if i m giving the no for which status is abcd,it is showing the else mess.
The main problem is that if condition always evaluates to false even if status in database is abcd.
plzzzz reply soon.thanks for support.
Jul 27 '08 #1
10 1956
Curtis Rutland
3,256 Expert 2GB
Code tags are a requirement.

Please read and follow the Posting Guidelines.

MODERATOR

Perhaps the value you are getting from the DB has spaces or something.
Jul 27 '08 #2
vanc
211 Expert 100+
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source= ;Database= ;uid= ;pwd= ";
cn.Open();
string sqt = "Select status from tracking where no=@o";
SqlCommand cmr = new SqlCommand(sqt, cn);
cmr.Parameters.AddWithValue("@o", Convert.ToInt32(textBox1.Text));
status = Convert.ToString(cmr.ExecuteScalar());
Couldn't see any problems but your queries. Try to make some straight queries to check status data to see if there is any problem and try different type of data execution. Like below, I'll use DataReader to get data.

Expand|Select|Wrap|Line Numbers
  1. cmd.CommandText = "Select status from tracking where no=" + int.Parse(textBox1.Text);
  2. SqlDataReader reader = cmd.ExecuteQuery(); //recheck for this statement since I can't recall the right command
  3. reader.Read();
  4. status = reader["status"].ToString();
  5. reader.Close();
  6.  
Cheers.
Jul 27 '08 #3
Couldn't see any problems but your queries. Try to make some straight queries to check status data to see if there is any problem and try different type of data execution. Like below, I'll use DataReader to get data.

Expand|Select|Wrap|Line Numbers
  1. cmd.CommandText = "Select status from tracking where no=" + int.Parse(textBox1.Text);
  2. SqlDataReader reader = cmd.ExecuteQuery(); //recheck for this statement since I can't recall the right command
  3. reader.Read();
  4. status = reader["status"].ToString();
  5. reader.Close();
  6.  
Cheers.
This code is not working.plz suggest something else.Error is coming in this code.
Jul 28 '08 #4
Code tags are a requirement.

Please read and follow the Posting Guidelines.

MODERATOR

Perhaps the value you are getting from the DB has spaces or something.
Thanks for help.
You r talking about spaces in DB,so can u plz tell me what should i do so that my code works.
plz reply soon....
Jul 28 '08 #5
r035198x
13,262 8TB
Thanks for help.
You r talking about spaces in DB,so can u plz tell me what should i do so that my code works.
plz reply soon....
If the DB values contain spaces then you can use the TRIM function to get rid of them.
Jul 28 '08 #6
If the DB values contain spaces then you can use the TRIM function to get rid of them.
In database there is no problem of space,but the code is not working.Its not the problem of space in database,even Datareader is giving same output as before (executing else part),plzzz help.
Jul 28 '08 #7
This code is not working.plz suggest something else.Error is coming in this code.
Datareader is same output as before.
Jul 28 '08 #8
Curtis Rutland
3,256 Expert 2GB
OK...try this. After you do your select, instead of doing a comparison, show a MessageBox like this:
Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show("Status Value: -" + status + "-");
That way, you can see exactly what the value is that your program is getting (I added the "-" so you can see if there are any leading/trailing spaces). This should help you debug the problem further.
Jul 28 '08 #9
OK...try this. After you do your select, instead of doing a comparison, show a MessageBox like this:
Expand|Select|Wrap|Line Numbers
  1. MessageBox.Show("Status Value: -" + status + "-");
That way, you can see exactly what the value is that your program is getting (I added the "-" so you can see if there are any leading/trailing spaces). This should help you debug the problem further.
The only change i made now as compared to before is that i changed the data type of status as text.but it was not working then also.
Then I only added this line(u send me) in my code n kept the type as text.The mess.box is showing status: -abcd-.and then i added the if condition same as before and it started working.But still now i dont know the correct reason why it was not executing before.Thanks a lot for the support.
thank u all for the support n timely response.
Jul 28 '08 #10
r035198x
13,262 8TB
..But still now i dont know the correct reason why it was not executing before....
Perhaps it was just build issue then. (i.e your new code wasn't being automatically picked).
Good luck with the rest of it.
Jul 29 '08 #11

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

Similar topics

0
by: Giulio Santorini | last post by:
Hi, I've got two drop down list controls. When I select a value from the first one I fill the second one. And when I select a value from the second one I would like to fill another control. But...
7
by: Peter D.C. | last post by:
Hi I want to update data hold in several textbox controls on an asp.net form. But it seems like it is the old textbox values that is "re-updates" through a stored procedure who updates a SQL...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
4
by: pankajsingh5k | last post by:
Hi guys, These question is for all the experts... Please help me before my brain explodes The problem is again with the formview control.. I have a formview and i have to use it that...
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:
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
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: 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
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.