|
|||||||
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
Addicted Member
Join Date: Jul 07
Location: VBForums.com
Posts: 131
![]() |
Hello to you all,
I have searched almost the all forum to resolve my problem but without any positive effect. I have a problem reading from the database some values. If one of the value is empty i get this error message: Conversion from type 'DBNull' to type 'String' is not valid. How can i pass thru this problem ? Please give me a suggestion and how can i optimize this code better! Code:
Dim denumire_furnizor, tip_unitate, statut_juridic As String
conn.Open()
Dim sql_1 = "SELECT denumire FROM unitatea_economica"
cmd = New OleDb.OleDbCommand(sql_1, conn)
denumire_furnizor = CStr(cmd.ExecuteScalar)
Dim sql_1_1 = "SELECT tip_unitate FROM unitatea_economica"
cmd = New OleDb.OleDbCommand(sql_1_1, conn)
tip_unitate = CStr(cmd.ExecuteScalar)
Dim sql_1_2 = "SELECT statut_juridic FROM unitatea_economica"
cmd = New OleDb.OleDbCommand(sql_1_2, conn)
statut_juridic = CStr(cmd.ExecuteScalar)
Me.lbl_furnizor.Text = tip_unitate & " " & denumire_furnizor & " " & statut_juridic
If conn.State <> ConnectionState.Closed Then
conn.Close()
End If
|
|
|
|
|
|
#2 |
|
Loquacious User
Join Date: Aug 02
Location: Idaho
Posts: 11,014
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Conversion from type 'DBNull' to type 'String' is not valid
The queries you have written will return just the first value from the table you are querying (if it is a table), which seems a bit peculiar. Usually there's a WHERE clause in there to reduce the number of records, but that's not the cause of the problem. The problem comes from the fact that one or all of these fields have a Null value in the first record.
I can't say that I have ever tried to check for Null on an ExecuteScalar call before, but I would think....that it would be a pain. If you simply check whether cmd.ExecuteScalar returns a Null, that would require two calls. Better would be to put the data into an object and test the object. However, if you have access to the database, and control over the design of it, the best solution would be to not have Null (empty) values in those fields. Give them a default value of pretty much anything. I think even an empty string ("") would do.
__________________
My usual boring signature: Nothing |
|
|
|
|
|
#3 |
|
PowerPoster
Join Date: Feb 06
Location: East of NYC, USA
Posts: 5,692
![]() ![]() ![]() ![]() |
Re: Conversion from type 'DBNull' to type 'String' is not valid
Code:
denumire_furnizor = CStr("" & cmd.ExecuteScalar)
__________________
The most difficult part of developing a program is understanding the problem. The second most difficult part is deciding how you're going to solve the problem. Actually writing the program (translating your solution into some computer language) is the easiest part. Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read. Please Help Us To Save Ana |
|
|
|
|
|
#4 |
|
Frenzied Member
Join Date: Oct 05
Location: Somewhere just west of the Atlantic
Posts: 1,564
![]() ![]() ![]() ![]() |
Re: Conversion from type 'DBNull' to type 'String' is not valid
I ran into the same issue and wrote a little function
Code:
Public Function FixNull(ByVal o As Object) As Object
If IsDBNull(o) Then
Return Nothing
Else
Return o
End If
End Function
Code:
denumire_furnizor = FixNull(cmd.ExecuteScalar)
__________________
Boooya
![]() Code Contributions: PHP PHP Image Gallery v1.0 • PHP Image Gallery v2.0 VB 2005 Find Computers on a network • Simple License Encryption • SQL Server Database Access dll • Use Reflection to Return Crystal ReportDocument • Silently Print PDF • Generic Xml Serailizer Useful Links: (more to come) MSDN (The first and foremost) • MSDN Design Guidelines • API Reference • Inno Setup Compiler • Inno Setup Preprocessor • ISTool - Fairly easy to use GUI for creating inno setup projects • Connection Strings • NAnt -Automated Builds • Cruise Control .NET - Frontend for automated builds
|
|
|
|
|
|
#5 |
|
Fanatic Member
Join Date: Mar 07
Location: Atlanta, GA USA
Posts: 524
![]() ![]() ![]() |
Re: Conversion from type 'DBNull' to type 'String' is not valid
Use String.Concat
vb Code:
|
|
|
|
|
|
#6 |
|
Addicted Member
Join Date: Jul 07
Location: VBForums.com
Posts: 131
![]() |
Re: Conversion from type 'DBNull' to type 'String' is not valid
Many thanks to all of you!!!
Shaggy Hiker Yes it's a table with only one row. Thank you for the indications! My problem is now solved... I have used "bmahler" solution. I dont know if it is the best one but it works and seems to be more simple. Many thanks to all of you again!!! |
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|