Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi... here is my code

C#
d=25;
Label[] lb1 = new Label[d];
string strd = "select monthly from feehead where fee_head='" + lb.Text + "'";
da = new SqlDataAdapter(strd, con);
DataSet ds11 = new DataSet();
da.Fill(ds11);
for (int l = 1; l < d; l++)
 {
     lb1[l].Text = ds11.Tables[0].Rows[0][0].ToString();
}


database is:
SQL
CREATE TABLE [FEEHEAD] (
    [ACT_CODE] [int] NULL ,
    [FEE_HEAD] [nvarchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [MONTHLY] [bit] NOT NULL ,
    [CL_BASED] [bit] NOT NULL ,
    [AMOUNT] [float] NULL ,
    [SHNAME] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [EMP] [float] NULL ,
    [CCL] [float] NULL ,
    [SPL] [float] NULL ,
    [EXT] [float] NULL ,
    [AccG] [int] NULL ,
    [HType] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [APR] [bit] NOT NULL ,
    [may] [bit] NOT NULL ,
    [JUN] [bit] NOT NULL ,
    [JUL] [bit] NOT NULL ,
    [AUG] [bit] NOT NULL ,
    [SEP] [bit] NOT NULL ,
    [OCT] [bit] NOT NULL ,
    [NOV] [bit] NOT NULL ,
    [DECM] [bit] NOT NULL ,
    [JAN] [bit] NOT NULL ,
    [FEB] [bit] NOT NULL ,
    [MAR] [bit] NOT NULL
) ON [PRIMARY]
GO




and my error is:
Object reference not set to an instance of an object.
Posted
Updated 24-Aug-15 2:21am
v2
Comments
aarif moh shaikh 24-Aug-15 8:17am    
Have you initialize you ConnectionString ??

1 solution

Member 11865955 wrote:

C#
Label[] lb1 = new Label[d];
...
for (int l = 1; l < d; l++)
{
     lb1[l].Text = ds11.Tables[0].Rows[0][0].ToString();
}

You've created an array which can hold a certain number of Label objects. When you create an array, all of the elements are set to the default value for the array type, which in this case is null.

You then attempt to set a property on each element in the array. But you haven't changed the default value of any of the elements, so you're trying to set a property on a null reference.

You need to create a new Label instance within the loop, set its Text property, and then store it in the array:
C#
for (int l = 1; l < d; l++)
{
    Label label = new Label();
    label.Text = ds11.Tables[0].Rows[0][0].ToString();
    lb1[l] = label;
}


You could have easily identified this problem yourself by using the debugger to step through your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900