Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello All,

 

I have created an application that pulls data from a database using an OleDbDataReader. It used a wile loop to collect the data from each line. Now this works fine until it comes across an empty cell in the database.

I have caught the exception “No value for one or more required parameters”.

 

How do I get the OleDbDataReader to read an empty cell?

 

Thanks for your help,

Harold Clements

Posted

There are several tricks to over this exception

 

1-> use the simple ToString() to collect your data as string value

 

Exmaple >>

 

cn.Open()

Dim rdr As OleDb.OleDbDataReader = cmd.ExecuteReader

While rdr.Read()

Me.lsbUseName.Items.Add(rdr("userName")).ToString()

End While

rdr.Close()

cn.Close()

 

 

Or another way to do it

 

Dim strSQL As String

Dim cnn As New OleDb.OleDbConnection(DATAPATH)

Try

cnn.Open()

strSQL = "SELECT AcYear FROM ACYears"

Dim cmd As New OleDb.OleDbCommand(strSQL, cnn)

Dim rdr As OleDb.OleDbDataReader = _

cmd.ExecuteReader(CommandBehavior.CloseConnection)

Dim ACYear As String = rdr.GetOrdinal("AcYear")

Do While rdr.Read()

CBoxYear.Items.Add(rdr("ACYear").ToString())

Loop

rdr.Close()

cmd.Dispose()

Catch err As Exception

MessageBox.Show(err.Message)

End Try

 

 

another solution is to use IS NOT NULL T-SQL

 

by saying that if the current return value is not null

return it otherwise forget it

 

Example >> see the last condition if Grade is Not Null pull the data otherwise skip it

 

strSQL1 = "SELECT Grade FROM DetailsRegistration " & _

"WHERE CourseDescriptions ='" & Course & _

"' AND StudentID ='" & txtID.Text & "' AND Grade IS NOT NULL "

 

Or another way to do it

 

By using the isNull function

 

search data while loading it if any is null

then replace it with UNKNOWNVALUE or any other you desired ...

( check_expression , replacement_value )

 

ISNULL(MYCOLUMN,UNKNOWNVALUE)

 

GOOD LUCK

Gary Says: To be a good programmer, you must be good at debugging
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...