Combobox DataSource

R

rene sørensen

I have a odd problem, when i add a DataTable to a combobox with DataSource,
i get an Error MessageBox, that says NullReferenceException. This is a new
language to me, but i think its strange that my try catch dont get this, the
code stops with the exception at this point "**
this.ProfileCB_Profile.DataSource = MyTable **".


CODE

DataRow FoundRow;
DataTable MyTable = this.MyDS.MyDataSet.Tables["Profile"];
DataRow MyRow;

try
{
// Search for alias
FoundRow = MyTable.Rows.Find( this.ProfileTB_Alias.Text );
if ( FoundRow == null )
{
// Create new row
MyRow = this.MyDS.MyDataSet.Tables["Profile"].NewRow();

// Insert data into new row
MyRow["alias"] = this.ProfileTB_Alias.Text.ToString();
MyRow["firstname"] = this.ProfileTB_Firstname.Text.ToString();
MyRow["lastname"] = this.ProfileTB_Lastname.Text.ToString();
//MyRow["program"] =
this.ProfileCB_Programs.SelectedItem.ToString();

// Insert new row into dataset
this.MyDS.MyDataSet.Tables["Profile"].Rows.Add( MyRow );

** this.ProfileCB_Profile.DataSource = MyTable; **
this.ProfileCB_Profile.DisplayMember = "alias";
}


DATASET

// Create a new DataTable.
DataTable myProfileTable = new DataTable("Profile");

// Declare variables for DataColumn and DataRow objects.
DataColumn myProfileColumn;

// Create new ProfileColumn, set DataType, ColumnName and add to DataTable.
myProfileColumn = new DataColumn();
myProfileColumn.DataType = System.Type.GetType("System.Int32");
myProfileColumn.ColumnName = "id";
myProfileColumn.ReadOnly = false;
myProfileColumn.Unique = true;
myProfileColumn.AutoIncrement = true;
myProfileColumn.AutoIncrementSeed = 1;

// Add the Column to the DataColumnCollection.
myProfileTable.Columns.Add(myProfileColumn);

// Create alias column
myProfileColumn = new DataColumn();
myProfileColumn.DataType = System.Type.GetType("System.String");
myProfileColumn.ColumnName = "alias";
myProfileColumn.AutoIncrement = false;
myProfileColumn.Caption = "alias";
myProfileColumn.ReadOnly = false;
myProfileColumn.Unique = true;

// Add the Column to the DataColumnCollection.
myProfileTable.Columns.Add(myProfileColumn);

// Create firstname column.
myProfileColumn = new DataColumn();
myProfileColumn.DataType = System.Type.GetType("System.String");
myProfileColumn.ColumnName = "firstname";
myProfileColumn.AutoIncrement = false;
myProfileColumn.Caption = "firstname";
myProfileColumn.ReadOnly = false;
myProfileColumn.Unique = false;

// Add the Column to the DataColumnCollection.
myProfileTable.Columns.Add(myProfileColumn);

// Create lastname column.
myProfileColumn = new DataColumn();
myProfileColumn.DataType = System.Type.GetType("System.String");
myProfileColumn.ColumnName = "lastname";
myProfileColumn.AutoIncrement = false;
myProfileColumn.Caption = "lastname";
myProfileColumn.ReadOnly = false;
myProfileColumn.Unique = false;

// Add the Column to the DataColumnCollection.
myProfileTable.Columns.Add(myProfileColumn);

// Create program_id column.
myProfileColumn = new DataColumn();
myProfileColumn.DataType = System.Type.GetType("System.Int32");
myProfileColumn.ColumnName = "program_id";
myProfileColumn.AutoIncrement = false;
myProfileColumn.Caption = "program_id";
myProfileColumn.ReadOnly = false;
myProfileColumn.Unique = false;
// Add the column to the table.
myProfileTable.Columns.Add(myProfileColumn);

// Make the ID column the primary key column.
DataColumn[] Profile_PrimaryKeyColumns = new DataColumn[1];
Profile_PrimaryKeyColumns[0] = myProfileTable.Columns["alias"];
myProfileTable.PrimaryKey = Profile_PrimaryKeyColumns;

// Add the new DataTable to the DataSet.
MyDataSet.Tables.Add(myProfileTable);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top