Hi Darwin,
I'm glad to know that you have find out the problem. Yes, the current
object which binded to the DataGrid is actually a not a DataRow but a
DataRowView. When casting an object with 'as' statement, it will not throw
exception if the cast is invalid. Instead, the result is a null reference.
So we have to check if the result is null before using 'dr'. (Apologize
that I didn't realize it in my former posts.)
We can also use explicit casting to convert the object like: DataRowView
dr= (DataRowView)bmClient.Current; When the cast is invalid, an exception
will be thrown.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
--------------------
| Content-Class: urn:content-classes:message
| From: "Darwin S." <
[email protected]>
| Sender: "Darwin S." <
[email protected]>
| References: <
[email protected]>
<
[email protected]>
<
[email protected]>
<
[email protected]>
<
[email protected]>
| Subject: RE: Help: BindingManagerBase.Current() now working
| Date: Wed, 12 Nov 2003 21:23:50 -0800
| Lines: 238
| Message-ID: <
[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOpplJHGHgD34BfQzupPYXwhVn5IQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.framework.adonet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.adonet:66182
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| I finally fixed the problem. This line:
|
| dsBrowz2.TClientRow dr = bmClient.Current as
| dsBrowz2.TClientRow;
|
| had to be changed to this:
|
| DataRowView dr= bmClient.Current as DataRowView;
|
| That worked. I was almost certain I had already tried
| that but once I treid it again, it solved the problem.
|
| Darwin
|
| >-----Original Message-----
| >I've already tried it your way many, many times. I
| posted
| >the message in the newsgroup as a last resort after
| >trying all I can and referring to the online
| >documentation. When stepping through the code, this
| line
| >excecutes without difficulty:
| >dsBrowz2.TClientRow dr = bmClient.Current as
| >dsBrowz2.TClientRow;
| >
| >But as soon as I try to reference dr in the IF
| statement,
| >the exception gets thrown. I have stepped through the
| >code examined the variables. I don't know what else to
| >do. I can't tell why dr or dr.clName would be cause an
| >exception.
| >
| >Should I send the solution to your email address
| provided
| >in your post?
| >
| >Thanks,
| >Darwin
| >
| >>-----Original Message-----
| >>Hi Darwin,
| >>
| >>Generally, when troubleshooting this kind of problem.
| We
| >just set a break
| >>point at the start of the prcedure and step through the
| >codes, using the
| >>watch window to monitoring the value of the variables.
| >Would you please try
| >>my way to debug it? Or if it still doesn't solve the
| >problem, is it
| >>convenient for you to send me the whole project? I'll
| >try my best to help
| >>you.
| >>
| >>Kevin Yu
| >>=======
| >>"This posting is provided "AS IS" with no warranties,
| >and confers no
| >>rights."
| >>
| >>--------------------
| >>| Content-Class: urn:content-classes:message
| >>| From: <
[email protected]>
| >>| Sender: <
[email protected]>
| >>| References: <
[email protected]>
| >><
[email protected]>
| >>| Subject: RE: Help: BindingManagerBase.Current() now
| >working
| >>| Date: Tue, 11 Nov 2003 08:53:57 -0800
| >>| Lines: 127
| >>| Message-ID: <
[email protected]>
| >>| MIME-Version: 1.0
| >>| Content-Type: text/plain;
| >>| charset="iso-8859-1"
| >>| Content-Transfer-Encoding: 7bit
| >>| X-Newsreader: Microsoft CDO for Windows 2000
| >>| Thread-Index: AcOodGXPK3zxlmxBR6WvFi9eDzUcqw==
| >>| X-MimeOLE: Produced By Microsoft MimeOLE
| >V5.50.4910.0300
| >>| Newsgroups: microsoft.public.dotnet.framework.adonet
| >>| Path: cpmsftngxa06.phx.gbl
| >>| Xref: cpmsftngxa06.phx.gbl
| >microsoft.public.dotnet.framework.adonet:66019
| >>| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| >>| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
| >>|
| >>| The exception message is:
| >>| [System.NullReferenceException] {"Object
| >reference not
| >>| set to an instance of an object." }
| >>|
| >>| Here is the entire method:
| >>|
| >>| private void btnExcel_Click(object sender,
| >>| System.EventArgs e)
| >>| {
| >>| excelApp = new Excel.ApplicationClass();
| >>| wb = excelApp.Workbooks.Add(Type.Missing);
| >>| BindingManagerBase bmClient =
| >dgClient.BindingContext
| >>| [dgClient.DataSource, "TClient"];
| >>| for(int pos = 0; pos < bmClient.Count; pos++)
| >>| {
| >>| bmClient.Position = pos;
| >>| dsBrowz2.TClientRow dr = bmClient.Current as
| >>| dsBrowz2.TClientRow;
| >>| Excel.Worksheet ws = wb.Worksheets.Add
| >>| (Type.Missing,Type.Missing,Type.Missing,Type.Missing)
| >as
| >>| Excel.Worksheet;
| >>| if (dr.clName.Length > 31)
| >>| ws.Name = dr.clName.Substring(0, 31);
| >>| else
| >>| ws.Name = dr.clName;
| >>| for(int col = 0; col <
| >>| dsBrowz21.TVendor.Columns.Count; col++)
| >>| {
| >>| ws.Rows.Cells[0,col] =
| >>| dsBrowz21.TVendor.Columns[col].Caption;
| >>| }
| >>| BindingManagerBase bmSupp= dgClSupp.BindingContext
| >>| [dgClSupp.DataSource];
| >>| for(int sPos = 0; sPos < bmSupp.Count; sPos++)
| >>| {
| >>| bmSupp.Position = sPos;
| >>| DataRowView sdrv = bmSupp.Current as
| >>| DataRowView;
| >>| for(int sCol = 0; sCol <
| >>| sdrv.Row.Table.Columns.Count; sCol++)
| >>| {
| >>| ws.Rows.Cells[sPos+1, sCol] = sdrv
| >>| [sCol].ToString();
| >>| }
| >>| }
| >>| }
| >>| excelApp.Visible = true;
| >>| }
| >>|
| >>| I have dr instances all throughout my code but they
| >are
| >>| all local variables and do not share scope with this
| >>| instance of dr.
| >>|
| >>| Thanks,
| >>| Darwin
| >>|
| >>| >-----Original Message-----
| >>| >Hi Darwin,
| >>| >
| >>| >I think your code is right, please try to check if
| >the
| >>| dr has been defined
| >>| >in ealier codes. And can you paste the whole
| >exception
| >>| here?
| >>| >
| >>| >If anything is unclear, please feel free to reply to
| >the
| >>| post.
| >>| >
| >>| >Kevin Yu
| >>| >=======
| >>| >"This posting is provided "AS IS" with no
| warranties,
| >>| and confers no
| >>| >rights."
| >>| >
| >>| >--------------------
| >>| >| Content-Class: urn:content-classes:message
| >>| >| From: "Darwin S." <
[email protected]>
| >>| >| Sender: "Darwin S." <
[email protected]>
| >>| >| Subject: Help: BindingManagerBase.Current() now
| >working
| >>| >| Date: Mon, 10 Nov 2003 17:52:52 -0800
| >>| >| Lines: 25
| >>| >| Message-ID: <0cbc01c3a7f6$8496e1e0
|
[email protected]>
| >>| >| MIME-Version: 1.0
| >>| >| Content-Type: text/plain;
| >>| >| charset="iso-8859-1"
| >>| >| Content-Transfer-Encoding: 7bit
| >>| >| X-Newsreader: Microsoft CDO for Windows 2000
| >>| >| X-MimeOLE: Produced By Microsoft MimeOLE
| >>| V5.50.4910.0300
| >>| >| Thread-Index: AcOn9oSWykA/ac4qQPakIWI7R2Q+7w==
| >>| >| Newsgroups:
| microsoft.public.dotnet.framework.adonet
| >>| >| Path: cpmsftngxa06.phx.gbl
| >>| >| Xref: cpmsftngxa06.phx.gbl
| >>| microsoft.public.dotnet.framework.adonet:65970
| >>| >| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| >>| >| X-Tomcat-NG:
| >microsoft.public.dotnet.framework.adonet
| >>| >|
| >>| >| Looking at this code from my application:
| >>| >|
| >>| >| BindingManagerBase bmClient =
| >dgClient.BindingContext
| >>| >| [dgClient.DataSource, "TClient"];
| >>| >| for(int pos = 0; pos < bmClient.Count; pos++)
| >>| >| {
| >>| >| bmClient.Position = pos;
| >>| >| dsBrowz2.TClientRow dr = bmClient.Current as
| >>| >| dsBrowz2.TClientRow;
| >>| >| if (dr.clName.Length > 31)
| >>| >| ws.Name = dr.clName.Substring(0, 31);
| >>| >| else
| >>| >| ws.Name = dr.clName;
| >>| >| }
| >>| >|
| >>| >| You can see that I set a BindingManagerBase object
| >>| with a
| >>| >| datagrid's (dgClient) BindingContext() method.
| >Then
| >>| >| inside the for loop I set a TClientRow object with
| >the
| >>| >| binding manager's Current property. That
| statement
| >>| >| works. But then in the next line, the dr. object
| >is
| >>| >| throws an exception because dr is undefined. What
| >am
| >>| I
| >>| >| doing wrong???
| >>| >|
| >>| >| Thanks,
| >>| >| Darwin
| >>| >|
| >>| >
| >>| >.
| >>| >
| >>|
| >>
| >>.
| >>
| >.
| >
|