DataGridItem.DataItem availability?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laser Lu

    DataGridItem.DataItem availability?

    Hi, all,
    I was tangled by the DataGridItem.Da taItem property. I tried to invoke
    that property to get and display the contents that was bound to the
    currently selected row. However, an exception surprised me that it said
    "Object reference not set to an instance of an object." How could it happen?
    I have set the data source, and called the DataBind().
    I looked up the MSDN documents, and just find some inessential
    descriptions, and a code example. I didn't try that example by myself, but I
    noticed a difference that, the example uses the single-file code (i.e code
    was written in the .aspx file) while I wrote my code in the code-behind
    mode. So I wonder if this is the reason? But I'm afraid I would be puzzled
    if MS says the DataItem property will not be available through code-behind:)
    Any help, please?


  • Eliyahu Goldin

    #2
    Re: DataGridItem.Da taItem availability?

    There is no difference where the code is.

    Post the relevant fragment from the code and the .aspx file, likely there is
    a problem over there.

    Eliyahu

    "Laser Lu" <laser_lu@hotma il.com> wrote in message
    news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...[color=blue]
    > Hi, all,
    > I was tangled by the DataGridItem.Da taItem property. I tried to invoke
    > that property to get and display the contents that was bound to the
    > currently selected row. However, an exception surprised me that it said
    > "Object reference not set to an instance of an object." How could it
    > happen? I have set the data source, and called the DataBind().
    > I looked up the MSDN documents, and just find some inessential
    > descriptions, and a code example. I didn't try that example by myself, but
    > I noticed a difference that, the example uses the single-file code (i.e
    > code was written in the .aspx file) while I wrote my code in the
    > code-behind mode. So I wonder if this is the reason? But I'm afraid I
    > would be puzzled if MS says the DataItem property will not be available
    > through code-behind:)
    > Any help, please?
    >[/color]


    Comment

    • Laser Lu

      #3
      Re: DataGridItem.Da taItem availability?

      Thanks for your response. Here is the code fragment:

      private void Page_Load(objec t sender, System.EventArg s e)
      {
      // Put user code to initialize the page here
      testAdapter.Fil l(dataSet);
      dataGrid.DataSo urce = dataSet;
      dataGrid.DataBi nd();

      Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
      NullReferenceEx ception will be thrown here!
      }

      The testAdapter executes an SelectCommand as 'select * from Customers' on
      the Northwind database. Actually, dozens of rows was returned from the
      database. However, when I tried to print out the DataItem.ToStri ng() using
      Response.Write( ), a NullReferenceEx ception occured. But it works fine, after
      I had commented that line of code.
      I don't know whether I wrote the code in the correct way, would you please
      point me out? Thanks:)


      "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
      news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > There is no difference where the code is.
      >
      > Post the relevant fragment from the code and the .aspx file, likely there
      > is a problem over there.
      >
      > Eliyahu
      >
      > "Laser Lu" <laser_lu@hotma il.com> wrote in message
      > news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...[color=green]
      >> Hi, all,
      >> I was tangled by the DataGridItem.Da taItem property. I tried to invoke
      >> that property to get and display the contents that was bound to the
      >> currently selected row. However, an exception surprised me that it said
      >> "Object reference not set to an instance of an object." How could it
      >> happen? I have set the data source, and called the DataBind().
      >> I looked up the MSDN documents, and just find some inessential
      >> descriptions, and a code example. I didn't try that example by myself,
      >> but I noticed a difference that, the example uses the single-file code
      >> (i.e code was written in the .aspx file) while I wrote my code in the
      >> code-behind mode. So I wonder if this is the reason? But I'm afraid I
      >> would be puzzled if MS says the DataItem property will not be available
      >> through code-behind:)
      >> Any help, please?
      >>[/color]
      >
      >[/color]


      Comment

      • Eliyahu Goldin

        #4
        Re: DataGridItem.Da taItem availability?

        Do you set DataMember property? You have to set it to the name of a table in
        your dataset.

        In what part of you code do you refer to dataGrid.Items[0].DataItem?
        Typically, it should be in one of the item-related events, like
        ItemDataBound or ItemCreated.

        If it is not enough, post the relevant part of the .aspx file. Don't sent
        attachments.

        Eliyahu

        "Laser Lu" <laser_lu@hotma il.com> wrote in message
        news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...[color=blue]
        > And the .aspx file and .cs file is provided in the attachment for your
        > convenience, if necessary. :)
        >
        > "Laser Lu" <laser_lu@hotma il.com> wrote in message
        > news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..[color=green]
        >> Thanks for your response. Here is the code fragment:
        >>
        >> private void Page_Load(objec t sender, System.EventArg s e)
        >> {
        >> // Put user code to initialize the page here
        >> testAdapter.Fil l(dataSet);
        >> dataGrid.DataSo urce = dataSet;
        >> dataGrid.DataBi nd();
        >>
        >> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
        >> NullReferenceEx ception will be thrown here!
        >> }
        >>
        >> The testAdapter executes an SelectCommand as 'select * from Customers' on
        >> the Northwind database. Actually, dozens of rows was returned from the
        >> database. However, when I tried to print out the DataItem.ToStri ng()
        >> using
        >> Response.Write( ), a NullReferenceEx ception occured. But it works fine,
        >> after
        >> I had commented that line of code.
        >> I don't know whether I wrote the code in the correct way, would you
        >> please
        >> point me out? Thanks:)
        >>
        >>
        >> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
        >> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
        >>> There is no difference where the code is.
        >>>
        >>> Post the relevant fragment from the code and the .aspx file, likely
        >>> there
        >>> is a problem over there.
        >>>
        >>> Eliyahu
        >>>
        >>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
        >>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
        >>>> Hi, all,
        >>>> I was tangled by the DataGridItem.Da taItem property. I tried to
        >>>> invoke
        >>>> that property to get and display the contents that was bound to the
        >>>> currently selected row. However, an exception surprised me that it said
        >>>> "Object reference not set to an instance of an object." How could it
        >>>> happen? I have set the data source, and called the DataBind().
        >>>> I looked up the MSDN documents, and just find some inessential
        >>>> descriptions, and a code example. I didn't try that example by myself,
        >>>> but I noticed a difference that, the example uses the single-file code
        >>>> (i.e code was written in the .aspx file) while I wrote my code in the
        >>>> code-behind mode. So I wonder if this is the reason? But I'm afraid I
        >>>> would be puzzled if MS says the DataItem property will not be available
        >>>> through code-behind:)
        >>>> Any help, please?
        >>>>
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >
        >[/color]


        Comment

        • Laser Lu

          #5
          Re: DataGridItem.Da taItem availability?

          I suppose, there is no need to explicity set the DataMember, if there is
          only one DataTable exists in the DataSet. The control will automaitcally
          find the first DataTable to bind. The previous code I posted withou
          specifying DataMember worked fine in my computer:)

          Additionally, I've just followed what you have mentioned, accessing the
          DataItem property in item-related events, both ItemCreated and
          ItemDataBound. But they still doesn't work, keeping throwing
          NullReferenceEx ceptions in both event handlers. :(

          What a pity! I don't know what to do. It really frustrated me! Help, please!

          BTW: the whole program for Text.aspx was posted below, and just for your
          information:

          /// Test.aspx
          <%@ Page language="c#" Codebehind="Tes t.aspx.cs" AutoEventWireup ="false"
          Inherits="CEIS. Test" %>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
          <HTML>
          <HEAD>
          <title>Test</title>
          <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
          <meta content="C#" name="CODE_LANG UAGE">
          <meta content="JavaSc ript" name="vs_defaul tClientScript">
          <meta content="http://schemas.microso ft.com/intellisense/ie5"
          name="vs_target Schema">
          </HEAD>
          <body>
          <form id="Form1" method="post" runat="server">
          <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
          </body>
          </HTML>

          /// Test.aspx.cs
          using System;
          using System.Collecti ons;
          using System.Componen tModel;
          using System.Data;
          using System.Drawing;
          using System.Web;
          using System.Web.Sess ionState;
          using System.Web.UI;
          using System.Web.UI.W ebControls;
          using System.Web.UI.H tmlControls;

          namespace CEIS
          {
          /// <summary>
          /// Summary description for Test.
          /// </summary>
          public class Test : System.Web.UI.P age
          {
          protected System.Data.Sql Client.SqlConne ction testConnection;
          protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
          protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
          protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
          protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
          protected System.Data.Sql Client.SqlDataA dapter testAdapter;
          protected System.Data.Dat aSet dataSet;
          protected System.Web.UI.W ebControls.Data Grid dataGrid;

          private void Page_Load(objec t sender, System.EventArg s e)
          {
          // Put user code to initialize the page here
          testAdapter.Fil l(dataSet);
          dataGrid.DataMe mber = "Table";
          dataGrid.DataSo urce = dataSet;
          dataGrid.DataBi nd();

          // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
          }

          #region Web Form Designer generated code
          override protected void OnInit(EventArg s e)
          {
          //
          // CODEGEN: This call is required by the ASP.NET Web Form Designer.
          //
          InitializeCompo nent();
          base.OnInit(e);
          }

          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeCompo nent()
          {
          System.Configur ation.AppSettin gsReader configurationAp pSettings = new
          System.Configur ation.AppSettin gsReader();
          this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
          this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
          this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
          this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
          this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
          this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
          this.dataSet = new System.Data.Dat aSet();
          ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
          this.dataGrid.I temCreated += new
          System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
          this.dataGrid.I temDataBound += new
          System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
          //
          // testConnection
          //
          this.testConnec tion.Connection String =
          ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
          typeof(string)) ));
          //
          // sqlSelectComman d1
          //
          this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM Customers";
          this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
          //
          // testAdapter
          //
          this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
          this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
          this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
          this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
          //
          // dataSet
          //
          this.dataSet.Da taSetName = "testDataSe t";
          this.dataSet.Lo cale = new System.Globaliz ation.CultureIn fo("en-US");
          this.Load += new System.EventHan dler(this.Page_ Load);
          ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();

          }
          #endregion

          private void dataGrid_ItemCr eated(object sender,
          System.Web.UI.W ebControls.Data GridItemEventAr gs e)
          {
          // Response.Write( e.Item.DataItem .ToString());
          }

          private void dataGrid_ItemDa taBound(object sender,
          System.Web.UI.W ebControls.Data GridItemEventAr gs e)
          {
          // Response.Write( e.Item.DataItem .ToString());
          }
          }
          }



          "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
          news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Do you set DataMember property? You have to set it to the name of a table
          > in your dataset.
          >
          > In what part of you code do you refer to dataGrid.Items[0].DataItem?
          > Typically, it should be in one of the item-related events, like
          > ItemDataBound or ItemCreated.
          >
          > If it is not enough, post the relevant part of the .aspx file. Don't sent
          > attachments.
          >
          > Eliyahu
          >
          > "Laser Lu" <laser_lu@hotma il.com> wrote in message
          > news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...[color=green]
          >> And the .aspx file and .cs file is provided in the attachment for your
          >> convenience, if necessary. :)
          >>
          >> "Laser Lu" <laser_lu@hotma il.com> wrote in message
          >> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..[color=darkred]
          >>> Thanks for your response. Here is the code fragment:
          >>>
          >>> private void Page_Load(objec t sender, System.EventArg s e)
          >>> {
          >>> // Put user code to initialize the page here
          >>> testAdapter.Fil l(dataSet);
          >>> dataGrid.DataSo urce = dataSet;
          >>> dataGrid.DataBi nd();
          >>>
          >>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
          >>> NullReferenceEx ception will be thrown here!
          >>> }
          >>>
          >>> The testAdapter executes an SelectCommand as 'select * from Customers'
          >>> on
          >>> the Northwind database. Actually, dozens of rows was returned from the
          >>> database. However, when I tried to print out the DataItem.ToStri ng()
          >>> using
          >>> Response.Write( ), a NullReferenceEx ception occured. But it works fine,
          >>> after
          >>> I had commented that line of code.
          >>> I don't know whether I wrote the code in the correct way, would you
          >>> please
          >>> point me out? Thanks:)
          >>>
          >>>
          >>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
          >>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
          >>>> There is no difference where the code is.
          >>>>
          >>>> Post the relevant fragment from the code and the .aspx file, likely
          >>>> there
          >>>> is a problem over there.
          >>>>
          >>>> Eliyahu
          >>>>
          >>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
          >>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
          >>>>> Hi, all,
          >>>>> I was tangled by the DataGridItem.Da taItem property. I tried to
          >>>>> invoke
          >>>>> that property to get and display the contents that was bound to the
          >>>>> currently selected row. However, an exception surprised me that it
          >>>>> said
          >>>>> "Object reference not set to an instance of an object." How could it
          >>>>> happen? I have set the data source, and called the DataBind().
          >>>>> I looked up the MSDN documents, and just find some inessential
          >>>>> descriptions, and a code example. I didn't try that example by myself,
          >>>>> but I noticed a difference that, the example uses the single-file code
          >>>>> (i.e code was written in the .aspx file) while I wrote my code in the
          >>>>> code-behind mode. So I wonder if this is the reason? But I'm afraid I
          >>>>> would be puzzled if MS says the DataItem property will not be
          >>>>> available
          >>>>> through code-behind:)
          >>>>> Any help, please?
          >>>>>
          >>>>
          >>>>
          >>>
          >>>[/color]
          >>
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Eliyahu Goldin

            #6
            Re: DataGridItem.Da taItem availability?

            From the MSDN Library on DataGrid.DataMe mber property:

            ---
            If the DataSet or DataViewManager contains only one DataTable, you should
            set the DataMember to the TableName of that DataTable.
            ---

            Just try this. It doesn't hurt.

            Eliyahu

            "Laser Lu" <laser_lu@hotma il.com> wrote in message
            news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..[color=blue]
            >I suppose, there is no need to explicity set the DataMember, if there is
            >only one DataTable exists in the DataSet. The control will automaitcally
            >find the first DataTable to bind. The previous code I posted withou
            >specifying DataMember worked fine in my computer:)
            >
            > Additionally, I've just followed what you have mentioned, accessing the
            > DataItem property in item-related events, both ItemCreated and
            > ItemDataBound. But they still doesn't work, keeping throwing
            > NullReferenceEx ceptions in both event handlers. :(
            >
            > What a pity! I don't know what to do. It really frustrated me! Help,
            > please!
            >
            > BTW: the whole program for Text.aspx was posted below, and just for your
            > information:
            >
            > /// Test.aspx
            > <%@ Page language="c#" Codebehind="Tes t.aspx.cs" AutoEventWireup ="false"
            > Inherits="CEIS. Test" %>
            > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
            > <HTML>
            > <HEAD>
            > <title>Test</title>
            > <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
            > <meta content="C#" name="CODE_LANG UAGE">
            > <meta content="JavaSc ript" name="vs_defaul tClientScript">
            > <meta content="http://schemas.microso ft.com/intellisense/ie5"
            > name="vs_target Schema">
            > </HEAD>
            > <body>
            > <form id="Form1" method="post" runat="server">
            > <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
            > </body>
            > </HTML>
            >
            > /// Test.aspx.cs
            > using System;
            > using System.Collecti ons;
            > using System.Componen tModel;
            > using System.Data;
            > using System.Drawing;
            > using System.Web;
            > using System.Web.Sess ionState;
            > using System.Web.UI;
            > using System.Web.UI.W ebControls;
            > using System.Web.UI.H tmlControls;
            >
            > namespace CEIS
            > {
            > /// <summary>
            > /// Summary description for Test.
            > /// </summary>
            > public class Test : System.Web.UI.P age
            > {
            > protected System.Data.Sql Client.SqlConne ction testConnection;
            > protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
            > protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
            > protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
            > protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
            > protected System.Data.Sql Client.SqlDataA dapter testAdapter;
            > protected System.Data.Dat aSet dataSet;
            > protected System.Web.UI.W ebControls.Data Grid dataGrid;
            >
            > private void Page_Load(objec t sender, System.EventArg s e)
            > {
            > // Put user code to initialize the page here
            > testAdapter.Fil l(dataSet);
            > dataGrid.DataMe mber = "Table";
            > dataGrid.DataSo urce = dataSet;
            > dataGrid.DataBi nd();
            >
            > // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
            > }
            >
            > #region Web Form Designer generated code
            > override protected void OnInit(EventArg s e)
            > {
            > //
            > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            > //
            > InitializeCompo nent();
            > base.OnInit(e);
            > }
            >
            > /// <summary>
            > /// Required method for Designer support - do not modify
            > /// the contents of this method with the code editor.
            > /// </summary>
            > private void InitializeCompo nent()
            > {
            > System.Configur ation.AppSettin gsReader configurationAp pSettings = new
            > System.Configur ation.AppSettin gsReader();
            > this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
            > this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
            > this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
            > this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
            > this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
            > this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
            > this.dataSet = new System.Data.Dat aSet();
            > ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
            > this.dataGrid.I temCreated += new
            > System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
            > this.dataGrid.I temDataBound += new
            > System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
            > //
            > // testConnection
            > //
            > this.testConnec tion.Connection String =
            > ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
            > typeof(string)) ));
            > //
            > // sqlSelectComman d1
            > //
            > this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
            > Customers";
            > this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
            > //
            > // testAdapter
            > //
            > this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
            > this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
            > this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
            > this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
            > //
            > // dataSet
            > //
            > this.dataSet.Da taSetName = "testDataSe t";
            > this.dataSet.Lo cale = new System.Globaliz ation.CultureIn fo("en-US");
            > this.Load += new System.EventHan dler(this.Page_ Load);
            > ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
            >
            > }
            > #endregion
            >
            > private void dataGrid_ItemCr eated(object sender,
            > System.Web.UI.W ebControls.Data GridItemEventAr gs e)
            > {
            > // Response.Write( e.Item.DataItem .ToString());
            > }
            >
            > private void dataGrid_ItemDa taBound(object sender,
            > System.Web.UI.W ebControls.Data GridItemEventAr gs e)
            > {
            > // Response.Write( e.Item.DataItem .ToString());
            > }
            > }
            > }
            >
            >
            >
            > "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
            > news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...[color=green]
            >> Do you set DataMember property? You have to set it to the name of a table
            >> in your dataset.
            >>
            >> In what part of you code do you refer to dataGrid.Items[0].DataItem?
            >> Typically, it should be in one of the item-related events, like
            >> ItemDataBound or ItemCreated.
            >>
            >> If it is not enough, post the relevant part of the .aspx file. Don't sent
            >> attachments.
            >>
            >> Eliyahu
            >>
            >> "Laser Lu" <laser_lu@hotma il.com> wrote in message
            >> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...[color=darkred]
            >>> And the .aspx file and .cs file is provided in the attachment for your
            >>> convenience, if necessary. :)
            >>>
            >>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
            >>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
            >>>> Thanks for your response. Here is the code fragment:
            >>>>
            >>>> private void Page_Load(objec t sender, System.EventArg s e)
            >>>> {
            >>>> // Put user code to initialize the page here
            >>>> testAdapter.Fil l(dataSet);
            >>>> dataGrid.DataSo urce = dataSet;
            >>>> dataGrid.DataBi nd();
            >>>>
            >>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
            >>>> NullReferenceEx ception will be thrown here!
            >>>> }
            >>>>
            >>>> The testAdapter executes an SelectCommand as 'select * from Customers'
            >>>> on
            >>>> the Northwind database. Actually, dozens of rows was returned from the
            >>>> database. However, when I tried to print out the DataItem.ToStri ng()
            >>>> using
            >>>> Response.Write( ), a NullReferenceEx ception occured. But it works fine,
            >>>> after
            >>>> I had commented that line of code.
            >>>> I don't know whether I wrote the code in the correct way, would you
            >>>> please
            >>>> point me out? Thanks:)
            >>>>
            >>>>
            >>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
            >>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
            >>>>> There is no difference where the code is.
            >>>>>
            >>>>> Post the relevant fragment from the code and the .aspx file, likely
            >>>>> there
            >>>>> is a problem over there.
            >>>>>
            >>>>> Eliyahu
            >>>>>
            >>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
            >>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
            >>>>>> Hi, all,
            >>>>>> I was tangled by the DataGridItem.Da taItem property. I tried to
            >>>>>> invoke
            >>>>>> that property to get and display the contents that was bound to the
            >>>>>> currently selected row. However, an exception surprised me that it
            >>>>>> said
            >>>>>> "Object reference not set to an instance of an object." How could it
            >>>>>> happen? I have set the data source, and called the DataBind().
            >>>>>> I looked up the MSDN documents, and just find some inessential
            >>>>>> descriptions, and a code example. I didn't try that example by
            >>>>>> myself,
            >>>>>> but I noticed a difference that, the example uses the single-file
            >>>>>> code
            >>>>>> (i.e code was written in the .aspx file) while I wrote my code in the
            >>>>>> code-behind mode. So I wonder if this is the reason? But I'm afraid I
            >>>>>> would be puzzled if MS says the DataItem property will not be
            >>>>>> available
            >>>>>> through code-behind:)
            >>>>>> Any help, please?
            >>>>>>
            >>>>>
            >>>>>
            >>>>
            >>>>
            >>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Laser Lu

              #7
              Re: DataGridItem.Da taItem availability?

              I have done that in my last code post. :) Maybe, that is not the key point.

              "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
              news:ObUpZGfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=blue]
              > From the MSDN Library on DataGrid.DataMe mber property:
              >
              > ---
              > If the DataSet or DataViewManager contains only one DataTable, you should
              > set the DataMember to the TableName of that DataTable.
              > ---
              >
              > Just try this. It doesn't hurt.
              >
              > Eliyahu
              >
              > "Laser Lu" <laser_lu@hotma il.com> wrote in message
              > news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..[color=green]
              >>I suppose, there is no need to explicity set the DataMember, if there is
              >>only one DataTable exists in the DataSet. The control will automaitcally
              >>find the first DataTable to bind. The previous code I posted withou
              >>specifying DataMember worked fine in my computer:)
              >>
              >> Additionally, I've just followed what you have mentioned, accessing the
              >> DataItem property in item-related events, both ItemCreated and
              >> ItemDataBound. But they still doesn't work, keeping throwing
              >> NullReferenceEx ceptions in both event handlers. :(
              >>
              >> What a pity! I don't know what to do. It really frustrated me! Help,
              >> please!
              >>
              >> BTW: the whole program for Text.aspx was posted below, and just for your
              >> information:
              >>
              >> /// Test.aspx
              >> <%@ Page language="c#" Codebehind="Tes t.aspx.cs" AutoEventWireup ="false"
              >> Inherits="CEIS. Test" %>
              >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
              >> <HTML>
              >> <HEAD>
              >> <title>Test</title>
              >> <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
              >> <meta content="C#" name="CODE_LANG UAGE">
              >> <meta content="JavaSc ript" name="vs_defaul tClientScript">
              >> <meta content="http://schemas.microso ft.com/intellisense/ie5"
              >> name="vs_target Schema">
              >> </HEAD>
              >> <body>
              >> <form id="Form1" method="post" runat="server">
              >> <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
              >> </body>
              >> </HTML>
              >>
              >> /// Test.aspx.cs
              >> using System;
              >> using System.Collecti ons;
              >> using System.Componen tModel;
              >> using System.Data;
              >> using System.Drawing;
              >> using System.Web;
              >> using System.Web.Sess ionState;
              >> using System.Web.UI;
              >> using System.Web.UI.W ebControls;
              >> using System.Web.UI.H tmlControls;
              >>
              >> namespace CEIS
              >> {
              >> /// <summary>
              >> /// Summary description for Test.
              >> /// </summary>
              >> public class Test : System.Web.UI.P age
              >> {
              >> protected System.Data.Sql Client.SqlConne ction testConnection;
              >> protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
              >> protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
              >> protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
              >> protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
              >> protected System.Data.Sql Client.SqlDataA dapter testAdapter;
              >> protected System.Data.Dat aSet dataSet;
              >> protected System.Web.UI.W ebControls.Data Grid dataGrid;
              >>
              >> private void Page_Load(objec t sender, System.EventArg s e)
              >> {
              >> // Put user code to initialize the page here
              >> testAdapter.Fil l(dataSet);
              >> dataGrid.DataMe mber = "Table";
              >> dataGrid.DataSo urce = dataSet;
              >> dataGrid.DataBi nd();
              >>
              >> // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
              >> }
              >>
              >> #region Web Form Designer generated code
              >> override protected void OnInit(EventArg s e)
              >> {
              >> //
              >> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
              >> //
              >> InitializeCompo nent();
              >> base.OnInit(e);
              >> }
              >>
              >> /// <summary>
              >> /// Required method for Designer support - do not modify
              >> /// the contents of this method with the code editor.
              >> /// </summary>
              >> private void InitializeCompo nent()
              >> {
              >> System.Configur ation.AppSettin gsReader configurationAp pSettings = new
              >> System.Configur ation.AppSettin gsReader();
              >> this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
              >> this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
              >> this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
              >> this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
              >> this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
              >> this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
              >> this.dataSet = new System.Data.Dat aSet();
              >> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
              >> this.dataGrid.I temCreated += new
              >> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
              >> this.dataGrid.I temDataBound += new
              >> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
              >> //
              >> // testConnection
              >> //
              >> this.testConnec tion.Connection String =
              >> ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
              >> typeof(string)) ));
              >> //
              >> // sqlSelectComman d1
              >> //
              >> this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
              >> Customers";
              >> this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
              >> //
              >> // testAdapter
              >> //
              >> this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
              >> this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
              >> this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
              >> this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
              >> //
              >> // dataSet
              >> //
              >> this.dataSet.Da taSetName = "testDataSe t";
              >> this.dataSet.Lo cale = new System.Globaliz ation.CultureIn fo("en-US");
              >> this.Load += new System.EventHan dler(this.Page_ Load);
              >> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
              >>
              >> }
              >> #endregion
              >>
              >> private void dataGrid_ItemCr eated(object sender,
              >> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
              >> {
              >> // Response.Write( e.Item.DataItem .ToString());
              >> }
              >>
              >> private void dataGrid_ItemDa taBound(object sender,
              >> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
              >> {
              >> // Response.Write( e.Item.DataItem .ToString());
              >> }
              >> }
              >> }
              >>
              >>
              >>
              >> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
              >> news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...[color=darkred]
              >>> Do you set DataMember property? You have to set it to the name of a
              >>> table in your dataset.
              >>>
              >>> In what part of you code do you refer to dataGrid.Items[0].DataItem?
              >>> Typically, it should be in one of the item-related events, like
              >>> ItemDataBound or ItemCreated.
              >>>
              >>> If it is not enough, post the relevant part of the .aspx file. Don't
              >>> sent attachments.
              >>>
              >>> Eliyahu
              >>>
              >>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
              >>> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...
              >>>> And the .aspx file and .cs file is provided in the attachment for your
              >>>> convenience, if necessary. :)
              >>>>
              >>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
              >>>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
              >>>>> Thanks for your response. Here is the code fragment:
              >>>>>
              >>>>> private void Page_Load(objec t sender, System.EventArg s e)
              >>>>> {
              >>>>> // Put user code to initialize the page here
              >>>>> testAdapter.Fil l(dataSet);
              >>>>> dataGrid.DataSo urce = dataSet;
              >>>>> dataGrid.DataBi nd();
              >>>>>
              >>>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
              >>>>> NullReferenceEx ception will be thrown here!
              >>>>> }
              >>>>>
              >>>>> The testAdapter executes an SelectCommand as 'select * from Customers'
              >>>>> on
              >>>>> the Northwind database. Actually, dozens of rows was returned from the
              >>>>> database. However, when I tried to print out the DataItem.ToStri ng()
              >>>>> using
              >>>>> Response.Write( ), a NullReferenceEx ception occured. But it works fine,
              >>>>> after
              >>>>> I had commented that line of code.
              >>>>> I don't know whether I wrote the code in the correct way, would you
              >>>>> please
              >>>>> point me out? Thanks:)
              >>>>>
              >>>>>
              >>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
              >>>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
              >>>>>> There is no difference where the code is.
              >>>>>>
              >>>>>> Post the relevant fragment from the code and the .aspx file, likely
              >>>>>> there
              >>>>>> is a problem over there.
              >>>>>>
              >>>>>> Eliyahu
              >>>>>>
              >>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
              >>>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
              >>>>>>> Hi, all,
              >>>>>>> I was tangled by the DataGridItem.Da taItem property. I tried to
              >>>>>>> invoke
              >>>>>>> that property to get and display the contents that was bound to the
              >>>>>>> currently selected row. However, an exception surprised me that it
              >>>>>>> said
              >>>>>>> "Object reference not set to an instance of an object." How could it
              >>>>>>> happen? I have set the data source, and called the DataBind().
              >>>>>>> I looked up the MSDN documents, and just find some inessential
              >>>>>>> descriptions, and a code example. I didn't try that example by
              >>>>>>> myself,
              >>>>>>> but I noticed a difference that, the example uses the single-file
              >>>>>>> code
              >>>>>>> (i.e code was written in the .aspx file) while I wrote my code in
              >>>>>>> the
              >>>>>>> code-behind mode. So I wonder if this is the reason? But I'm afraid
              >>>>>>> I
              >>>>>>> would be puzzled if MS says the DataItem property will not be
              >>>>>>> available
              >>>>>>> through code-behind:)
              >>>>>>> Any help, please?
              >>>>>>>
              >>>>>>
              >>>>>>
              >>>>>
              >>>>>
              >>>>
              >>>>
              >>>>
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Laser Lu

                #8
                Re: DataGridItem.Da taItem availability?

                Anybody else knows the reason? please help! Thanks a lot!!

                "Laser Lu" <laser_lu@hotma il.com> wrote in message
                news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..[color=blue]
                > Thanks for your response. Here is the code fragment:
                >
                > private void Page_Load(objec t sender, System.EventArg s e)
                > {
                > // Put user code to initialize the page here
                > testAdapter.Fil l(dataSet);
                > dataGrid.DataSo urce = dataSet;
                > dataGrid.DataBi nd();
                >
                > Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                > NullReferenceEx ception will be thrown here!
                > }
                >
                > The testAdapter executes an SelectCommand as 'select * from Customers' on
                > the Northwind database. Actually, dozens of rows was returned from the
                > database. However, when I tried to print out the DataItem.ToStri ng() using
                > Response.Write( ), a NullReferenceEx ception occured. But it works fine,
                > after I had commented that line of code.
                > I don't know whether I wrote the code in the correct way, would you please
                > point me out? Thanks:)
                >
                >
                > "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                > news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..[color=green]
                >> There is no difference where the code is.
                >>
                >> Post the relevant fragment from the code and the .aspx file, likely there
                >> is a problem over there.
                >>
                >> Eliyahu
                >>
                >> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                >> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...[color=darkred]
                >>> Hi, all,
                >>> I was tangled by the DataGridItem.Da taItem property. I tried to
                >>> invoke that property to get and display the contents that was bound to
                >>> the currently selected row. However, an exception surprised me that it
                >>> said "Object reference not set to an instance of an object." How could
                >>> it happen? I have set the data source, and called the DataBind().
                >>> I looked up the MSDN documents, and just find some inessential
                >>> descriptions, and a code example. I didn't try that example by myself,
                >>> but I noticed a difference that, the example uses the single-file code
                >>> (i.e code was written in the .aspx file) while I wrote my code in the
                >>> code-behind mode. So I wonder if this is the reason? But I'm afraid I
                >>> would be puzzled if MS says the DataItem property will not be available
                >>> through code-behind:)
                >>> Any help, please?
                >>>[/color]
                >>
                >>[/color]
                >
                >[/color]


                Comment

                • Eliyahu Goldin

                  #9
                  Re: DataGridItem.Da taItem availability?

                  Sorry, missed it.

                  I think the table name should be "Table1" rather than "Table". Anyway, why
                  don't you set a breakpoint on the line following the Fill call and check
                  what is in the dataset?

                  Eliyahu

                  "Laser Lu" <laser_lu@hotma il.com> wrote in message
                  news:OIfczPfQGH A.1160@TK2MSFTN GP09.phx.gbl...[color=blue]
                  >I have done that in my last code post. :) Maybe, that is not the key point.
                  >
                  > "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                  > news:ObUpZGfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=green]
                  >> From the MSDN Library on DataGrid.DataMe mber property:
                  >>
                  >> ---
                  >> If the DataSet or DataViewManager contains only one DataTable, you should
                  >> set the DataMember to the TableName of that DataTable.
                  >> ---
                  >>
                  >> Just try this. It doesn't hurt.
                  >>
                  >> Eliyahu
                  >>
                  >> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                  >> news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
                  >>>I suppose, there is no need to explicity set the DataMember, if there is
                  >>>only one DataTable exists in the DataSet. The control will automaitcally
                  >>>find the first DataTable to bind. The previous code I posted withou
                  >>>specifying DataMember worked fine in my computer:)
                  >>>
                  >>> Additionally, I've just followed what you have mentioned, accessing the
                  >>> DataItem property in item-related events, both ItemCreated and
                  >>> ItemDataBound. But they still doesn't work, keeping throwing
                  >>> NullReferenceEx ceptions in both event handlers. :(
                  >>>
                  >>> What a pity! I don't know what to do. It really frustrated me! Help,
                  >>> please!
                  >>>
                  >>> BTW: the whole program for Text.aspx was posted below, and just for your
                  >>> information:
                  >>>
                  >>> /// Test.aspx
                  >>> <%@ Page language="c#" Codebehind="Tes t.aspx.cs" AutoEventWireup ="false"
                  >>> Inherits="CEIS. Test" %>
                  >>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
                  >>> <HTML>
                  >>> <HEAD>
                  >>> <title>Test</title>
                  >>> <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
                  >>> <meta content="C#" name="CODE_LANG UAGE">
                  >>> <meta content="JavaSc ript" name="vs_defaul tClientScript">
                  >>> <meta content="http://schemas.microso ft.com/intellisense/ie5"
                  >>> name="vs_target Schema">
                  >>> </HEAD>
                  >>> <body>
                  >>> <form id="Form1" method="post" runat="server">
                  >>> <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
                  >>> </body>
                  >>> </HTML>
                  >>>
                  >>> /// Test.aspx.cs
                  >>> using System;
                  >>> using System.Collecti ons;
                  >>> using System.Componen tModel;
                  >>> using System.Data;
                  >>> using System.Drawing;
                  >>> using System.Web;
                  >>> using System.Web.Sess ionState;
                  >>> using System.Web.UI;
                  >>> using System.Web.UI.W ebControls;
                  >>> using System.Web.UI.H tmlControls;
                  >>>
                  >>> namespace CEIS
                  >>> {
                  >>> /// <summary>
                  >>> /// Summary description for Test.
                  >>> /// </summary>
                  >>> public class Test : System.Web.UI.P age
                  >>> {
                  >>> protected System.Data.Sql Client.SqlConne ction testConnection;
                  >>> protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
                  >>> protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
                  >>> protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
                  >>> protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
                  >>> protected System.Data.Sql Client.SqlDataA dapter testAdapter;
                  >>> protected System.Data.Dat aSet dataSet;
                  >>> protected System.Web.UI.W ebControls.Data Grid dataGrid;
                  >>>
                  >>> private void Page_Load(objec t sender, System.EventArg s e)
                  >>> {
                  >>> // Put user code to initialize the page here
                  >>> testAdapter.Fil l(dataSet);
                  >>> dataGrid.DataMe mber = "Table";
                  >>> dataGrid.DataSo urce = dataSet;
                  >>> dataGrid.DataBi nd();
                  >>>
                  >>> // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
                  >>> }
                  >>>
                  >>> #region Web Form Designer generated code
                  >>> override protected void OnInit(EventArg s e)
                  >>> {
                  >>> //
                  >>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  >>> //
                  >>> InitializeCompo nent();
                  >>> base.OnInit(e);
                  >>> }
                  >>>
                  >>> /// <summary>
                  >>> /// Required method for Designer support - do not modify
                  >>> /// the contents of this method with the code editor.
                  >>> /// </summary>
                  >>> private void InitializeCompo nent()
                  >>> {
                  >>> System.Configur ation.AppSettin gsReader configurationAp pSettings = new
                  >>> System.Configur ation.AppSettin gsReader();
                  >>> this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
                  >>> this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
                  >>> this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
                  >>> this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
                  >>> this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
                  >>> this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
                  >>> this.dataSet = new System.Data.Dat aSet();
                  >>>
                  >>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
                  >>> this.dataGrid.I temCreated += new
                  >>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
                  >>> this.dataGrid.I temDataBound += new
                  >>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
                  >>> //
                  >>> // testConnection
                  >>> //
                  >>> this.testConnec tion.Connection String =
                  >>> ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
                  >>> typeof(string)) ));
                  >>> //
                  >>> // sqlSelectComman d1
                  >>> //
                  >>> this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
                  >>> Customers";
                  >>> this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
                  >>> //
                  >>> // testAdapter
                  >>> //
                  >>> this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
                  >>> this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
                  >>> this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
                  >>> this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
                  >>> //
                  >>> // dataSet
                  >>> //
                  >>> this.dataSet.Da taSetName = "testDataSe t";
                  >>> this.dataSet.Lo cale = new System.Globaliz ation.CultureIn fo("en-US");
                  >>> this.Load += new System.EventHan dler(this.Page_ Load);
                  >>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
                  >>>
                  >>> }
                  >>> #endregion
                  >>>
                  >>> private void dataGrid_ItemCr eated(object sender,
                  >>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                  >>> {
                  >>> // Response.Write( e.Item.DataItem .ToString());
                  >>> }
                  >>>
                  >>> private void dataGrid_ItemDa taBound(object sender,
                  >>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                  >>> {
                  >>> // Response.Write( e.Item.DataItem .ToString());
                  >>> }
                  >>> }
                  >>> }
                  >>>
                  >>>
                  >>>
                  >>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                  >>> news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...
                  >>>> Do you set DataMember property? You have to set it to the name of a
                  >>>> table in your dataset.
                  >>>>
                  >>>> In what part of you code do you refer to dataGrid.Items[0].DataItem?
                  >>>> Typically, it should be in one of the item-related events, like
                  >>>> ItemDataBound or ItemCreated.
                  >>>>
                  >>>> If it is not enough, post the relevant part of the .aspx file. Don't
                  >>>> sent attachments.
                  >>>>
                  >>>> Eliyahu
                  >>>>
                  >>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                  >>>> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...
                  >>>>> And the .aspx file and .cs file is provided in the attachment for your
                  >>>>> convenience, if necessary. :)
                  >>>>>
                  >>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                  >>>>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
                  >>>>>> Thanks for your response. Here is the code fragment:
                  >>>>>>
                  >>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                  >>>>>> {
                  >>>>>> // Put user code to initialize the page here
                  >>>>>> testAdapter.Fil l(dataSet);
                  >>>>>> dataGrid.DataSo urce = dataSet;
                  >>>>>> dataGrid.DataBi nd();
                  >>>>>>
                  >>>>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                  >>>>>> NullReferenceEx ception will be thrown here!
                  >>>>>> }
                  >>>>>>
                  >>>>>> The testAdapter executes an SelectCommand as 'select * from
                  >>>>>> Customers' on
                  >>>>>> the Northwind database. Actually, dozens of rows was returned from
                  >>>>>> the
                  >>>>>> database. However, when I tried to print out the DataItem.ToStri ng()
                  >>>>>> using
                  >>>>>> Response.Write( ), a NullReferenceEx ception occured. But it works
                  >>>>>> fine, after
                  >>>>>> I had commented that line of code.
                  >>>>>> I don't know whether I wrote the code in the correct way, would you
                  >>>>>> please
                  >>>>>> point me out? Thanks:)
                  >>>>>>
                  >>>>>>
                  >>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                  >>>>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
                  >>>>>>> There is no difference where the code is.
                  >>>>>>>
                  >>>>>>> Post the relevant fragment from the code and the .aspx file, likely
                  >>>>>>> there
                  >>>>>>> is a problem over there.
                  >>>>>>>
                  >>>>>>> Eliyahu
                  >>>>>>>
                  >>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                  >>>>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
                  >>>>>>>> Hi, all,
                  >>>>>>>> I was tangled by the DataGridItem.Da taItem property. I tried to
                  >>>>>>>> invoke
                  >>>>>>>> that property to get and display the contents that was bound to the
                  >>>>>>>> currently selected row. However, an exception surprised me that it
                  >>>>>>>> said
                  >>>>>>>> "Object reference not set to an instance of an object." How could
                  >>>>>>>> it
                  >>>>>>>> happen? I have set the data source, and called the DataBind().
                  >>>>>>>> I looked up the MSDN documents, and just find some inessential
                  >>>>>>>> descriptions, and a code example. I didn't try that example by
                  >>>>>>>> myself,
                  >>>>>>>> but I noticed a difference that, the example uses the single-file
                  >>>>>>>> code
                  >>>>>>>> (i.e code was written in the .aspx file) while I wrote my code in
                  >>>>>>>> the
                  >>>>>>>> code-behind mode. So I wonder if this is the reason? But I'm afraid
                  >>>>>>>> I
                  >>>>>>>> would be puzzled if MS says the DataItem property will not be
                  >>>>>>>> available
                  >>>>>>>> through code-behind:)
                  >>>>>>>> Any help, please?
                  >>>>>>>>
                  >>>>>>>
                  >>>>>>>
                  >>>>>>
                  >>>>>>
                  >>>>>
                  >>>>>
                  >>>>>
                  >>>>
                  >>>>
                  >>>
                  >>>[/color]
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Laser Lu

                    #10
                    Re: DataGridItem.Da taItem availability?

                    I also did that, but the dataset and dataview both have data contained in
                    them. The only thing that was strange is that DataItem was not set to any
                    object reference in the code-behind. But it indeed works for inline code
                    like <%# Container.DataI tem %> in the .aspx file. So strange, I dont' know
                    why:(

                    "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                    news:OOr8TWfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > Sorry, missed it.
                    >
                    > I think the table name should be "Table1" rather than "Table". Anyway, why
                    > don't you set a breakpoint on the line following the Fill call and check
                    > what is in the dataset?
                    >
                    > Eliyahu
                    >
                    > "Laser Lu" <laser_lu@hotma il.com> wrote in message
                    > news:OIfczPfQGH A.1160@TK2MSFTN GP09.phx.gbl...[color=green]
                    >>I have done that in my last code post. :) Maybe, that is not the key
                    >>point.
                    >>
                    >> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                    >> news:ObUpZGfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=darkred]
                    >>> From the MSDN Library on DataGrid.DataMe mber property:
                    >>>
                    >>> ---
                    >>> If the DataSet or DataViewManager contains only one DataTable, you
                    >>> should set the DataMember to the TableName of that DataTable.
                    >>> ---
                    >>>
                    >>> Just try this. It doesn't hurt.
                    >>>
                    >>> Eliyahu
                    >>>
                    >>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                    >>> news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..
                    >>>>I suppose, there is no need to explicity set the DataMember, if there is
                    >>>>only one DataTable exists in the DataSet. The control will automaitcally
                    >>>>find the first DataTable to bind. The previous code I posted withou
                    >>>>specifyin g DataMember worked fine in my computer:)
                    >>>>
                    >>>> Additionally, I've just followed what you have mentioned, accessing the
                    >>>> DataItem property in item-related events, both ItemCreated and
                    >>>> ItemDataBound. But they still doesn't work, keeping throwing
                    >>>> NullReferenceEx ceptions in both event handlers. :(
                    >>>>
                    >>>> What a pity! I don't know what to do. It really frustrated me! Help,
                    >>>> please!
                    >>>>
                    >>>> BTW: the whole program for Text.aspx was posted below, and just for
                    >>>> your information:
                    >>>>
                    >>>> /// Test.aspx
                    >>>> <%@ Page language="c#" Codebehind="Tes t.aspx.cs"
                    >>>> AutoEventWireup ="false" Inherits="CEIS. Test" %>
                    >>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
                    >>>> <HTML>
                    >>>> <HEAD>
                    >>>> <title>Test</title>
                    >>>> <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
                    >>>> <meta content="C#" name="CODE_LANG UAGE">
                    >>>> <meta content="JavaSc ript" name="vs_defaul tClientScript">
                    >>>> <meta content="http://schemas.microso ft.com/intellisense/ie5"
                    >>>> name="vs_target Schema">
                    >>>> </HEAD>
                    >>>> <body>
                    >>>> <form id="Form1" method="post" runat="server">
                    >>>> <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
                    >>>> </body>
                    >>>> </HTML>
                    >>>>
                    >>>> /// Test.aspx.cs
                    >>>> using System;
                    >>>> using System.Collecti ons;
                    >>>> using System.Componen tModel;
                    >>>> using System.Data;
                    >>>> using System.Drawing;
                    >>>> using System.Web;
                    >>>> using System.Web.Sess ionState;
                    >>>> using System.Web.UI;
                    >>>> using System.Web.UI.W ebControls;
                    >>>> using System.Web.UI.H tmlControls;
                    >>>>
                    >>>> namespace CEIS
                    >>>> {
                    >>>> /// <summary>
                    >>>> /// Summary description for Test.
                    >>>> /// </summary>
                    >>>> public class Test : System.Web.UI.P age
                    >>>> {
                    >>>> protected System.Data.Sql Client.SqlConne ction testConnection;
                    >>>> protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
                    >>>> protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
                    >>>> protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
                    >>>> protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
                    >>>> protected System.Data.Sql Client.SqlDataA dapter testAdapter;
                    >>>> protected System.Data.Dat aSet dataSet;
                    >>>> protected System.Web.UI.W ebControls.Data Grid dataGrid;
                    >>>>
                    >>>> private void Page_Load(objec t sender, System.EventArg s e)
                    >>>> {
                    >>>> // Put user code to initialize the page here
                    >>>> testAdapter.Fil l(dataSet);
                    >>>> dataGrid.DataMe mber = "Table";
                    >>>> dataGrid.DataSo urce = dataSet;
                    >>>> dataGrid.DataBi nd();
                    >>>>
                    >>>> // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
                    >>>> }
                    >>>>
                    >>>> #region Web Form Designer generated code
                    >>>> override protected void OnInit(EventArg s e)
                    >>>> {
                    >>>> //
                    >>>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                    >>>> //
                    >>>> InitializeCompo nent();
                    >>>> base.OnInit(e);
                    >>>> }
                    >>>>
                    >>>> /// <summary>
                    >>>> /// Required method for Designer support - do not modify
                    >>>> /// the contents of this method with the code editor.
                    >>>> /// </summary>
                    >>>> private void InitializeCompo nent()
                    >>>> {
                    >>>> System.Configur ation.AppSettin gsReader configurationAp pSettings = new
                    >>>> System.Configur ation.AppSettin gsReader();
                    >>>> this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
                    >>>> this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
                    >>>> this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
                    >>>> this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
                    >>>> this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
                    >>>> this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
                    >>>> this.dataSet = new System.Data.Dat aSet();
                    >>>>
                    >>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
                    >>>> this.dataGrid.I temCreated += new
                    >>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
                    >>>> this.dataGrid.I temDataBound += new
                    >>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
                    >>>> //
                    >>>> // testConnection
                    >>>> //
                    >>>> this.testConnec tion.Connection String =
                    >>>> ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
                    >>>> typeof(string)) ));
                    >>>> //
                    >>>> // sqlSelectComman d1
                    >>>> //
                    >>>> this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
                    >>>> Customers";
                    >>>> this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
                    >>>> //
                    >>>> // testAdapter
                    >>>> //
                    >>>> this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
                    >>>> this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
                    >>>> this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
                    >>>> this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
                    >>>> //
                    >>>> // dataSet
                    >>>> //
                    >>>> this.dataSet.Da taSetName = "testDataSe t";
                    >>>> this.dataSet.Lo cale = new System.Globaliz ation.CultureIn fo("en-US");
                    >>>> this.Load += new System.EventHan dler(this.Page_ Load);
                    >>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
                    >>>>
                    >>>> }
                    >>>> #endregion
                    >>>>
                    >>>> private void dataGrid_ItemCr eated(object sender,
                    >>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                    >>>> {
                    >>>> // Response.Write( e.Item.DataItem .ToString());
                    >>>> }
                    >>>>
                    >>>> private void dataGrid_ItemDa taBound(object sender,
                    >>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                    >>>> {
                    >>>> // Response.Write( e.Item.DataItem .ToString());
                    >>>> }
                    >>>> }
                    >>>> }
                    >>>>
                    >>>>
                    >>>>
                    >>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                    >>>> news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...
                    >>>>> Do you set DataMember property? You have to set it to the name of a
                    >>>>> table in your dataset.
                    >>>>>
                    >>>>> In what part of you code do you refer to dataGrid.Items[0].DataItem?
                    >>>>> Typically, it should be in one of the item-related events, like
                    >>>>> ItemDataBound or ItemCreated.
                    >>>>>
                    >>>>> If it is not enough, post the relevant part of the .aspx file. Don't
                    >>>>> sent attachments.
                    >>>>>
                    >>>>> Eliyahu
                    >>>>>
                    >>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                    >>>>> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...
                    >>>>>> And the .aspx file and .cs file is provided in the attachment for
                    >>>>>> your convenience, if necessary. :)
                    >>>>>>
                    >>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                    >>>>>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
                    >>>>>>> Thanks for your response. Here is the code fragment:
                    >>>>>>>
                    >>>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                    >>>>>>> {
                    >>>>>>> // Put user code to initialize the page here
                    >>>>>>> testAdapter.Fil l(dataSet);
                    >>>>>>> dataGrid.DataSo urce = dataSet;
                    >>>>>>> dataGrid.DataBi nd();
                    >>>>>>>
                    >>>>>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                    >>>>>>> NullReferenceEx ception will be thrown here!
                    >>>>>>> }
                    >>>>>>>
                    >>>>>>> The testAdapter executes an SelectCommand as 'select * from
                    >>>>>>> Customers' on
                    >>>>>>> the Northwind database. Actually, dozens of rows was returned from
                    >>>>>>> the
                    >>>>>>> database. However, when I tried to print out the DataItem.ToStri ng()
                    >>>>>>> using
                    >>>>>>> Response.Write( ), a NullReferenceEx ception occured. But it works
                    >>>>>>> fine, after
                    >>>>>>> I had commented that line of code.
                    >>>>>>> I don't know whether I wrote the code in the correct way, would you
                    >>>>>>> please
                    >>>>>>> point me out? Thanks:)
                    >>>>>>>
                    >>>>>>>
                    >>>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                    >>>>>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
                    >>>>>>>> There is no difference where the code is.
                    >>>>>>>>
                    >>>>>>>> Post the relevant fragment from the code and the .aspx file, likely
                    >>>>>>>> there
                    >>>>>>>> is a problem over there.
                    >>>>>>>>
                    >>>>>>>> Eliyahu
                    >>>>>>>>
                    >>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                    >>>>>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
                    >>>>>>>>> Hi, all,
                    >>>>>>>>> I was tangled by the DataGridItem.Da taItem property. I tried to
                    >>>>>>>>> invoke
                    >>>>>>>>> that property to get and display the contents that was bound to
                    >>>>>>>>> the
                    >>>>>>>>> currently selected row. However, an exception surprised me that it
                    >>>>>>>>> said
                    >>>>>>>>> "Object reference not set to an instance of an object." How could
                    >>>>>>>>> it
                    >>>>>>>>> happen? I have set the data source, and called the DataBind().
                    >>>>>>>>> I looked up the MSDN documents, and just find some inessential
                    >>>>>>>>> descriptions, and a code example. I didn't try that example by
                    >>>>>>>>> myself,
                    >>>>>>>>> but I noticed a difference that, the example uses the single-file
                    >>>>>>>>> code
                    >>>>>>>>> (i.e code was written in the .aspx file) while I wrote my code in
                    >>>>>>>>> the
                    >>>>>>>>> code-behind mode. So I wonder if this is the reason? But I'm
                    >>>>>>>>> afraid I
                    >>>>>>>>> would be puzzled if MS says the DataItem property will not be
                    >>>>>>>>> available
                    >>>>>>>>> through code-behind:)
                    >>>>>>>>> Any help, please?
                    >>>>>>>>>
                    >>>>>>>>
                    >>>>>>>>
                    >>>>>>>
                    >>>>>>>
                    >>>>>>
                    >>>>>>
                    >>>>>>
                    >>>>>
                    >>>>>
                    >>>>
                    >>>>
                    >>>
                    >>>[/color]
                    >>
                    >>[/color]
                    >
                    >[/color]


                    Comment

                    • Eliyahu Goldin

                      #11
                      Re: DataGridItem.Da taItem availability?

                      Ok, I had another look at your code. Does you grid have a header?

                      In the code
                      [color=blue][color=green][color=darkred]
                      >>>>> private void dataGrid_ItemDa taBound(object sender,
                      >>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                      >>>>> {
                      >>>>> // Response.Write( e.Item.DataItem .ToString());
                      >>>>> }[/color][/color][/color]

                      you should be checking e.Item.ItemType . DataItem is applicable only to
                      ItemType Item and AlternativeItem .

                      You are likely observing DataItem=null for the Header item.

                      Eliyahu

                      "Laser Lu" <laser_lu@hotma il.com> wrote in message
                      news:uL2iVffQGH A.1688@TK2MSFTN GP11.phx.gbl...[color=blue]
                      >I also did that, but the dataset and dataview both have data contained in
                      >them. The only thing that was strange is that DataItem was not set to any
                      >object reference in the code-behind. But it indeed works for inline code
                      >like <%# Container.DataI tem %> in the .aspx file. So strange, I dont' know
                      >why:(
                      >
                      > "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                      > news:OOr8TWfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=green]
                      >> Sorry, missed it.
                      >>
                      >> I think the table name should be "Table1" rather than "Table". Anyway,
                      >> why don't you set a breakpoint on the line following the Fill call and
                      >> check what is in the dataset?
                      >>
                      >> Eliyahu
                      >>
                      >> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                      >> news:OIfczPfQGH A.1160@TK2MSFTN GP09.phx.gbl...[color=darkred]
                      >>>I have done that in my last code post. :) Maybe, that is not the key
                      >>>point.
                      >>>
                      >>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                      >>> news:ObUpZGfQGH A.3984@TK2MSFTN GP14.phx.gbl...
                      >>>> From the MSDN Library on DataGrid.DataMe mber property:
                      >>>>
                      >>>> ---
                      >>>> If the DataSet or DataViewManager contains only one DataTable, you
                      >>>> should set the DataMember to the TableName of that DataTable.
                      >>>> ---
                      >>>>
                      >>>> Just try this. It doesn't hurt.
                      >>>>
                      >>>> Eliyahu
                      >>>>
                      >>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                      >>>> news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..
                      >>>>>I suppose, there is no need to explicity set the DataMember, if there
                      >>>>>is only one DataTable exists in the DataSet. The control will
                      >>>>>automaitca lly find the first DataTable to bind. The previous code I
                      >>>>>posted withou specifying DataMember worked fine in my computer:)
                      >>>>>
                      >>>>> Additionally, I've just followed what you have mentioned, accessing
                      >>>>> the DataItem property in item-related events, both ItemCreated and
                      >>>>> ItemDataBound. But they still doesn't work, keeping throwing
                      >>>>> NullReferenceEx ceptions in both event handlers. :(
                      >>>>>
                      >>>>> What a pity! I don't know what to do. It really frustrated me! Help,
                      >>>>> please!
                      >>>>>
                      >>>>> BTW: the whole program for Text.aspx was posted below, and just for
                      >>>>> your information:
                      >>>>>
                      >>>>> /// Test.aspx
                      >>>>> <%@ Page language="c#" Codebehind="Tes t.aspx.cs"
                      >>>>> AutoEventWireup ="false" Inherits="CEIS. Test" %>
                      >>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
                      >>>>> <HTML>
                      >>>>> <HEAD>
                      >>>>> <title>Test</title>
                      >>>>> <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
                      >>>>> <meta content="C#" name="CODE_LANG UAGE">
                      >>>>> <meta content="JavaSc ript" name="vs_defaul tClientScript">
                      >>>>> <meta content="http://schemas.microso ft.com/intellisense/ie5"
                      >>>>> name="vs_target Schema">
                      >>>>> </HEAD>
                      >>>>> <body>
                      >>>>> <form id="Form1" method="post" runat="server">
                      >>>>> <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
                      >>>>> </body>
                      >>>>> </HTML>
                      >>>>>
                      >>>>> /// Test.aspx.cs
                      >>>>> using System;
                      >>>>> using System.Collecti ons;
                      >>>>> using System.Componen tModel;
                      >>>>> using System.Data;
                      >>>>> using System.Drawing;
                      >>>>> using System.Web;
                      >>>>> using System.Web.Sess ionState;
                      >>>>> using System.Web.UI;
                      >>>>> using System.Web.UI.W ebControls;
                      >>>>> using System.Web.UI.H tmlControls;
                      >>>>>
                      >>>>> namespace CEIS
                      >>>>> {
                      >>>>> /// <summary>
                      >>>>> /// Summary description for Test.
                      >>>>> /// </summary>
                      >>>>> public class Test : System.Web.UI.P age
                      >>>>> {
                      >>>>> protected System.Data.Sql Client.SqlConne ction testConnection;
                      >>>>> protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
                      >>>>> protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
                      >>>>> protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
                      >>>>> protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
                      >>>>> protected System.Data.Sql Client.SqlDataA dapter testAdapter;
                      >>>>> protected System.Data.Dat aSet dataSet;
                      >>>>> protected System.Web.UI.W ebControls.Data Grid dataGrid;
                      >>>>>
                      >>>>> private void Page_Load(objec t sender, System.EventArg s e)
                      >>>>> {
                      >>>>> // Put user code to initialize the page here
                      >>>>> testAdapter.Fil l(dataSet);
                      >>>>> dataGrid.DataMe mber = "Table";
                      >>>>> dataGrid.DataSo urce = dataSet;
                      >>>>> dataGrid.DataBi nd();
                      >>>>>
                      >>>>> // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
                      >>>>> }
                      >>>>>
                      >>>>> #region Web Form Designer generated code
                      >>>>> override protected void OnInit(EventArg s e)
                      >>>>> {
                      >>>>> //
                      >>>>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                      >>>>> //
                      >>>>> InitializeCompo nent();
                      >>>>> base.OnInit(e);
                      >>>>> }
                      >>>>>
                      >>>>> /// <summary>
                      >>>>> /// Required method for Designer support - do not modify
                      >>>>> /// the contents of this method with the code editor.
                      >>>>> /// </summary>
                      >>>>> private void InitializeCompo nent()
                      >>>>> {
                      >>>>> System.Configur ation.AppSettin gsReader configurationAp pSettings =
                      >>>>> new System.Configur ation.AppSettin gsReader();
                      >>>>> this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
                      >>>>> this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
                      >>>>> this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
                      >>>>> this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
                      >>>>> this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
                      >>>>> this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
                      >>>>> this.dataSet = new System.Data.Dat aSet();
                      >>>>>
                      >>>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
                      >>>>> this.dataGrid.I temCreated += new
                      >>>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
                      >>>>> this.dataGrid.I temDataBound += new
                      >>>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
                      >>>>> //
                      >>>>> // testConnection
                      >>>>> //
                      >>>>> this.testConnec tion.Connection String =
                      >>>>> ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
                      >>>>> typeof(string)) ));
                      >>>>> //
                      >>>>> // sqlSelectComman d1
                      >>>>> //
                      >>>>> this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
                      >>>>> Customers";
                      >>>>> this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
                      >>>>> //
                      >>>>> // testAdapter
                      >>>>> //
                      >>>>> this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
                      >>>>> this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
                      >>>>> this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
                      >>>>> this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
                      >>>>> //
                      >>>>> // dataSet
                      >>>>> //
                      >>>>> this.dataSet.Da taSetName = "testDataSe t";
                      >>>>> this.dataSet.Lo cale = new System.Globaliz ation.CultureIn fo("en-US");
                      >>>>> this.Load += new System.EventHan dler(this.Page_ Load);
                      >>>>>
                      >>>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
                      >>>>>
                      >>>>> }
                      >>>>> #endregion
                      >>>>>
                      >>>>> private void dataGrid_ItemCr eated(object sender,
                      >>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                      >>>>> {
                      >>>>> // Response.Write( e.Item.DataItem .ToString());
                      >>>>> }
                      >>>>>
                      >>>>> private void dataGrid_ItemDa taBound(object sender,
                      >>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                      >>>>> {
                      >>>>> // Response.Write( e.Item.DataItem .ToString());
                      >>>>> }
                      >>>>> }
                      >>>>> }
                      >>>>>
                      >>>>>
                      >>>>>
                      >>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                      >>>>> news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...
                      >>>>>> Do you set DataMember property? You have to set it to the name of a
                      >>>>>> table in your dataset.
                      >>>>>>
                      >>>>>> In what part of you code do you refer to dataGrid.Items[0].DataItem?
                      >>>>>> Typically, it should be in one of the item-related events, like
                      >>>>>> ItemDataBound or ItemCreated.
                      >>>>>>
                      >>>>>> If it is not enough, post the relevant part of the .aspx file. Don't
                      >>>>>> sent attachments.
                      >>>>>>
                      >>>>>> Eliyahu
                      >>>>>>
                      >>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                      >>>>>> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...
                      >>>>>>> And the .aspx file and .cs file is provided in the attachment for
                      >>>>>>> your convenience, if necessary. :)
                      >>>>>>>
                      >>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                      >>>>>>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
                      >>>>>>>> Thanks for your response. Here is the code fragment:
                      >>>>>>>>
                      >>>>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                      >>>>>>>> {
                      >>>>>>>> // Put user code to initialize the page here
                      >>>>>>>> testAdapter.Fil l(dataSet);
                      >>>>>>>> dataGrid.DataSo urce = dataSet;
                      >>>>>>>> dataGrid.DataBi nd();
                      >>>>>>>>
                      >>>>>>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                      >>>>>>>> NullReferenceEx ception will be thrown here!
                      >>>>>>>> }
                      >>>>>>>>
                      >>>>>>>> The testAdapter executes an SelectCommand as 'select * from
                      >>>>>>>> Customers' on
                      >>>>>>>> the Northwind database. Actually, dozens of rows was returned from
                      >>>>>>>> the
                      >>>>>>>> database. However, when I tried to print out the
                      >>>>>>>> DataItem.ToStri ng() using
                      >>>>>>>> Response.Write( ), a NullReferenceEx ception occured. But it works
                      >>>>>>>> fine, after
                      >>>>>>>> I had commented that line of code.
                      >>>>>>>> I don't know whether I wrote the code in the correct way, would you
                      >>>>>>>> please
                      >>>>>>>> point me out? Thanks:)
                      >>>>>>>>
                      >>>>>>>>
                      >>>>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                      >>>>>>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
                      >>>>>>>>> There is no difference where the code is.
                      >>>>>>>>>
                      >>>>>>>>> Post the relevant fragment from the code and the .aspx file,
                      >>>>>>>>> likely there
                      >>>>>>>>> is a problem over there.
                      >>>>>>>>>
                      >>>>>>>>> Eliyahu
                      >>>>>>>>>
                      >>>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                      >>>>>>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
                      >>>>>>>>>> Hi, all,
                      >>>>>>>>>> I was tangled by the DataGridItem.Da taItem property. I tried
                      >>>>>>>>>> to invoke
                      >>>>>>>>>> that property to get and display the contents that was bound to
                      >>>>>>>>>> the
                      >>>>>>>>>> currently selected row. However, an exception surprised me that
                      >>>>>>>>>> it said
                      >>>>>>>>>> "Object reference not set to an instance of an object." How could
                      >>>>>>>>>> it
                      >>>>>>>>>> happen? I have set the data source, and called the DataBind().
                      >>>>>>>>>> I looked up the MSDN documents, and just find some inessential
                      >>>>>>>>>> descriptions, and a code example. I didn't try that example by
                      >>>>>>>>>> myself,
                      >>>>>>>>>> but I noticed a difference that, the example uses the single-file
                      >>>>>>>>>> code
                      >>>>>>>>>> (i.e code was written in the .aspx file) while I wrote my code in
                      >>>>>>>>>> the
                      >>>>>>>>>> code-behind mode. So I wonder if this is the reason? But I'm
                      >>>>>>>>>> afraid I
                      >>>>>>>>>> would be puzzled if MS says the DataItem property will not be
                      >>>>>>>>>> available
                      >>>>>>>>>> through code-behind:)
                      >>>>>>>>>> Any help, please?
                      >>>>>>>>>>
                      >>>>>>>>>
                      >>>>>>>>>
                      >>>>>>>>
                      >>>>>>>>
                      >>>>>>>
                      >>>>>>>
                      >>>>>>>
                      >>>>>>
                      >>>>>>
                      >>>>>
                      >>>>>
                      >>>>
                      >>>>
                      >>>
                      >>>[/color]
                      >>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Laser Lu

                        #12
                        Re: DataGridItem.Da taItem availability?

                        Thanks for your deeper look at my code:) However, I'm afraid that's also not
                        the exact reason. I'm sure I had tested the code on Items that are of
                        ItemType.Item/AlternativeItem . They just don't work, always return null
                        DateItem. :(

                        "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                        news:eGAlskfQGH A.5152@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > Ok, I had another look at your code. Does you grid have a header?
                        >
                        > In the code
                        >[color=green][color=darkred]
                        >>>>>> private void dataGrid_ItemDa taBound(object sender,
                        >>>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                        >>>>>> {
                        >>>>>> // Response.Write( e.Item.DataItem .ToString());
                        >>>>>> }[/color][/color]
                        >
                        > you should be checking e.Item.ItemType . DataItem is applicable only to
                        > ItemType Item and AlternativeItem .
                        >
                        > You are likely observing DataItem=null for the Header item.
                        >
                        > Eliyahu
                        >
                        > "Laser Lu" <laser_lu@hotma il.com> wrote in message
                        > news:uL2iVffQGH A.1688@TK2MSFTN GP11.phx.gbl...[color=green]
                        >>I also did that, but the dataset and dataview both have data contained in
                        >>them. The only thing that was strange is that DataItem was not set to any
                        >>object reference in the code-behind. But it indeed works for inline code
                        >>like <%# Container.DataI tem %> in the .aspx file. So strange, I dont' know
                        >>why:(
                        >>
                        >> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                        >> news:OOr8TWfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=darkred]
                        >>> Sorry, missed it.
                        >>>
                        >>> I think the table name should be "Table1" rather than "Table". Anyway,
                        >>> why don't you set a breakpoint on the line following the Fill call and
                        >>> check what is in the dataset?
                        >>>
                        >>> Eliyahu
                        >>>
                        >>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                        >>> news:OIfczPfQGH A.1160@TK2MSFTN GP09.phx.gbl...
                        >>>>I have done that in my last code post. :) Maybe, that is not the key
                        >>>>point.
                        >>>>
                        >>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                        >>>> news:ObUpZGfQGH A.3984@TK2MSFTN GP14.phx.gbl...
                        >>>>> From the MSDN Library on DataGrid.DataMe mber property:
                        >>>>>
                        >>>>> ---
                        >>>>> If the DataSet or DataViewManager contains only one DataTable, you
                        >>>>> should set the DataMember to the TableName of that DataTable.
                        >>>>> ---
                        >>>>>
                        >>>>> Just try this. It doesn't hurt.
                        >>>>>
                        >>>>> Eliyahu
                        >>>>>
                        >>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                        >>>>> news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..
                        >>>>>>I suppose, there is no need to explicity set the DataMember, if there
                        >>>>>>is only one DataTable exists in the DataSet. The control will
                        >>>>>>automaitc ally find the first DataTable to bind. The previous code I
                        >>>>>>posted withou specifying DataMember worked fine in my computer:)
                        >>>>>>
                        >>>>>> Additionally, I've just followed what you have mentioned, accessing
                        >>>>>> the DataItem property in item-related events, both ItemCreated and
                        >>>>>> ItemDataBound. But they still doesn't work, keeping throwing
                        >>>>>> NullReferenceEx ceptions in both event handlers. :(
                        >>>>>>
                        >>>>>> What a pity! I don't know what to do. It really frustrated me! Help,
                        >>>>>> please!
                        >>>>>>
                        >>>>>> BTW: the whole program for Text.aspx was posted below, and just for
                        >>>>>> your information:
                        >>>>>>
                        >>>>>> /// Test.aspx
                        >>>>>> <%@ Page language="c#" Codebehind="Tes t.aspx.cs"
                        >>>>>> AutoEventWireup ="false" Inherits="CEIS. Test" %>
                        >>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
                        >>>>>> <HTML>
                        >>>>>> <HEAD>
                        >>>>>> <title>Test</title>
                        >>>>>> <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
                        >>>>>> <meta content="C#" name="CODE_LANG UAGE">
                        >>>>>> <meta content="JavaSc ript" name="vs_defaul tClientScript">
                        >>>>>> <meta content="http://schemas.microso ft.com/intellisense/ie5"
                        >>>>>> name="vs_target Schema">
                        >>>>>> </HEAD>
                        >>>>>> <body>
                        >>>>>> <form id="Form1" method="post" runat="server">
                        >>>>>> <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
                        >>>>>> </body>
                        >>>>>> </HTML>
                        >>>>>>
                        >>>>>> /// Test.aspx.cs
                        >>>>>> using System;
                        >>>>>> using System.Collecti ons;
                        >>>>>> using System.Componen tModel;
                        >>>>>> using System.Data;
                        >>>>>> using System.Drawing;
                        >>>>>> using System.Web;
                        >>>>>> using System.Web.Sess ionState;
                        >>>>>> using System.Web.UI;
                        >>>>>> using System.Web.UI.W ebControls;
                        >>>>>> using System.Web.UI.H tmlControls;
                        >>>>>>
                        >>>>>> namespace CEIS
                        >>>>>> {
                        >>>>>> /// <summary>
                        >>>>>> /// Summary description for Test.
                        >>>>>> /// </summary>
                        >>>>>> public class Test : System.Web.UI.P age
                        >>>>>> {
                        >>>>>> protected System.Data.Sql Client.SqlConne ction testConnection;
                        >>>>>> protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
                        >>>>>> protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
                        >>>>>> protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
                        >>>>>> protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
                        >>>>>> protected System.Data.Sql Client.SqlDataA dapter testAdapter;
                        >>>>>> protected System.Data.Dat aSet dataSet;
                        >>>>>> protected System.Web.UI.W ebControls.Data Grid dataGrid;
                        >>>>>>
                        >>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                        >>>>>> {
                        >>>>>> // Put user code to initialize the page here
                        >>>>>> testAdapter.Fil l(dataSet);
                        >>>>>> dataGrid.DataMe mber = "Table";
                        >>>>>> dataGrid.DataSo urce = dataSet;
                        >>>>>> dataGrid.DataBi nd();
                        >>>>>>
                        >>>>>> // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
                        >>>>>> }
                        >>>>>>
                        >>>>>> #region Web Form Designer generated code
                        >>>>>> override protected void OnInit(EventArg s e)
                        >>>>>> {
                        >>>>>> //
                        >>>>>> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                        >>>>>> //
                        >>>>>> InitializeCompo nent();
                        >>>>>> base.OnInit(e);
                        >>>>>> }
                        >>>>>>
                        >>>>>> /// <summary>
                        >>>>>> /// Required method for Designer support - do not modify
                        >>>>>> /// the contents of this method with the code editor.
                        >>>>>> /// </summary>
                        >>>>>> private void InitializeCompo nent()
                        >>>>>> {
                        >>>>>> System.Configur ation.AppSettin gsReader configurationAp pSettings =
                        >>>>>> new System.Configur ation.AppSettin gsReader();
                        >>>>>> this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
                        >>>>>> this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
                        >>>>>> this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
                        >>>>>> this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
                        >>>>>> this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
                        >>>>>> this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
                        >>>>>> this.dataSet = new System.Data.Dat aSet();
                        >>>>>>
                        >>>>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
                        >>>>>> this.dataGrid.I temCreated += new
                        >>>>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
                        >>>>>> this.dataGrid.I temDataBound += new
                        >>>>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
                        >>>>>> //
                        >>>>>> // testConnection
                        >>>>>> //
                        >>>>>> this.testConnec tion.Connection String =
                        >>>>>> ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
                        >>>>>> typeof(string)) ));
                        >>>>>> //
                        >>>>>> // sqlSelectComman d1
                        >>>>>> //
                        >>>>>> this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
                        >>>>>> Customers";
                        >>>>>> this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
                        >>>>>> //
                        >>>>>> // testAdapter
                        >>>>>> //
                        >>>>>> this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
                        >>>>>> this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
                        >>>>>> this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
                        >>>>>> this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
                        >>>>>> //
                        >>>>>> // dataSet
                        >>>>>> //
                        >>>>>> this.dataSet.Da taSetName = "testDataSe t";
                        >>>>>> this.dataSet.Lo cale = new
                        >>>>>> System.Globaliz ation.CultureIn fo("en-US");
                        >>>>>> this.Load += new System.EventHan dler(this.Page_ Load);
                        >>>>>>
                        >>>>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
                        >>>>>>
                        >>>>>> }
                        >>>>>> #endregion
                        >>>>>>
                        >>>>>> private void dataGrid_ItemCr eated(object sender,
                        >>>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                        >>>>>> {
                        >>>>>> // Response.Write( e.Item.DataItem .ToString());
                        >>>>>> }
                        >>>>>>
                        >>>>>> private void dataGrid_ItemDa taBound(object sender,
                        >>>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                        >>>>>> {
                        >>>>>> // Response.Write( e.Item.DataItem .ToString());
                        >>>>>> }
                        >>>>>> }
                        >>>>>> }
                        >>>>>>
                        >>>>>>
                        >>>>>>
                        >>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                        >>>>>> news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...
                        >>>>>>> Do you set DataMember property? You have to set it to the name of a
                        >>>>>>> table in your dataset.
                        >>>>>>>
                        >>>>>>> In what part of you code do you refer to dataGrid.Items[0].DataItem?
                        >>>>>>> Typically, it should be in one of the item-related events, like
                        >>>>>>> ItemDataBound or ItemCreated.
                        >>>>>>>
                        >>>>>>> If it is not enough, post the relevant part of the .aspx file. Don't
                        >>>>>>> sent attachments.
                        >>>>>>>
                        >>>>>>> Eliyahu
                        >>>>>>>
                        >>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                        >>>>>>> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...
                        >>>>>>>> And the .aspx file and .cs file is provided in the attachment for
                        >>>>>>>> your convenience, if necessary. :)
                        >>>>>>>>
                        >>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                        >>>>>>>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
                        >>>>>>>>> Thanks for your response. Here is the code fragment:
                        >>>>>>>>>
                        >>>>>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                        >>>>>>>>> {
                        >>>>>>>>> // Put user code to initialize the page here
                        >>>>>>>>> testAdapter.Fil l(dataSet);
                        >>>>>>>>> dataGrid.DataSo urce = dataSet;
                        >>>>>>>>> dataGrid.DataBi nd();
                        >>>>>>>>>
                        >>>>>>>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                        >>>>>>>>> NullReferenceEx ception will be thrown here!
                        >>>>>>>>> }
                        >>>>>>>>>
                        >>>>>>>>> The testAdapter executes an SelectCommand as 'select * from
                        >>>>>>>>> Customers' on
                        >>>>>>>>> the Northwind database. Actually, dozens of rows was returned from
                        >>>>>>>>> the
                        >>>>>>>>> database. However, when I tried to print out the
                        >>>>>>>>> DataItem.ToStri ng() using
                        >>>>>>>>> Response.Write( ), a NullReferenceEx ception occured. But it works
                        >>>>>>>>> fine, after
                        >>>>>>>>> I had commented that line of code.
                        >>>>>>>>> I don't know whether I wrote the code in the correct way, would
                        >>>>>>>>> you please
                        >>>>>>>>> point me out? Thanks:)
                        >>>>>>>>>
                        >>>>>>>>>
                        >>>>>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                        >>>>>>>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
                        >>>>>>>>>> There is no difference where the code is.
                        >>>>>>>>>>
                        >>>>>>>>>> Post the relevant fragment from the code and the .aspx file,
                        >>>>>>>>>> likely there
                        >>>>>>>>>> is a problem over there.
                        >>>>>>>>>>
                        >>>>>>>>>> Eliyahu
                        >>>>>>>>>>
                        >>>>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                        >>>>>>>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
                        >>>>>>>>>>> Hi, all,
                        >>>>>>>>>>> I was tangled by the DataGridItem.Da taItem property. I tried
                        >>>>>>>>>>> to invoke
                        >>>>>>>>>>> that property to get and display the contents that was bound to
                        >>>>>>>>>>> the
                        >>>>>>>>>>> currently selected row. However, an exception surprised me that
                        >>>>>>>>>>> it said
                        >>>>>>>>>>> "Object reference not set to an instance of an object." How
                        >>>>>>>>>>> could it
                        >>>>>>>>>>> happen? I have set the data source, and called the DataBind().
                        >>>>>>>>>>> I looked up the MSDN documents, and just find some
                        >>>>>>>>>>> inessential
                        >>>>>>>>>>> descriptions, and a code example. I didn't try that example by
                        >>>>>>>>>>> myself,
                        >>>>>>>>>>> but I noticed a difference that, the example uses the
                        >>>>>>>>>>> single-file code
                        >>>>>>>>>>> (i.e code was written in the .aspx file) while I wrote my code
                        >>>>>>>>>>> in the
                        >>>>>>>>>>> code-behind mode. So I wonder if this is the reason? But I'm
                        >>>>>>>>>>> afraid I
                        >>>>>>>>>>> would be puzzled if MS says the DataItem property will not be
                        >>>>>>>>>>> available
                        >>>>>>>>>>> through code-behind:)
                        >>>>>>>>>>> Any help, please?
                        >>>>>>>>>>>
                        >>>>>>>>>>
                        >>>>>>>>>>
                        >>>>>>>>>
                        >>>>>>>>>
                        >>>>>>>>
                        >>>>>>>>
                        >>>>>>>>
                        >>>>>>>
                        >>>>>>>
                        >>>>>>
                        >>>>>>
                        >>>>>
                        >>>>>
                        >>>>
                        >>>>
                        >>>
                        >>>[/color]
                        >>
                        >>[/color]
                        >
                        >[/color]


                        Comment

                        • Laser Lu

                          #13
                          Re: DataGridItem.Da taItem availability?

                          Sorry, 'DataItem', not 'DateItem'.:)

                          "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          news:%23QXK4tfQ GHA.3916@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                          > Thanks for your deeper look at my code:) However, I'm afraid that's also
                          > not the exact reason. I'm sure I had tested the code on Items that are of
                          > ItemType.Item/AlternativeItem . They just don't work, always return null
                          > DateItem. :(
                          >
                          > "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                          > news:eGAlskfQGH A.5152@TK2MSFTN GP10.phx.gbl...[color=green]
                          >> Ok, I had another look at your code. Does you grid have a header?
                          >>
                          >> In the code
                          >>[color=darkred]
                          >>>>>>> private void dataGrid_ItemDa taBound(object sender,
                          >>>>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                          >>>>>>> {
                          >>>>>>> // Response.Write( e.Item.DataItem .ToString());
                          >>>>>>> }[/color]
                          >>
                          >> you should be checking e.Item.ItemType . DataItem is applicable only to
                          >> ItemType Item and AlternativeItem .
                          >>
                          >> You are likely observing DataItem=null for the Header item.
                          >>
                          >> Eliyahu
                          >>
                          >> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          >> news:uL2iVffQGH A.1688@TK2MSFTN GP11.phx.gbl...[color=darkred]
                          >>>I also did that, but the dataset and dataview both have data contained in
                          >>>them. The only thing that was strange is that DataItem was not set to any
                          >>>object reference in the code-behind. But it indeed works for inline code
                          >>>like <%# Container.DataI tem %> in the .aspx file. So strange, I dont'
                          >>>know why:(
                          >>>
                          >>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                          >>> news:OOr8TWfQGH A.3984@TK2MSFTN GP14.phx.gbl...
                          >>>> Sorry, missed it.
                          >>>>
                          >>>> I think the table name should be "Table1" rather than "Table". Anyway,
                          >>>> why don't you set a breakpoint on the line following the Fill call and
                          >>>> check what is in the dataset?
                          >>>>
                          >>>> Eliyahu
                          >>>>
                          >>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          >>>> news:OIfczPfQGH A.1160@TK2MSFTN GP09.phx.gbl...
                          >>>>>I have done that in my last code post. :) Maybe, that is not the key
                          >>>>>point.
                          >>>>>
                          >>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                          >>>>> news:ObUpZGfQGH A.3984@TK2MSFTN GP14.phx.gbl...
                          >>>>>> From the MSDN Library on DataGrid.DataMe mber property:
                          >>>>>>
                          >>>>>> ---
                          >>>>>> If the DataSet or DataViewManager contains only one DataTable, you
                          >>>>>> should set the DataMember to the TableName of that DataTable.
                          >>>>>> ---
                          >>>>>>
                          >>>>>> Just try this. It doesn't hurt.
                          >>>>>>
                          >>>>>> Eliyahu
                          >>>>>>
                          >>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          >>>>>> news:%234fL3BfQ GHA.4344@TK2MSF TNGP12.phx.gbl. ..
                          >>>>>>>I suppose, there is no need to explicity set the DataMember, if there
                          >>>>>>>is only one DataTable exists in the DataSet. The control will
                          >>>>>>>automait cally find the first DataTable to bind. The previous code I
                          >>>>>>>posted withou specifying DataMember worked fine in my computer:)
                          >>>>>>>
                          >>>>>>> Additionally, I've just followed what you have mentioned, accessing
                          >>>>>>> the DataItem property in item-related events, both ItemCreated and
                          >>>>>>> ItemDataBound. But they still doesn't work, keeping throwing
                          >>>>>>> NullReferenceEx ceptions in both event handlers. :(
                          >>>>>>>
                          >>>>>>> What a pity! I don't know what to do. It really frustrated me! Help,
                          >>>>>>> please!
                          >>>>>>>
                          >>>>>>> BTW: the whole program for Text.aspx was posted below, and just for
                          >>>>>>> your information:
                          >>>>>>>
                          >>>>>>> /// Test.aspx
                          >>>>>>> <%@ Page language="c#" Codebehind="Tes t.aspx.cs"
                          >>>>>>> AutoEventWireup ="false" Inherits="CEIS. Test" %>
                          >>>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
                          >>>>>>> <HTML>
                          >>>>>>> <HEAD>
                          >>>>>>> <title>Test</title>
                          >>>>>>> <meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
                          >>>>>>> <meta content="C#" name="CODE_LANG UAGE">
                          >>>>>>> <meta content="JavaSc ript" name="vs_defaul tClientScript">
                          >>>>>>> <meta content="http://schemas.microso ft.com/intellisense/ie5"
                          >>>>>>> name="vs_target Schema">
                          >>>>>>> </HEAD>
                          >>>>>>> <body>
                          >>>>>>> <form id="Form1" method="post" runat="server">
                          >>>>>>> <asp:datagrid id="dataGrid" runat="server"> </asp:datagrid></form>
                          >>>>>>> </body>
                          >>>>>>> </HTML>
                          >>>>>>>
                          >>>>>>> /// Test.aspx.cs
                          >>>>>>> using System;
                          >>>>>>> using System.Collecti ons;
                          >>>>>>> using System.Componen tModel;
                          >>>>>>> using System.Data;
                          >>>>>>> using System.Drawing;
                          >>>>>>> using System.Web;
                          >>>>>>> using System.Web.Sess ionState;
                          >>>>>>> using System.Web.UI;
                          >>>>>>> using System.Web.UI.W ebControls;
                          >>>>>>> using System.Web.UI.H tmlControls;
                          >>>>>>>
                          >>>>>>> namespace CEIS
                          >>>>>>> {
                          >>>>>>> /// <summary>
                          >>>>>>> /// Summary description for Test.
                          >>>>>>> /// </summary>
                          >>>>>>> public class Test : System.Web.UI.P age
                          >>>>>>> {
                          >>>>>>> protected System.Data.Sql Client.SqlConne ction testConnection;
                          >>>>>>> protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
                          >>>>>>> protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
                          >>>>>>> protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
                          >>>>>>> protected System.Data.Sql Client.SqlComma nd sqlDeleteComman d1;
                          >>>>>>> protected System.Data.Sql Client.SqlDataA dapter testAdapter;
                          >>>>>>> protected System.Data.Dat aSet dataSet;
                          >>>>>>> protected System.Web.UI.W ebControls.Data Grid dataGrid;
                          >>>>>>>
                          >>>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                          >>>>>>> {
                          >>>>>>> // Put user code to initialize the page here
                          >>>>>>> testAdapter.Fil l(dataSet);
                          >>>>>>> dataGrid.DataMe mber = "Table";
                          >>>>>>> dataGrid.DataSo urce = dataSet;
                          >>>>>>> dataGrid.DataBi nd();
                          >>>>>>>
                          >>>>>>> // Response.Write( dataGrid.Items[0].DataItem.ToStr ing());
                          >>>>>>> }
                          >>>>>>>
                          >>>>>>> #region Web Form Designer generated code
                          >>>>>>> override protected void OnInit(EventArg s e)
                          >>>>>>> {
                          >>>>>>> //
                          >>>>>>> // CODEGEN: This call is required by the ASP.NET Web Form
                          >>>>>>> Designer.
                          >>>>>>> //
                          >>>>>>> InitializeCompo nent();
                          >>>>>>> base.OnInit(e);
                          >>>>>>> }
                          >>>>>>>
                          >>>>>>> /// <summary>
                          >>>>>>> /// Required method for Designer support - do not modify
                          >>>>>>> /// the contents of this method with the code editor.
                          >>>>>>> /// </summary>
                          >>>>>>> private void InitializeCompo nent()
                          >>>>>>> {
                          >>>>>>> System.Configur ation.AppSettin gsReader configurationAp pSettings =
                          >>>>>>> new System.Configur ation.AppSettin gsReader();
                          >>>>>>> this.testConnec tion = new System.Data.Sql Client.SqlConne ction();
                          >>>>>>> this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
                          >>>>>>> this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
                          >>>>>>> this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
                          >>>>>>> this.sqlDeleteC ommand1 = new System.Data.Sql Client.SqlComma nd();
                          >>>>>>> this.testAdapte r = new System.Data.Sql Client.SqlDataA dapter();
                          >>>>>>> this.dataSet = new System.Data.Dat aSet();
                          >>>>>>>
                          >>>>>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).B eginInit();
                          >>>>>>> this.dataGrid.I temCreated += new
                          >>>>>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemCreate d);
                          >>>>>>> this.dataGrid.I temDataBound += new
                          >>>>>>> System.Web.UI.W ebControls.Data GridItemEventHa ndler(this.data Grid_ItemDataBo und);
                          >>>>>>> //
                          >>>>>>> // testConnection
                          >>>>>>> //
                          >>>>>>> this.testConnec tion.Connection String =
                          >>>>>>> ((string)(confi gurationAppSett ings.GetValue(" testConnection. ConnectionStrin g",
                          >>>>>>> typeof(string)) ));
                          >>>>>>> //
                          >>>>>>> // sqlSelectComman d1
                          >>>>>>> //
                          >>>>>>> this.sqlSelectC ommand1.Command Text = "SELECT Customers.* FROM
                          >>>>>>> Customers";
                          >>>>>>> this.sqlSelectC ommand1.Connect ion = this.testConnec tion;
                          >>>>>>> //
                          >>>>>>> // testAdapter
                          >>>>>>> //
                          >>>>>>> this.testAdapte r.DeleteCommand = this.sqlDeleteC ommand1;
                          >>>>>>> this.testAdapte r.InsertCommand = this.sqlInsertC ommand1;
                          >>>>>>> this.testAdapte r.SelectCommand = this.sqlSelectC ommand1;
                          >>>>>>> this.testAdapte r.UpdateCommand = this.sqlUpdateC ommand1;
                          >>>>>>> //
                          >>>>>>> // dataSet
                          >>>>>>> //
                          >>>>>>> this.dataSet.Da taSetName = "testDataSe t";
                          >>>>>>> this.dataSet.Lo cale = new
                          >>>>>>> System.Globaliz ation.CultureIn fo("en-US");
                          >>>>>>> this.Load += new System.EventHan dler(this.Page_ Load);
                          >>>>>>>
                          >>>>>>> ((System.Compon entModel.ISuppo rtInitialize)(t his.dataSet)).E ndInit();
                          >>>>>>>
                          >>>>>>> }
                          >>>>>>> #endregion
                          >>>>>>>
                          >>>>>>> private void dataGrid_ItemCr eated(object sender,
                          >>>>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                          >>>>>>> {
                          >>>>>>> // Response.Write( e.Item.DataItem .ToString());
                          >>>>>>> }
                          >>>>>>>
                          >>>>>>> private void dataGrid_ItemDa taBound(object sender,
                          >>>>>>> System.Web.UI.W ebControls.Data GridItemEventAr gs e)
                          >>>>>>> {
                          >>>>>>> // Response.Write( e.Item.DataItem .ToString());
                          >>>>>>> }
                          >>>>>>> }
                          >>>>>>> }
                          >>>>>>>
                          >>>>>>>
                          >>>>>>>
                          >>>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                          >>>>>>> news:ORhsjUeQGH A.1728@TK2MSFTN GP11.phx.gbl...
                          >>>>>>>> Do you set DataMember property? You have to set it to the name of a
                          >>>>>>>> table in your dataset.
                          >>>>>>>>
                          >>>>>>>> In what part of you code do you refer to
                          >>>>>>>> dataGrid.Items[0].DataItem? Typically, it should be in one of the
                          >>>>>>>> item-related events, like ItemDataBound or ItemCreated.
                          >>>>>>>>
                          >>>>>>>> If it is not enough, post the relevant part of the .aspx file.
                          >>>>>>>> Don't sent attachments.
                          >>>>>>>>
                          >>>>>>>> Eliyahu
                          >>>>>>>>
                          >>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          >>>>>>>> news:e5dnfqdQGH A.648@TK2MSFTNG P14.phx.gbl...
                          >>>>>>>>> And the .aspx file and .cs file is provided in the attachment for
                          >>>>>>>>> your convenience, if necessary. :)
                          >>>>>>>>>
                          >>>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          >>>>>>>>> news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..
                          >>>>>>>>>> Thanks for your response. Here is the code fragment:
                          >>>>>>>>>>
                          >>>>>>>>>> private void Page_Load(objec t sender, System.EventArg s e)
                          >>>>>>>>>> {
                          >>>>>>>>>> // Put user code to initialize the page here
                          >>>>>>>>>> testAdapter.Fil l(dataSet);
                          >>>>>>>>>> dataGrid.DataSo urce = dataSet;
                          >>>>>>>>>> dataGrid.DataBi nd();
                          >>>>>>>>>>
                          >>>>>>>>>> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                          >>>>>>>>>> NullReferenceEx ception will be thrown here!
                          >>>>>>>>>> }
                          >>>>>>>>>>
                          >>>>>>>>>> The testAdapter executes an SelectCommand as 'select * from
                          >>>>>>>>>> Customers' on
                          >>>>>>>>>> the Northwind database. Actually, dozens of rows was returned
                          >>>>>>>>>> from the
                          >>>>>>>>>> database. However, when I tried to print out the
                          >>>>>>>>>> DataItem.ToStri ng() using
                          >>>>>>>>>> Response.Write( ), a NullReferenceEx ception occured. But it works
                          >>>>>>>>>> fine, after
                          >>>>>>>>>> I had commented that line of code.
                          >>>>>>>>>> I don't know whether I wrote the code in the correct way, would
                          >>>>>>>>>> you please
                          >>>>>>>>>> point me out? Thanks:)
                          >>>>>>>>>>
                          >>>>>>>>>>
                          >>>>>>>>>> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in
                          >>>>>>>>>> message
                          >>>>>>>>>> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..
                          >>>>>>>>>>> There is no difference where the code is.
                          >>>>>>>>>>>
                          >>>>>>>>>>> Post the relevant fragment from the code and the .aspx file,
                          >>>>>>>>>>> likely there
                          >>>>>>>>>>> is a problem over there.
                          >>>>>>>>>>>
                          >>>>>>>>>>> Eliyahu
                          >>>>>>>>>>>
                          >>>>>>>>>>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                          >>>>>>>>>>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
                          >>>>>>>>>>>> Hi, all,
                          >>>>>>>>>>>> I was tangled by the DataGridItem.Da taItem property. I tried
                          >>>>>>>>>>>> to invoke
                          >>>>>>>>>>>> that property to get and display the contents that was bound to
                          >>>>>>>>>>>> the
                          >>>>>>>>>>>> currently selected row. However, an exception surprised me that
                          >>>>>>>>>>>> it said
                          >>>>>>>>>>>> "Object reference not set to an instance of an object." How
                          >>>>>>>>>>>> could it
                          >>>>>>>>>>>> happen? I have set the data source, and called the DataBind().
                          >>>>>>>>>>>> I looked up the MSDN documents, and just find some
                          >>>>>>>>>>>> inessential
                          >>>>>>>>>>>> descriptions, and a code example. I didn't try that example by
                          >>>>>>>>>>>> myself,
                          >>>>>>>>>>>> but I noticed a difference that, the example uses the
                          >>>>>>>>>>>> single-file code
                          >>>>>>>>>>>> (i.e code was written in the .aspx file) while I wrote my code
                          >>>>>>>>>>>> in the
                          >>>>>>>>>>>> code-behind mode. So I wonder if this is the reason? But I'm
                          >>>>>>>>>>>> afraid I
                          >>>>>>>>>>>> would be puzzled if MS says the DataItem property will not be
                          >>>>>>>>>>>> available
                          >>>>>>>>>>>> through code-behind:)
                          >>>>>>>>>>>> Any help, please?
                          >>>>>>>>>>>>
                          >>>>>>>>>>>
                          >>>>>>>>>>>
                          >>>>>>>>>>
                          >>>>>>>>>>
                          >>>>>>>>>
                          >>>>>>>>>
                          >>>>>>>>>
                          >>>>>>>>
                          >>>>>>>>
                          >>>>>>>
                          >>>>>>>
                          >>>>>>
                          >>>>>>
                          >>>>>
                          >>>>>
                          >>>>
                          >>>>
                          >>>
                          >>>[/color]
                          >>
                          >>[/color]
                          >
                          >[/color]


                          Comment

                          • Laser Lu

                            #14
                            Re: DataGridItem.Da taItem availability?

                            Nobody knows that?

                            "Laser Lu" <laser_lu@hotma il.com> wrote in message
                            news:u66xfSfQGH A.3984@TK2MSFTN GP14.phx.gbl...[color=blue]
                            > Anybody else knows the reason? please help! Thanks a lot!!
                            >
                            > "Laser Lu" <laser_lu@hotma il.com> wrote in message
                            > news:%23J9MRldQ GHA.3896@TK2MSF TNGP15.phx.gbl. ..[color=green]
                            >> Thanks for your response. Here is the code fragment:
                            >>
                            >> private void Page_Load(objec t sender, System.EventArg s e)
                            >> {
                            >> // Put user code to initialize the page here
                            >> testAdapter.Fil l(dataSet);
                            >> dataGrid.DataSo urce = dataSet;
                            >> dataGrid.DataBi nd();
                            >>
                            >> Response.Write( dataGrid.Items[0].DataItem.ToStr ing()); // A
                            >> NullReferenceEx ception will be thrown here!
                            >> }
                            >>
                            >> The testAdapter executes an SelectCommand as 'select * from Customers' on
                            >> the Northwind database. Actually, dozens of rows was returned from the
                            >> database. However, when I tried to print out the DataItem.ToStri ng()
                            >> using Response.Write( ), a NullReferenceEx ception occured. But it works
                            >> fine, after I had commented that line of code.
                            >> I don't know whether I wrote the code in the correct way, would you
                            >> please point me out? Thanks:)
                            >>
                            >>
                            >> "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
                            >> news:%23y3c4NdQ GHA.5152@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                            >>> There is no difference where the code is.
                            >>>
                            >>> Post the relevant fragment from the code and the .aspx file, likely
                            >>> there is a problem over there.
                            >>>
                            >>> Eliyahu
                            >>>
                            >>> "Laser Lu" <laser_lu@hotma il.com> wrote in message
                            >>> news:u2pG8FdQGH A.516@TK2MSFTNG P15.phx.gbl...
                            >>>> Hi, all,
                            >>>> I was tangled by the DataGridItem.Da taItem property. I tried to
                            >>>> invoke that property to get and display the contents that was bound to
                            >>>> the currently selected row. However, an exception surprised me that it
                            >>>> said "Object reference not set to an instance of an object." How could
                            >>>> it happen? I have set the data source, and called the DataBind().
                            >>>> I looked up the MSDN documents, and just find some inessential
                            >>>> descriptions, and a code example. I didn't try that example by myself,
                            >>>> but I noticed a difference that, the example uses the single-file code
                            >>>> (i.e code was written in the .aspx file) while I wrote my code in the
                            >>>> code-behind mode. So I wonder if this is the reason? But I'm afraid I
                            >>>> would be puzzled if MS says the DataItem property will not be available
                            >>>> through code-behind:)
                            >>>> Any help, please?
                            >>>>
                            >>>
                            >>>[/color]
                            >>
                            >>[/color]
                            >
                            >[/color]


                            Comment

                            • pedaammulu@yahoo.com

                              #15
                              Re: DataGridItem.Da taItem availability?

                              Hi,


                              The following code should work.

                              Dim myConnection As SqlConnection = New SqlConnection(" Data
                              Source=SYS1;Int egrated Security=SSPI;I nitial Catalog=NorthWi nd")
                              Const strSQL As String = "SELECT CustomerID, CompanyName,
                              ContactName FROM Customers"
                              Dim myAdapter As SqlDataAdapter = New SqlDataAdapter( strSQL,
                              myConnection)
                              Protected WithEvents Mygrid_ID As
                              System.Web.UI.W ebControls.Data Grid
                              Dim dstcustomers As New DataSet()

                              Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
                              System.EventArg s) Handles MyBase.Load
                              'Put user code to initialize the page here

                              If Not IsPostBack() Then
                              BindDataGrid()
                              End If
                              Response.Write( "dfgg")
                              Response.Write( Mygrid_ID.Items (0).Cells(0).Te xt)
                              End Sub

                              Sub BindDataGrid()
                              myConnection.Op en()
                              myAdapter.Fill( dstcustomers, "Customers" )
                              Mygrid_ID.DataS ource = dstcustomers
                              Mygrid_ID.DataB ind()
                              End Sub

                              End Class


                              Regards
                              Bhar
                              Books for programmers
                              Desde 1983, situados en Torrijos (Castilla-La Mancha) Jamones El Chato ha conseguido reconocimiento posicionando sus productos no sólo en España sino internacionalmente.


                              Comment

                              Working...