Solved

How to skip null columns with oracle data reader

Posted on 2010-08-24
12
2,312 Views
Last Modified: 2013-12-19
Hi experts,
the code below works but how do i get it to include null columns instead of through an exception
"Column contains NULL data"

thanks
public static void Main(string[] args)
        {


            OracleConnection con = new OracleConnection();
            con = new OracleConnection("Data Source=**; User ID=**;Password=**");
            OracleCommand cmd = new OracleCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;

            cmd.CommandText = "HE_GETGRADES";
            cmd.Parameters.Add("grades_cursor", OracleDbType.RefCursor);
            cmd.Parameters["grades_cursor"].Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add("vp_id", OracleDbType.Decimal).Value = 15000028435389;

            con.Open();
            OracleDataReader reader;
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
                    reader.GetName(1));

                while (reader.Read())
                {
                    Console.WriteLine("\t{0}\t{1}", reader.GetString(0),
                        reader.GetString(1));
                }
                reader.NextResult();

                //if (reader.Read())

                //{
                //    //Console.WriteLine("My result is: " + reader.GetString(1));
                //    Console.WriteLine("{0}\t{1}", reader.GetString(0),
                //        reader.GetString(1));

                //    Console.ReadLine();

                //}
                //else
                //{
                //    Console.WriteLine("No Rows Found.");
                //    Console.ReadLine();
                //}
                con.Close();
                con.Dispose();
            }
        }
    }
}

Open in new window

0
Comment
Question by:SirReadAlot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33511161
2 options:
* either change the procedure to return non-nulls only
* change the code to check each column if it's null, and react accordingly (aka use a replacement value)
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 33511221
At the .Net code layer check out:
http://forums.oracle.com/forums/thread.jspa?threadID=598612

Convert.IsDBNull(dr[1]))
0
 

Author Comment

by:SirReadAlot
ID: 33511809
orry meant this, but it still give the same error

while (reader.Read() || (Convert.IsDBNull(reader[1])))
            {
                Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
                    reader.GetName(1));

                while (reader.Read())
                {
                    Console.WriteLine("\t{0}\t{1}", reader.GetString(0),
                        reader.GetString(1));
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 33511832
Why do you have two while loops?  You only need one.

I'll work on expanding my test from your previous question.  I'll post it soon.
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 33512142
Here's what I came up with.  If there's still problems, please add/subtract to my test SQL so I can reproduce the error on my end.

Given the database objects:
-----------------------------------
drop table tab1 purge;
create table tab1(col1 number, someColumn varchar2(20), thirdColumn char(1));

insert into tab1 values(15000028435389,'Hello','Z');
insert into tab1 values(25000028435389,'World','Z');
insert into tab1 values(25000028435389,null,'Z');
commit;

create or replace function myFunc (inputID number)
return sys_refcursor
is
myresult sys_refcursor;
begin
open myresult for select * from tab1 where col1=inputID;

return myresult;
end;
/



using System;
using System.Data;

using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

public class Bob
{

	public static void Main(string[] args)
	{


			OracleConnection con = new OracleConnection();

			con = new OracleConnection("User Id=bud;Password=bud;Data Source=bud");

			OracleCommand cmd = new OracleCommand();
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection = con;
			cmd.CommandText = " myFunc ";

			cmd.Parameters.Add("myResult", OracleDbType.RefCursor);

			cmd.Parameters["myResult"].Direction = ParameterDirection.ReturnValue;

			cmd.Parameters.Add("myInput", OracleDbType.Int64).Value = 25000028435389;

			con.Open();


      		OracleDataReader reader;
			bool haveRows = false;

	        reader = cmd.ExecuteReader();
	        
	        	while (reader.Read()) {
					Console.WriteLine("=========================");
					Console.WriteLine("Column1 has: " + (Convert.IsDBNull(reader.GetValue(0))?"null":reader.GetDecimal(0).ToString()));
					Console.WriteLine("Column2 has: " + (Convert.IsDBNull(reader.GetValue(1))?"null":reader.GetString(1)));
					Console.WriteLine("Column3 has: " + (Convert.IsDBNull(reader.GetValue(2))?"null":reader.GetString(2)));
					haveRows = true;
				}


				if(!haveRows) {
					Console.WriteLine("No Rows Found.");
				}

			con.Close();
			con.Dispose();

	}

}

Open in new window

0
 

Author Comment

by:SirReadAlot
ID: 33512215
ok  2sec
0
 

Author Comment

by:SirReadAlot
ID: 33512325
HI,

no errors with your sql,

but i received Specified cast is not valid. in line 1

            >>    Console.WriteLine("Column1 has: " + (Convert.IsDBNull(reader.GetValue(0)) ? "null" : reader.GetDecimal(0).ToString()));

thanks
0
 

Author Comment

by:SirReadAlot
ID: 33512349
DONE THIS

 Console.WriteLine("Column1 has: " + (Convert.IsDBNull(reader.GetValue(0)) ? "null" : reader.GetString(0).ToString()));
                Console.WriteLine("Column2 has: " + (Convert.IsDBNull(reader.GetValue(1)) ? "null" : reader.GetString(1).ToString()));
                Console.WriteLine("Column3 has: " + (Convert.IsDBNull(reader.GetValue(2)) ? "null" : reader.GetString(2).ToString())); >>ERRORS HERE so i will check thre datatype
                haveRows = true;
0
 

Author Comment

by:SirReadAlot
ID: 33512379
hi
Console.WriteLine("Column1 has: " + (Convert.IsDBNull(reader.GetValue(0)) ? "null" : reader.GetString(0).ToString()));
                Console.WriteLine("Column2 has: " + (Convert.IsDBNull(reader.GetValue(1)) ? "null" : reader.GetString(1).ToString()));
                Console.WriteLine("Column3 has: " + (Convert.IsDBNull(reader.GetValue(2)) ? "null" : reader.GetOracleDecimal(2).ToString()));

works now, so to view the rest of the colums I will have to keep adding
Console.WriteLine("Column1 has: " + (Convert.IsDBNull(reader.GetValue(0)) ? "null" : reader.GetString(0).ToString()));
                Console.WriteLine("Column2 has: " + (Convert.IsDBNull(reader.GetValue(1)) ? "null" : reader.GetString(1).ToString()));
                Console.WriteLine("Column3 has: " + (Convert.IsDBNull(reader.GetValue(2)) ? "null" : reader.GetOracleDecimal(2).ToString()));

right
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 33512442
Correct.  You need to make sure the column offset is correct in the GetValue and Get<DataType> calls and the data types match.

So the next column would have offset (3).

Console.WriteLine("Column4 has: " + (Convert.IsDBNull(reader.GetValue(3)) ? "null" : reader.GetOracleDecimal(3).ToString()));

0
 

Author Comment

by:SirReadAlot
ID: 33513505
many thanks once again
0
 

Author Closing Comment

by:SirReadAlot
ID: 33513516
what a cool teacher!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question