We help IT Professionals succeed at work.

what is the issue in the code

KaranGupta
KaranGupta asked
on
DataColumn dc1 = new DataColumn("SubjectText",System.Type.GetType("System.String"));
                            
                            DataColumn dc2 = new DataColumn("SubjectID", System.Type.GetType("System.String"));
                            DataTable dt = new DataTable();
                            dt.Columns.Add(dc1);
                            dt.Columns.Add(dc2);

                            DataRow dr = dt.NewRow();

                            dr["SubjectID"] = "net20";

                            dr["SubjectText"] = DBNull.Value;

                            dt.Rows.Add(dr);

                            foreach (DataRow dr1 in dt.Rows)
                            {                                
                                Console.WriteLine(dr1["SubjectText"]);
                            }

Open in new window


If you look at the table while debugging the application blank is coming instead of null. Why


Regards
Karan Gupta
Comment
Watch Question

SwapnilSoftware Architect
Top Expert 2005

Commented:
Hi KaranGupta,

Use  SqlString.Nul instead of DBNull.Value like below.

 dr["SubjectText"] = SqlString.Null;

Thanks,
NetSwap.
SwapnilSoftware Architect
Top Expert 2005

Commented:
add following to use the SqlString type.

using System.Data.SqlTypes;

Thanks,
NetSwap.

Author

Commented:
Hi netswap

see this code

DataColumn dc1 = new DataColumn("SubjectText", System.Type.GetType("System.String"));

            DataColumn dc2 = new DataColumn("SubjectID", System.Type.GetType("System.String"));
            DataTable dt = new DataTable();
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);

            DataRow dr = dt.NewRow();

            dr["SubjectID"] = "net20";

            dr["SubjectText"] = SqlString.Null;

            dt.Rows.Add(dr);

            foreach (DataRow dr1 in dt.Rows)
            {
                if (Convert.IsDBNull(dr1["SubjectText"]))
                {
                    Console.WriteLine("dbnull");
                }
            }

Open in new window


Here dbnull is not displaying so SqlString.Null is not dbnull

Regards
Karan Gupta
Technology Development Consultant
Commented:
check code and find nullls....:-D


Regards,
nishant
DataColumn dc1 = new DataColumn("SubjectText", System.Type.GetType("System.String"));
       DataColumn dc2 = new DataColumn("SubjectID", System.Type.GetType("System.String"));
       DataTable dt = new DataTable();
       dt.Columns.Add(dc1);
       dt.Columns.Add(dc2);
       DataRow dr = dt.NewRow();
       dr["SubjectID"] = "net20";
       dr["SubjectText"] = null;
       dt.Rows.Add(dr);
       foreach (DataRow dr1 in dt.Rows)
       {
           if (dr1["SubjectText"].GetType() == System.DBNull.Value.GetType())
           {
               Console.Write("found null in value...");
           }
       }

Open in new window

SwapnilSoftware Architect
Top Expert 2005

Commented:
Hi Karangupta,

      Please find attached image. The original code you were doing is appropriate in your case. DBNull.Value is stored in column properly and its also checked with ISDBNull() function.

Thanks,
Netswap.
EE.JPG