C# .NET - No Row at postion 0 - Asked By Romila C on 15-Aug-11 01:56 PM

Hi, I tried to read datas from MS Access table. Now, some Columns there are having a null values.

How can I fix this error if No Row at position 0.

This the select statement I use

SELECT   [GROUP #], [CONTACT 1], [CONTACT 2], [  Contact 3]
FROM     [ Contact]
WHERE   ([GROUP #] = ?)

So when I try to look up the Contact table there are rows that has no records on there. My question is how to fix this error?
 Here is my other code

myContact = myDataTable[0][1].ToString();


I don't wanna change it.

Is there a technique that ignore reading this table if it contain a null values?
Devil Scorpio replied to Romila C on 15-Aug-11 02:38 PM
Hi Romila,

Use DBNull function to ignore reading table if a column contains null value

If (! IsDBNull(
myDataTable[0][1])) then

   myContact = myDataTable[0][1].ToString();

End if
Romila C replied to Devil Scorpio on 15-Aug-11 03:21 PM
Hello I use c# code on this program. What's the correct syntax in C# code if I use DBnull?

I appreciate your reply.

P.S

I am reading a text file here.

I use private void button_click

then it starts all the process. My only main concern is some rows has no datas so it gives me that error. But anyway Devil Scorpio gives me an idea but I am not exactly sure how I am gonna apply this to my program.
Irfan Khan replied to Romila C on 15-Aug-11 03:45 PM

Let's take a closer look at working with nullvalues within the .NET Framework.

Null and void

A null value is a value that doesn't refer toany object. It's the default value of reference-type variables(e.g., class, interface, delegate).

These items are called reference types becausethey're objects that store references to the actual data. String,object, and struct are reference type examples. The null keyword isa literal that represents a null reference with the C# .NETenvironment. You may use the null keyword to check or assign thevalue of an object. For example, the following C# code determinesif a string value is null:

string sTest = "Test";
if (sTest == null) {
Console.WriteLine("sTest is Null")
}

This code works without any problems with C#,but there's no VB.NET equivalent of the null keyword. Instead,VB.NET uses the Nothing keyword. The following code demonstratesits use:

Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine("sTest is Null")
End If

Another area of confusion stems from the factthat VB.NET doesn't treat null and Nothing as equals; consequently,a VB.NET programmer may have to check for both values.

The variation in support may cause confusion,but a developer rarely develops in both languages simultaneously.On the other hand, Microsoft provides a uniform method for workingwith null values: The base System.Convert namespace includes theDBNull object.

DBNull

The use of the DBNull class indicates theabsence of a known value. While the Microsoft documentation saysit's typically in a database application, you may also use it withany type of data.

In database applications, a null object is avalid value for a field. This class differentiates between a nullvalue (a null object) and an uninitialized value (the DBNull.Valueinstance). For example, a table can have records with uninitializedfields. By default, these uninitialized fields have the DBNullvalue.

You can utilize the DBNull object by way of itsValue property. This represents a null value, which is never equalto anything. The following C# snippet revises the previous exampleto utilize the DBNull class:

if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");

Notice how this code utilizes the stringclass's equality method. The VB.NET sample may utilize the samecode, like this:

If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine("sTest is Null")
End  If

You may also use the DBNull.Value constant onthe right-hand of an assignment operation, thus storing a nullvalue within a reference type. In addition, the .NET Frameworkprovides the IsDBNull function (in the System.Convert namespace),which is a shorthand approach for determining if a reference typecontains a null value. The following VB.NET uses IsDBNull to checkan object's value:

Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine("oTest is Null")
End If

Even if you check for null values at everyconceivable location within your code, they still may creep intothe application. Fortunately, the try/catch exception blockprovides another line of defense; it allows you to catch anyunhandled exceptions that null values cause. The System namespaceprovides the NullReferenceException for these situations. See howthe following C# code uses NullReferenceException.

try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine("sTest is null.");
}

Devil Scorpio replied to Romila C on 15-Aug-11 04:33 PM
Hi Romila,

In simple

You can check in C# if the value of a datarow is null by following way

Suppose oRow is datarow & you are doing NULL validation for GROUP # column
if (oRow["GROUP #"].ToString() == "")
{
//Your code
}

or you could check for null if (oRow["GROUP #"] == null)
{
//Your code
Jitendra Faye replied to Romila C on 16-Aug-11 12:26 AM
This problem comes, when you try to access the data result but it doesn't contains any row.

so first check that does your resultset containing any row, before accessing data from resultset.

like this -

 if(Ds.Tables[0].Rows.Count>0)
   {
    //read data
   }

Try this and let me know.
aneesa replied to Romila C on 16-Aug-11 01:52 AM
DataTable dt;///load ur data to thi datatable
if(dt.Rows.Count>0)
{
if(dt.Rows[0].ItemArray[0].Tostring()!=System.DBNull.Value.Tostring())
{
/// here write ur code
}
}
dipa ahuja replied to Romila C on 16-Aug-11 04:09 AM
Before reading the data Check the row value:

void bind()
{
  string con = "ConnectionString";
  SqlDataAdapter da = new SqlDataAdapter("select * from people", con);
 
  DataTable dt = new DataTable();
  da.Fill(dt);
  DataList1.DataSource = dt;
  DataList1.DataBind();
  if (dt.Rows.Count == 0)
  {
    //No rows
  }
  else
  {
    //Access Rows
  }
}