Need help with code...

J

JJ297

I have a field called Office Code on my page. It's not mandatory that
it be filled in but if it's not filled in I want 0000 to go into the
database. How do I write this? I attempted it below.

Dim cmd As New Data.SqlClient.SqlCommand
With cmd
.Connection = conn
.CommandType = Data.CommandType.StoredProcedure
.CommandText = "AddLoanRequest"
.Parameters.AddWithValue("@TitleID",
Integer.Parse(Request.QueryString("TitleID")))
.Parameters.AddWithValue("@RequestDate", LoanDate.Text)
.Parameters.AddWithValue("@description",
Request.QueryString("classificationid"))
.Parameters.AddWithValue("@FName", FName.Text)
.Parameters.AddWithValue("@LName", LName.Text)
.Parameters.AddWithValue("@PhoneNum", PhNoTxt.Text)
If OfcCodeTxt.Text = "" Then
OfcCodeTxt.Text = "0000"
Else
.Parameters.AddWithValue("@OfficeCode",
OfcCodeTxt.Text)
End If


End With
 
A

Aidy

If OfcCodeTxt.Text.Trim() = "" Then
.Parameters.AddWithValue("@OfficeCode", "0000")
Else
.Parameters.AddWithValue("@OfficeCode", OfcCodeTxt.Text)
End If
 
J

JJ297

If OfcCodeTxt.Text.Trim() = "" Then
                .Parameters.AddWithValue("@OfficeCode", "0000")
            Else
                .Parameters.AddWithValue("@OfficeCode", OfcCodeTxt.Text)
            End If








- Show quoted text -

Okay thanks can you tell me what why am I using the word trim()?

If OfcCodeTxt.Text.Trim() = "" Then
 
M

Mark Rae [MVP]

Why not set "0000" as the default value for that column in the table?

Indeed, or set it as the default value for the parameter in the stored
procedure:

CREATE PROCEDURE AddLoanRequest
-- other parameters
@OfficeCode char(4) = '0000'

AS
-- rest of procedure
 
A

Aidy

Okay thanks can you tell me what why am I using the word trim()?

It's not needed, but it removes any training or leading spaces from the text
box. It stops people getting around mandatory fields by just entering
spaces, it also strips the space from words that people have copied from
documents like Word etc. When you click a word to highlight it, often it
selects the word and the trailing space, and that can lead to much
head-scratching on your end when there are hidden spaces in the database.

I'd also go one further and suggest you don't check for "" either, but
instead check if the Length = 0. Strings contain their length internally so
checking the length of a string is fast.
 
A

Aidy

Why not set "0000" as the default value for that column in the table?

I'm not keen on default values in tables as you can forget they are there.
I prefer to always handle this stuff in the code so that you always know
where the table's values are coming from.
 
J

JJ297

I meant trailing spaces.

Thanks everyone for your replies. It's working and now I know how to
handle it both ways.

Thanks Aidy for the explanation of leading and trailing spaces too.
 
B

BobF

Aidy said:
I'm not keen on default values in tables as you can forget they are there.
I prefer to always handle this stuff in the code so that you always know
where the table's values are coming from.

A simple comment in the code that says "'0000' is set as a default ..."
would suffice for me.
 
A

Aidy

A simple comment in the code that says "'0000' is set as a default ..."
would suffice for me.

Surely it's the same effort to code it so 0000 is the default than it is to
comment it :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top