BindingSource.Current is null on position change.

G

Gerard

Hi
I am trying to do a 1 to many relationship using dynamic textboxes,
binding navigator and a filter. It works on form_load but when i
change to the next row on the bindingNavigator I get.

"Object reference not set to an instance of an object."

Here is the code:

DataRowView view =
(DataRowView)tBLSitesBindingSource.Current;


// crashes current = null;
string companyID = view.Row["companyID"].ToString();
// crashes here after position change in bindingnav


tBLSitesBindingSource.Filter = "companyID = " + companyID;

for (int i = 0; i < rowCount; i++)
{
TextBox tb = new TextBox();
tb.DataBindings.Add(new
System.Windows.Forms.Binding("Text", tBLSitesBindingSource,
"siteName", true));
manyVertPos = manyVertPos + 40;
tb.Location = new System.Drawing.Point(5,
manyVertPos);
tb.Name = "ManyTxtBox" + i;
tb.Size = new System.Drawing.Size(178, 20);

this.Controls.Add(tb);
GRPSites.Controls.Add(tb);

}

Is this an acceptible way of doing a 1 to many relationship or am I
barking up the wrong tree.

Cheers
 
J

jp2msft

Just an idea to get you started:

if (tBLSitesBindingSource.Current != null)
{
DataRowView view = (DataRowView)tBLSitesBindingSource.Current;
// ...
}

I can't tell you anything about the 1 to many relationship part, though. I'm
not that good at databases at this point and time.
 
R

RobinS

Gerard said:
Hi
I am trying to do a 1 to many relationship using dynamic textboxes,
binding navigator and a filter. It works on form_load but when i
change to the next row on the bindingNavigator I get.

"Object reference not set to an instance of an object."

Here is the code:

DataRowView view =
(DataRowView)tBLSitesBindingSource.Current;


// crashes current = null;
string companyID = view.Row["companyID"].ToString();
// crashes here after position change in bindingnav


tBLSitesBindingSource.Filter = "companyID = " + companyID;

for (int i = 0; i < rowCount; i++)
{
TextBox tb = new TextBox();
tb.DataBindings.Add(new
System.Windows.Forms.Binding("Text", tBLSitesBindingSource,
"siteName", true));
manyVertPos = manyVertPos + 40;
tb.Location = new System.Drawing.Point(5,
manyVertPos);
tb.Name = "ManyTxtBox" + i;
tb.Size = new System.Drawing.Size(178, 20);

this.Controls.Add(tb);
GRPSites.Controls.Add(tb);

}

Is this an acceptible way of doing a 1 to many relationship or am I
barking up the wrong tree.

Cheers


Ok, I'll bite. What exactly are you trying to do? Why don't you show the
results of the filtering in a DataGridView? Do you re-create all of the
textboxes every time the user changes records?

RobinS.
GoldMail.com
 
G

Gerard

Just an idea to get you started:

if (tBLSitesBindingSource.Current != null)
{
  DataRowView view = (DataRowView)tBLSitesBindingSource.Current;
  // ...

}

I can't tell you anything about the 1 to many relationship part, though. I'm
not that good at databases at this point and time.



Gerard said:
Hi
I am trying to do a 1 to many relationship using dynamic textboxes,
binding navigator and a filter.  It works on form_load but when i
change to the next row on the bindingNavigator I get.
"Object reference not set to an instance of an object."
Here is the code:
            DataRowView view =
(DataRowView)tBLSitesBindingSource.Current;
 //  crashes current = null;
            string companyID = view.Row["companyID"].ToString();
 //  crashes here after position change in bindingnav
            tBLSitesBindingSource.Filter = "companyID = " + companyID;
            for (int i = 0; i < rowCount; i++)
            {
                TextBox tb = new TextBox();
                tb.DataBindings.Add(new
System.Windows.Forms.Binding("Text", tBLSitesBindingSource,
"siteName", true));
                manyVertPos = manyVertPos + 40;
                tb.Location = new System.Drawing.Point(5,
manyVertPos);
                tb.Name = "ManyTxtBox" + i;
                tb.Size = new System.Drawing.Size(178,20);

                this.Controls.Add(tb);
                GRPSites.Controls.Add(tb);
            }
Is this an acceptible way of doing a 1 to many relationship or am I
barking up the wrong tree.
Cheers- Hide quoted text -

- Show quoted text -


I ve tried something similar the problem was that it always returns
null even though I know there are records there. It doesn't break out
of loop.

cheers
 
G

Gerard

To be honest I just don't like how the data grid looks. Saying that
if there are more then 20 rows, I will redirect to something
different. What I'm trying to do is say for 1 company I may have 3
sites a1 a2 a3. So if on the binding navigator I move poistion to
site a, then only sites a1, a2 and a3 show as text boxes, along with a
delete and save buttons. The filtering was to select only a1,a2, a3
without getting b1 b2 c3 etc

cheers
 
Top