Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
a create user control with property,i make a instance from usercontrol but running time have error.
this my user control code
C#
public partial class fact_row : UserControl
   {

       public fact_row()
       {
           InitializeComponent();
       }
       public fact_row(string rowid, string account_no1, string account_no2, string operation, string type,
                       string debtor, string creditor)
       {
           this._row_id = rowid;
           this._account_no1 = account_no1;
           this._account_no2 = account_no2;
           this._operation = operation;
           this._type = type;
           this._debtor = debtor;
           this._creditor = creditor;
       }
       public string _row_id
       {
           get { return lbl_row_id.Text; }
           set { lbl_row_id.Text = value; }
       }
       public string _account_no1
       {
           get { return lbl_account_no1.Text; }
           set { lbl_account_no1.Text = value; }
       }
       public string _account_no2
       {
           get { return lbl_account_no2.Text; }
           set { lbl_account_no2.Text = value; }

       }
       public string _operation
       {
           get { return lbl_operation.Text; }
           set { lbl_operation.Text = value; }

       }
       public string _type
       {
           get { return lbl_type.Text; }
           set { lbl_type.Text = value; }
       }
       public string _debtor
       {
           get { return _unsetmask(lbl_Debtor.Text); }
           set { lbl_Debtor.Text = _setmask(value); }
       }
       public string _creditor
       {
           get { return _unsetmask(lbl_Creditor.Text); }
           set { lbl_Creditor.Text = _setmask(value); }
       }
       public Font _Font { get; set; }
       public Color _foreColor
       {
           get { return this.ForeColor; }
           set { this.ForeColor = value; }
       }
       protected string _setmask(string input)
       {
           string result = "";
           char[] arr = input.ToCharArray();
           Array.Reverse(arr);
           string temp = new string(arr);
           int i = 1;
           foreach (char c in temp)
           {
               result += c.ToString();
               if (i % 3 == 0 && i != temp.Length)
               {
                   result += ",";
               }
               i++;
           }
           char[] arr1 = result.ToCharArray();
           Array.Reverse(arr1);

           return new string(arr1);
       }
       protected string _unsetmask(string input)
       {
           return input.Replace(",", string.Empty);
       }
   }


and use this user control in my project that button code is:
C#
private void button1_Click(object sender, EventArgs e)
       {
          fact_row fact_row2=new fact_row("2","","102","1","john","2541000","");
           fact_row2.Height = 33;
           fact_row2.Width = 765;
           fact_row2._row_id = "2";
           fact_row2._debtor = "254622000";
           fact_row2.Show();
           panelEx5.Controls.Add(fact_row2);
           fact_row2.Location=new Point(0,34);
       }


runtime error is:
Object reference not set to an instance of an object.
Posted
Comments
ZurdoDev 3-Mar-14 10:53am    
Which line throws the error?
Vedat Ozan Oner 3-Mar-14 10:56am    
you can use your debugger to find the problem without waiting an answer. put breakpoints with F9, start with F5 and go line by line with F10.
mosi98 3-Mar-14 11:01am    
public string _row_id
{
get { return lbl_row_id.Text; }
set { lbl_row_id.Text = value; }
}

It is an error to 'value'

Look at your constructors:
C#
public fact_row()
{
    InitializeComponent();
}
public fact_row(string rowid, string account_no1, string account_no2, string operation, string type,
                string debtor, string creditor)
{
    this._row_id = rowid;
    this._account_no1 = account_no1;
    this._account_no2 = account_no2;
    this._operation = operation;
    this._type = type;
    this._debtor = debtor;
    this._creditor = creditor;
}


Only one of them calls InitializeComponent - which is the method which creates all the controls your user control displays. If you don;t call it, then your text boxes, labels, and panels never get created, so the references are null.
Add the call your your parameterized constructor, and the problem will go away!
C#
public fact_row(string rowid, string account_no1, string account_no2, string operation, string type,
                string debtor, string creditor)
{
    InitializeComponent();
    this._row_id = rowid;
    this._account_no1 = account_no1;
    this._account_no2 = account_no2;
    this._operation = operation;
    this._type = type;
    this._debtor = debtor;
    this._creditor = creditor;
}



BTW: the standard is that private backing fields start with underline, public properties shoudl
start with an Uppercase letter - and that you should use camelCase instead of underlines in variable, property, method, and control names:
C#
public string RowId
{
    get { return lblRowId.Text; }
    set { lblRowId.Text = value; }
}


[edit]Missed an underline :doh:[/edit]
 
Share this answer
 
v2
You must load the user control in your aspx file.
See the example : http://asp.net-tutorials.com/user-controls/using/[^]
User controls in ASP .NET[^]

and if you want it in code behind then you should use LoadControl("path to your user control").

-KR
 
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