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

Inserting alphanumeric into SQL db?

13
Hi everyone,

I'm hoping someone can help me. I've created a windows app that inserts data into a sql database.

The data comes from textboxes. This is a simple app that will only be used by me on a local computer so I'm not worried about sql injections.

My problem is this...if I enter a job id that contains all numbers (ie. 12345) into textBox1 everything works fine and the database inserts the new record. But if I enter a job id that is alphanumeric (ie. 12345b) I receive a syntax error.

In my database the field is set as varchar(MAX) so I don't understand why this is throwing an error. If I manually insert a alphanumeric job id into the db it works. This is only happening when trying to update via the win form. Here is the code and thanks for your time...

Expand|Select|Wrap|Line Numbers
  1. //Build Commands ---------------------------------------------
  2. SqlCommand mySqlCommand = db.CreateCommand();
  3. int log_total = Convert.ToInt32( txt_total.Text) - Convert.ToInt32(starting_total) ;
  4. string date = String.Format("{0:yyyyMMdd}", DateTime.Now);
  5.  
  6.  
  7. // THIS ONE IS THE PROBLEM ------------------------------------
  8. string jobID = textBox1.Text;
  9.  
  10.  
  11. string update_log = jobID + "," + starting_total + "," + txt_total.Text + "," + log_total + "," + date; 
  12.  
  13. mySqlCommand.CommandText =
  14. "INSERT INTO Log (Job, Start, Finish, Total, Date)" +  " VALUES (" + update_log + ")"; 
  15.  
  16.  
  17.  
  18. // Open, Execute, Close ----------------------------------------
  19.  
  20. db.Open();
  21.  
  22. SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
  23.  
  24. db.Close();
Jan 22 '09 #1
3 1548
Paul73
13
Nevermind. I was missing a ' around the JobID field.
Jan 22 '09 #2
Curtis Rutland
3,256 Expert 2GB
I'd really suggest that you use String.Format or use parameters. I know it's just for you, but using either of those makes your code far more maintainable and readable, and it would be much easier to spot those errors. In fact, if you used parameters, that error would never have even come up.
Jan 23 '09 #3
Paul73
13
I'd really suggest that you use String.Format or use parameters. I know it's just for you, but using either of those makes your code far more maintainable and readable, and it would be much easier to spot those errors. In fact, if you used parameters, that error would never have even come up.
Thanks, I'll take your advice. Have a great weekend!
Jan 24 '09 #4

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

Similar topics

3
by: Daniel Tonks | last post by:
OK, here's possibly a weird one. Is there any way to do string comparisons and ignore all non-alphanumeric characters? For instance, check "foobar" and have it match an existing record of "f$#!oo...
1
by: Avnish | last post by:
Hi, I am looking for some form of validation for all the alphanumeric characters in the entire unicode range e.g. the validation should also accept japanese characters but should restrict...
4
by: Matt | last post by:
I want the javascript to test an alphanumeric (a string contains alphabet or numbers only) string. Should I write a regular expression? What's the best way to do? please help. thanks
6
by: ironcito | last post by:
Hello! I'm looking for a way to have a field in my database that will automatically be filled with a random 4-character alphanumeric string every time I enter a new record. Like an autonumber...
9
by: fooboo | last post by:
Does anyone know if a easier way (built in function, or something) that can verify that a string is an alphanumeric number? Here is what I am doing now: for(i=0; i < strlen(temp); i++){...
3
by: Amaryllis | last post by:
Hi again, I'm new to the world of communicating between VB.NET and AS/400, so I've been posting a lot of questions lately since no one else in the company has done anything like this before. ...
7
by: Fernando Rodríguez | last post by:
Hi, How can I know if a string only has alfanumeric chars? Thanks
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
7
by: kanepart2 | last post by:
Hey all, I have to validate a textbox in windows forms for alphanumeric characters such that non alphanumeric key presses are ignored. Some help would be appreciated
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: 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?
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
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,...

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.