Showing posts with label form. Show all posts
Showing posts with label form. Show all posts

Friday, March 23, 2012

Radio button - not reqire to select!

I am currently using a web form to insert data into SQL DB.
The radio button for Gender is not a require field.
currently if I use this command is in VB
' cmd.Parameters.Add(New SQLParameter("@.Gender", rblGender.SelectedItem.text)) >
it work fine if the user select one of the radio button, if the user don't select any button then it will give an error.
so I thought I can try the if else statement to see if it work, unfortunately it didn't either and error out on rblGender.SelectedItem.text
ex.
If rblGender.SelectedItem.text <> "" then
cmd.Parameters.Add(New SQLParameter("@.Gender", rblGender.SelectedItem.text))
Else
cmd.Parameters.Add(New SQLParameter("@.Gender", ""))
End IF
Any help would be appreciated.
hydro

You could do something like this:
If rblGender.SelectedIndex <> -1 then
cmd.Parameters.Add(New SQLParameter("@.Gender", rblGender.SelectedItem.text))
Else
cmd.Parameters.Add(New SQLParameter("@.Gender", DbNull.Value))
End IF
|||

That work!! Thanks!!

hydro

sql

Wednesday, March 21, 2012

Quotes within a query

Help, please.

I'm going crazy trying to figure out how to form this SQL query. I am querying an Informix linked server and I need to pass a variable date. I am using an expression to create the query like so

"Select count(*) from " + @.[User::varDBName] + ":informix.doc_tl WHERE " + @.[User::varDBName] +":informix.doc_tl.d_received = {D " + @.[User::varDate] +"} "

The informix query needs the date to be {D "2007-01-15"} but for the life of me, I can't get the date enclosed in quotes.

The error I get is

An OLE DB record is available. Source: "(null)". HResult: 0x80040E14

Description: "(null)"

Can anyone tell me what I'm doing wrong?

Thanks

Hi Chris,

Can you try using single quote character (') or escaped quote character i.e. using quote twice ("")?

-- Waseem

|||

Hi!

Thanks for the reply. I thought I tried both but upon retry, the single quote character worked. I do get a code page warning but I think that is OK.

Thanks so much!

Tuesday, March 20, 2012

Quote in input field yeilds error

Hello!

I have an input form that contains a textarea in which people can input the description of an item. They then click Insert or Update and the information is inserted or updated to a SQL Server database. Everything works fine unless someone includes a quote in the description. For example:

The item is Bob's computer.

The apostrophe in Bob creates a problem. I receive the following:

Incorrect syntax near 's'. Unclosed quotation mark before the character string '.

I understand the problem. How do I correct it?

Thanks!

PS I am using C#.Use parameters.

See here|||OF COURSE!! I knew I had done this at some time...thanks for JOGGING my brain!! :)|||I have the same problem, but I don't see how that tutorial would work for an imput text box. If the user types in something like "Mike's car" (without the quotes), it ruins the sql string. How can I code around, or get the server to accept single quotes or apostrophes?
Thanks,
Sean|||The previous link will in fact resolve the problem. Honest.

A poorer alternative is replacing all ' with two ' characters ('' - this is NOT a regular quote, but two single quotes). Doing this still allows SQL Injection attacks to occur.|||Sorry, but I don't see how to apply it to an update statement. Here's a piece of my code:


Sub btnSubmit_Click(sender As Object, e As EventArgs)
Dim strPurpose as string =txtPurpose.text
Dim MySQL as string = "Insert into tbl_ExpsReports (expsPurpose) values ('" & strPurpose & "')"
Dim myConn As New OLEDBConnection(configurationSettings.AppSettings("MSDBconn"))
Dim Cmd as New OleDbCommand(MySQL, MyConn)

MyConn.Open()
cmd.ExecuteNonQuery
MyConn.close()
End Sub

How do I allow the user to key in a single quote or apostrope into the txtPurpose text box? The tutorial seems to be geared towards a return rather than input statement.
Thanks,
Sean|||What you are doing is not that unusual:

Sub btnSubmit_Click(sender As Object, e As EventArgs)
Dim strPurpose as string =txtPurpose.text
Dim MySQL as string = "Insert into tbl_ExpsReports (expsPurpose) values (?)"

Dim myConn As New OLEDBConnection(configurationSettings.AppSettings("MSDBconn"))
Dim Cmd as New OleDbCommand(MySQL, MyConn)
Cmd.Parameters.Add("expsPurpose",strPurpose)

MyConn.Open()
cmd.ExecuteNonQuery
MyConn.close()
End Sub
</code>|||So, what you are saying is, if I use a perameterized insert statement, then the user can key in an apostrophe or single quote? Cool! ;^]|||Yes. And prevents SQL Injection.|||SQL injection... hmmmm... sounds bad.

Wednesday, March 7, 2012

Quick Query

Our dev database was not built correctly for phone #'s so all of the
sample data or test data (not sure if it is legit) has them in the form
of XXX-YYY-ZZZZ. Is there a query that can remove the dashes from
those values and turn it into XXXYYYZZZZ?
Thanks.Try:
update MyTable
set
Phone = replace (Phone, '-', '')
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
<josh.tucholski@.gmail.com> wrote in message
news:1156253610.284442.275620@.i3g2000cwc.googlegroups.com...
Our dev database was not built correctly for phone #'s so all of the
sample data or test data (not sure if it is legit) has them in the form
of XXX-YYY-ZZZZ. Is there a query that can remove the dashes from
those values and turn it into XXXYYYZZZZ?
Thanks.|||update YourTable
set PhoneNumber = replace(PhoneNumber, '-', '')
-Sue
On 22 Aug 2006 06:33:30 -0700, josh.tucholski@.gmail.com
wrote:
>Our dev database was not built correctly for phone #'s so all of the
>sample data or test data (not sure if it is legit) has them in the form
>of XXX-YYY-ZZZZ. Is there a query that can remove the dashes from
>those values and turn it into XXXYYYZZZZ?
>Thanks.