Data binding to a listbox

Hello. I am a beginner. I am using Visual studio 2005 (B2) to create a user interface that contains a list box, data navigator and several text boxes that are bound to two relational tables (Investors and ContactDetail). Investor ID is the primary key in the Investors table and has a type of UniqueIdentifier. There is a forgiegnkey relationship between the tables on Investor Id. The list box is bound to the InvestorProfileDataSet which contatins the Investor table and the ContactDetails table. The list box display member is Investors.name and the value member is Investor.id.

The user should be able to add new Investors by clicking the add button on a data navigator and type the contact details on the text boxes. I have two problems implementing it this way.

1) When the users adds a new row and I try to update the database from the data set , I get an error because the InvestorID field is null. How can I create a guid, get the current row index from the binding source, and add it to the new row in the dataset before commiting it to the database? What is the code to do this? Or is there a better way?

1) When the user adds a new row using the binding navigator, the user must click another name in the listbox to update the changes to the dataset. Most user will add the new row and then click the save button to commit the changes to the database. In this case, the row is not yet reflected in the dataset. How can I refresh the dataset before commiting to the database to ensure the new record is included?

Thanks for your help.

Chris

[1571 byte] By [Christopher11] at [2008-3-5]
# 1

Hello. For 1, depending on your database, you can have the database auto-generate your IDs for you (the preferred method). If you don't want to go that route, you can auto-convert null values (or DBNull.Value?) to a GUID in the Format event for the binding. Assuming your InvestorID field is bound to an InvestorIDTextBox and you're using C# (if not, I can provide a VB sample as well) you'd need to put the following code in your Form Load event:


Binding b = this.InvestorIDTextBox.DataBindings["Text"];

b.Format += delegate(object s, ConvertEventArgs a)
{
if (a.Value == DBNull.Value)
{
a.Value = Guid.NewGuid().ToString();
}
};

As for issue 2, if you use the designer to generate the BindingNavigator (and the Save button), you should get code that automatically saves the value to the DataSet prior to writing the results to the database. If you've added your own BindingNavigator with a Save, then make sure you either call EndEdit() on the BindingSource or call Validate() on the Form prior to calling Update on the TableAdapter. EndEdit()/Validate() both force the bound controls to push their values to the data source.

Joe

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Joe,

Thank you for the advice however I don't think this solves my problem. Just to let you know, I am using visual basic. For 1, I do not have a text box that is bound to the investorID field. The user does not need to know about this field. The sole pupose of the investorID field so I can normalize the database. You stated that I should let the database auto generate the Id but I do not know how to do this. I tried by settting the data type at the database level to UniqueIdentifier, however I get an error when I try to update the database because this field is null in the dataset.

Sorry if I missed the point but I am new to this.

Chris.

Christopher11 at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
You can do this either in code or using the data base. It looks like you're using SQL and if so, you need to setup the column to be a uniqueidentifier and set the "Default Value" to "(newid())" - do this by designing the table in SQL Server Enterprise Manager.

If you're not using SQL Server, can't get this to work, or want to use the code route, let me know and I'll provide a VB.NET code sample (which is slightly more complex).

Joe

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...