Disabling character in combo box
-
This question has 2 parts...either answer would fix a problem for me My problem is that I need to query a database, and one of my parameters is the text from a combo box. If the user puts an apostrophe, then this messes up my query. So here are my questions: How would I go about pre-processing a keypress event in a combo box so I can disable specific characters from ever being typed, such as the apostrophe? ~or~ How can I prevent an apostrophe (or probably quotes too) from screwing up transferring rows? (I'm getting the error when I use this data in a Crystal Report, and when I build a select command for a data adapter, and include the text which contains the apostrophe.) Thanks for any help, I apprecaite it :-D
-
This question has 2 parts...either answer would fix a problem for me My problem is that I need to query a database, and one of my parameters is the text from a combo box. If the user puts an apostrophe, then this messes up my query. So here are my questions: How would I go about pre-processing a keypress event in a combo box so I can disable specific characters from ever being typed, such as the apostrophe? ~or~ How can I prevent an apostrophe (or probably quotes too) from screwing up transferring rows? (I'm getting the error when I use this data in a Crystal Report, and when I build a select command for a data adapter, and include the text which contains the apostrophe.) Thanks for any help, I apprecaite it :-D
You can prevent apostrophe by placing 2 single apostrophe instead of one... i.e. string strName = "baby's day out"; ..."where name='"+strName+"'"; change "strName" like this : string strName = "baby''s day out" thus after the text has been entered in the combo parse the string for single apostrophe. If present replace that with double like above example and then use the string in ur query. hope this works... regards, Aryadip. Cheers !! and have a Funky day !!
-
You can prevent apostrophe by placing 2 single apostrophe instead of one... i.e. string strName = "baby's day out"; ..."where name='"+strName+"'"; change "strName" like this : string strName = "baby''s day out" thus after the text has been entered in the combo parse the string for single apostrophe. If present replace that with double like above example and then use the string in ur query. hope this works... regards, Aryadip. Cheers !! and have a Funky day !!
-
This question has 2 parts...either answer would fix a problem for me My problem is that I need to query a database, and one of my parameters is the text from a combo box. If the user puts an apostrophe, then this messes up my query. So here are my questions: How would I go about pre-processing a keypress event in a combo box so I can disable specific characters from ever being typed, such as the apostrophe? ~or~ How can I prevent an apostrophe (or probably quotes too) from screwing up transferring rows? (I'm getting the error when I use this data in a Crystal Report, and when I build a select command for a data adapter, and include the text which contains the apostrophe.) Thanks for any help, I apprecaite it :-D
This is what happens when you don't use parameterized commands! I know that I've given you code samples before for this. Creating a SQL command by concatenating your params together is arcane and should not be used in .NET. If you use a
SqlCommand
, for example, then you should addSqlParameter
s to itsSqlCommand.Parameters
collection property and use@_paramname_
in your query without quotes. The command will do what's necessary. See the documentation for theSqlParameter
(orOleDbParameter
, or any of the others) in the .NET Framework SDK. There are also a lot of other benefits to using parameterized commands, such as easy batch processing when you save the parameters as variables and simply update theirValue
property, then re-execute the command.Microsoft MVP, Visual C# My Articles