Need help with code...

  • Thread starter Thread starter JJ297
  • Start date Start date
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
 
If OfcCodeTxt.Text.Trim() = "" Then
.Parameters.AddWithValue("@OfficeCode", "0000")
Else
.Parameters.AddWithValue("@OfficeCode", OfcCodeTxt.Text)
End If
 
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
 
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
 
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.
 
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.
 
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.
 
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 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 :)
 
Back
Top