Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I created a UserControl:

The cod in the UserControl form is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestiuneProduse;

namespace FereastraPrincipala.Contracte
{
    public partial class InformatiiContracte : UserControl
    {

        private bool ClientiAdaugati = false;

        public InformatiiContracte()
        {
            InitializeComponent();
            
        }

       

        private void InformatiiContracte_Load(object sender, EventArgs e)
        {
            txtNumeClient.Focus();
            InformatiiDespreClient();
        }

        void InformatiiDespreClient()
        {

            txtNumeClient.Items.Clear();
            Program.Connection.CommandText = "select RTRIM(Nume) + ', '+ RTRIM(Prenume) AS NumeComplet, ClientId from DateClientiAmanet ORDER BY Nume";
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);

            txtNumeClient.DataSource = Table;
            txtNumeClient.DisplayMember = "NumeComplet";
            txtNumeClient.ValueMember = "ClientId";
            ClientiAdaugati = true;

        }


        void InformatiiDespreClientDetaliate()
        {

            Program.Connection.CommandText = "select * from DateClientiAmanet where ClientId=" + txtNumeClient.SelectedValue.ToString();
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            foreach (DataRow Row in Table.Rows)
            {
                txtNume.Text = Table.Rows[0]["Nume"].ToString() + ", " + Table.Rows[0]["Prenume"].ToString();
                txtAdresa.Text = "Strada " + Table.Rows[0]["Strada"].ToString() + ", nr. " + Table.Rows[0]["Numarul"].ToString() + ", bl. " + Table.Rows[0]["Bloc"].ToString() + ", sc. " + Table.Rows[0]["Scara"].ToString() + ", et. " + Table.Rows[0]["Etajul"].ToString() + ", ap. " + Table.Rows[0]["Apartament"].ToString();
                txtCNP.Text = Table.Rows[0]["CNP"].ToString();

            }

            this.txtBoxDetaliiClient.Text = "Client: " + txtNumeClient.SelectedValue.ToString();
        }

        private void txtNumeClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ClientiAdaugati)
                InformatiiDespreClientDetaliate();

        }

        
    }
}


In the main form I drag the UserControl to the form,
when i run the program i have an error:

Object reference not set to an instance of an object. 



the cod in the main form is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using FereastraPrincipala.Contracte;

namespace FereastraPrincipala
{
    public partial class frmPrincipala : Form
    {
        
        public frmPrincipala()
        {
            InitializeComponent();
            SeteazaCuloare("Azure");

            informatiiContracte1.Visible = false;
            
         
        }

       

        public void SeteazaCuloare(string Name)
        {
            Color HaloColor = Color.White;
            switch (Name)
            {
                case "Dark":
                    this.BackColor = Color.FromArgb(88,77,69);
                    HaloColor = Color.FromArgb(200,200,200);
                    SetBase(87,61,53,HaloColor);
                    break;
                case "Nature":
                    this.BackColor = Color.FromArgb(78, 127, 52);
                    HaloColor = Color.FromArgb(254, 209, 94);
                    SetBase(73, 118, 46,HaloColor );
                    break;
                case "Dawn":
                    this.BackColor = Color.FromArgb(177, 108, 45);
                    SetBase(172, 99, 39, Color.FromArgb(254, 209, 94));
                    break;
                case "Corn":
                    this.BackColor = Color.FromArgb(230, 193, 106);
                    SetBase(225, 184, 100, Color.FromArgb(191, 219, 255));
                    break;
                case "Chocolate":
                    this.BackColor = Color.FromArgb(87, 54, 34);
                    SetBase(82, 45, 28, Color.FromArgb(232,80,90));
                    break;
                case "Navy":
                    this.BackColor = Color.FromArgb(88,121,169);
                    SetBase(84,112,163, Color.FromArgb(254, 209, 94));
                    break;
                case "Ice":
                    this.BackColor = Color.FromArgb(235, 243, 236);
                    SetBase(228, 234, 230, Color.FromArgb(254, 209, 94));
                    break;
                case "Vanilla":
                    this.BackColor = Color.FromArgb(233, 243, 213);
                    SetBase(228, 234, 207, Color.FromArgb(254, 209, 94));
                    break;
                case "Canela":
                    this.BackColor = Color.FromArgb(235, 226, 197);
                    SetBase(228, 217, 191, Color.FromArgb(254, 209, 94));
                    break;
                case "Cake":
                    this.BackColor = Color.FromArgb(235, 213, 197);
                    SetBase(228, 204, 198, Color.FromArgb(254, 209, 94));
                    break;
                default:
                    
                    this.BackColor = Color.FromArgb(191, 219, 255);
                    SetBase(215, 227, 242, Color.FromArgb(254, 209, 94));
                    break;
            }


        }

        

        private void ribbonButton5_Click(object sender, EventArgs e)
        {

            informatiiContracte1.Visible = true;

        }

             
        
    }


    
}



so how do I remove error?
Posted
Comments
OriginalGriff 13-Mar-11 11:08am    
What line is the exception occurring on?
aciobanita 13-Mar-11 11:17am    
at FereastraPrincipala.Contracte.InformatiiContracte.InformatiiDespreClient() in E:\AddressBook AMANET\TRIAL MAKER\Source\TabStripApp\Contracte\InformatiiContracte.cs:line 36
at FereastraPrincipala.Contracte.InformatiiContracte..ctor() in E:\AddressBook AMANET\TRIAL MAKER\Source\TabStripApp\Contracte\InformatiiContracte.cs:line 21
aciobanita 13-Mar-11 11:20am    
And when i drag the UserControl to the form

a nother error

The variable 'informatiiContracte1' is either undeclared or was never assigned. E:\AddressBook AMANET\TRIAL MAKER\Source\TabStripApp\FereastraPrincipala.Designer.cs

at line:
this.Controls.Add(this.informatiiContracte1);
aciobanita 13-Mar-11 11:21am    
Im new in c#,

1 solution

Line 36 references "Program" and it's Property "Connection" and then "CommandText" which I do not see the definition of anywhere: Is the class class continued in a different file?

It looks like you have lifted a file directly from a Demo of some kind, and not included all the files you need.


"If i remouve the InformatiiDespreClient(); from
private void InformatiiContracte_Load(object sender, EventArgs e)
{
txtNumeClient.Focus();
InformatiiDespreClient();
}
the errors errors disappear"


Yes, because you are no longer referencing the variable that is not initialized.

Start by putting a breakpoint on the first line in "InformatiiDespreClient" and check each reference in turn. Then look for the code that initialises it.

Sorry, but without knowing your code, I can only be vague... :)
 
Share this answer
 
v2
Comments
aciobanita 13-Mar-11 12:07pm    
ies the conections is in a separate file. The program run, but i whant to remove the error?
aciobanita 13-Mar-11 12:09pm    
If i remouve the InformatiiDespreClient(); from

private void InformatiiContracte_Load(object sender, EventArgs e)
{
txtNumeClient.Focus();
InformatiiDespreClient();
}

the errors errors disappear
aciobanita 13-Mar-11 12:10pm    
I have to declare an instance for my UserContro in the main form?
OriginalGriff 13-Mar-11 12:34pm    
Answer updated
aciobanita 13-Mar-11 12:44pm    
Is sims that a eror apeer at line 999 in FereastraPrincipala.Designer.cs

this.Controls.Add(this.informatiiContracte1);

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