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

How can I improve this code please?

How can I improve this code please?
It sometimes produces this error:
"Object reference not set to an instance of an object"
When I do this:
strSQL = "UPDATE TBL_Items SET" & _
" item_itemnumber = " & PrepareStr(Label6.Text) & _
" ,item_itemcurrentbidprice = " &
PrepareStr(itemCurrentbidprice) & _
" ,item_itembidhistory = " &
PrepareStr(itembidHistory) & _
" ,item_itemcurrentorpurchasedbidder = " &
PrepareStr(itemCurrentorpurchasedbidder) & _
"FROM TBL_Items " & _
"WHERE item_itemnumber = '" & Label6.Text & "'"

Here is the code:

Private Function PrepareStr(ByVal strValue As String) As String
' This function accepts a string and creates a string that can
' be used in a SQL statement by adding single quotes around
' it and handling empty values.
If strValue.Trim() = "" Then
Return "NULL"
Else
Return "'" & strValue.Trim() & "'"
End If
End Function

Thanks,
Trint
.Net programmer
tr********@hotmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
4 1144
It looks like your function will return the word NULL to your calling
function, when what it looks like you want to return is 'NULL'. Another
idea is to return String.Empty instead of "NULL".

Stephajn
"Trint Smith" <tr********@hotmail.com> wrote in message
news:u8******************@TK2MSFTNGP10.phx.gbl...
How can I improve this code please?
It sometimes produces this error:
"Object reference not set to an instance of an object"
When I do this:
strSQL = "UPDATE TBL_Items SET" & _
" item_itemnumber = " & PrepareStr(Label6.Text) & _
" ,item_itemcurrentbidprice = " &
PrepareStr(itemCurrentbidprice) & _
" ,item_itembidhistory = " &
PrepareStr(itembidHistory) & _
" ,item_itemcurrentorpurchasedbidder = " &
PrepareStr(itemCurrentorpurchasedbidder) & _
"FROM TBL_Items " & _
"WHERE item_itemnumber = '" & Label6.Text & "'"

Here is the code:

Private Function PrepareStr(ByVal strValue As String) As String
' This function accepts a string and creates a string that can
' be used in a SQL statement by adding single quotes around
' it and handling empty values.
If strValue.Trim() = "" Then
Return "NULL"
Else
Return "'" & strValue.Trim() & "'"
End If
End Function

Thanks,
Trint
Net programmer
tr********@hotmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2
* Trint Smith <tr********@hotmail.com> scripsit:
How can I improve this code please?
It sometimes produces this error:
"Object reference not set to an instance of an object"


On which line?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet/>

..NET FAQs: <http://www.mvps.org/dotnet/dotnet/faqs/> (German)
Nov 20 '05 #3
Cor
Hi Trint,

Have a look at command.parameters that is made for this.

http://msdn.microsoft.com/library/de...eterstopic.asp

I hope this helps,

Cor
How can I improve this code please?
It sometimes produces this error:
"Object reference not set to an instance of an object"
When I do this:
strSQL = "UPDATE TBL_Items SET" & _
" item_itemnumber = " & PrepareStr(Label6.Text) & _
" ,item_itemcurrentbidprice = " &
PrepareStr(itemCurrentbidprice) & _
" ,item_itembidhistory = " &
PrepareStr(itembidHistory) & _
" ,item_itemcurrentorpurchasedbidder = " &
PrepareStr(itemCurrentorpurchasedbidder) & _
"FROM TBL_Items " & _
"WHERE item_itemnumber = '" & Label6.Text & "'"

Here is the code:

Private Function PrepareStr(ByVal strValue As String) As String
' This function accepts a string and creates a string that can
' be used in a SQL statement by adding single quotes around
' it and handling empty values.
If strValue.Trim() = "" Then
Return "NULL"
Else
Return "'" & strValue.Trim() & "'"
End If
End Function

Nov 20 '05 #4
thanks Cor.
Herfried:
If strValue.Trim() = "" Then
Return "NULL"


Thanks,
Trint

.Net programmer
tr********@hotmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: | last post by:
I am trying to improve the robustness and elegance of my parametized sql statements in ASP 3.0 as they get passed to the sql server SP. Could anyone tell me if there are weaknessess in the way I...
7
by: Bing Wu | last post by:
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL ,...
9
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
9
by: goosen_cug | last post by:
This program is a "Sequential List" class I want to do the Union Operation,Intersection Operation of the Set.But this program have a problem: /////////////////////////// Compiling... Set.cpp...
16
by: weidongtom | last post by:
Hi, I have just finished reading some tutorials on C, I am wondering how I could improve my skill. Is there any advice? Is reading others' codes the best way? If so, what type of codes are...
11
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem...
8
by: tony | last post by:
Hi, now I've known the basics about C++. What I can do is pretty limited. I've known something in Java: 1.Networking(Socket, RMI, Servlet) 2.Database(JDBC) 3.GUI (Java Swing) 4.and some others...
2
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
3
by: oravm | last post by:
Hi, I re-write a query and used bulk collect to improve the performance of the batch process. The query below has NO compile error but when execute query there is error 'ORA-01403: no data...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.