PC Review


Reply
Thread Tools Rate Thread

Combobox DataSource

 
 
rene sřrensen
Guest
Posts: n/a
 
      17th Dec 2004
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);


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
combobox datasource Alejandro González Microsoft C# .NET 1 26th May 2006 05:17 PM
Combobox Datasource =?Utf-8?B?S2luYQ==?= Microsoft Dot NET Framework Forms 0 27th Apr 2005 08:51 PM
Combobox and DataSource Bruce D Microsoft VB .NET 3 12th Apr 2005 10:06 PM
comboBox datasource juli jul Microsoft C# .NET 5 27th Mar 2005 06:13 PM
ComboBox.DataSource .Net v1.1.4322: Problem to assign new datasource Stephan Microsoft Dot NET Framework 3 10th Oct 2003 08:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:42 AM.