Question : How to skip null columns with oracle data reader

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
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
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();
            }
        }
    }
}

Answer : How to skip null columns with oracle data reader

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;
/


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
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();

	}

}
Random Solutions  
 
programming4us programming4us