Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    trying to bind datagrid column text to radiobuttonlist

    Hi,
    im trying to use a datagrid to display a survey which is stored in a db. Theres 3 columns - column 1 to store the question number,column 2 to store the actual question and column 3 to store the different answer options.I wanted to bind the diff answeroptions in column3 to a radiobuttonlist and this is when the probs started

    i keep getting this error:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    heres the code im using:

    Code:
    Sub BindDataGrid()
       Dim conn As New OdbcConnection(ConnStr)
    
          Dim cmd As New OdbcCommand("SELECT Questions.qid,Questions.qtext,ans_options.anstext from questions inner join ans_options on questions.qid = ans_options.qid ", conn)
        
             conn.Open()
             DataGrid1.DataSource = cmd.ExecuteReader((CommandBehavior.CloseConnection Or CommandBehavior.SingleResult))
             DataGrid1.DataBind()
        
    
    End Sub 'BindDataGrid
    
    'bind radiobuttonlist to third column
    Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
       Dim Conn As New OdbcConnection("ConnStr")
       Dim Cmd As New OdbcCommand("SELECT anstext FROM Ans_Options", Conn)
       Dim dtrAnswerOptions As OdbcDataReader
       Dim rbl As RadioButtonList
    
     
          Conn.Open()
            
          Select Case e.Item.ItemType
             Case ListItemType.Item, ListItemType.AlternatingItem
                rbl = DirectCast(e.Item.FindControl("rblist"), RadioButtonList)
                 dtrAnswerOptions = Cmd.ExecuteReader()
    
                rbl.DataSource = dtrAnswerOptions
                rbl.DataValueField = "anstext"
                rbl.DataTextField = "anstext"
                rbl.DataBind()
             Case Else
          End Select
      
    End Sub
    
    </script>
     </head>
     <body>
     
     
     <p align="center">survey </a></p>
     
     <form runat="server">
       <asp:DataGrid ID="DataGrid1" HorizontalAlign="Center" CellPadding="3"
         HeaderStyle-BackColor="#F7F7F7" EditItemStyle-BackColor="#F7F7F7"
         AutoGenerateColumns="False" DataKeyField="Qid" 
         onItemDataBound = "datagrid1_itemdatabound"
         Runat="server">
         <Columns>
         <asp:TemplateColumn HeaderStyle-Width="30" HeaderText="No.">
             <ItemTemplate>
               <asp:Literal ID="Literal1" Text='<%#
                 DataBinder.Eval(Container.DataItem, "qid") %>'
                 Runat="server" />
             </ItemTemplate>
           </asp:TemplateColumn>
           
     
           <asp:TemplateColumn HeaderStyle-Width="30" HeaderText="Question">
             <ItemTemplate>
               <asp:Literal ID="Literal2" Text='<%#
                 DataBinder.Eval(Container.DataItem, "qtext") %>'
                 Runat="server" />
             </ItemTemplate>
           </asp:TemplateColumn>
           
           <asp:TemplateColumn HeaderStyle-Width="100" HeaderText="Option">
             <ItemTemplate>
        <asp:RadioButtonList id="rblist" OnSelectedIndexChanged='<%#
      DataBinder.Eval(Container.DataItem, "anstext") %>'
      Runat="server" />
        
     </ItemTemplate>
     </asp:TemplateColumn>
          
     </Columns>
           </asp:DataGrid>
    sorry to post so much code,its just i have no idea where the problem is
    any suggestions gratefully received

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    You need that stack trace. It tells you which object reference was supposedly null.

    The problem is that you're calling a method somewhere on something you expect isn't null, but it is.
    object.methodcall()
    but object is null, so somewhere, something isn't getting creating. Could be anywhere; thus, the stack trace.

    For example:
    rbl = DirectCast(e.Item.FindControl("rblist"), RadioButtonList)

    You never check that it actually found that control. Then you use rbl as if it had.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Nikki,
    thanks for your reply-im very grateful for your info,however i have no idea how to figure out from that stack trace which object is null..none of it makes any sense to me,im still relatively new to asp.net

    this is what it says,anyone know what on earth this means

    [NullReferenceException: Object reference not set to an instance of an object.]
    System.Web.Compilation.TemplateControlCompiler.BuildPropertyBindingMethod(ControlBuilder builder) +1660
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +842
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +353
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +517
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +684
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +684
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +353
    System.Web.Compilation.TemplateControlCompiler.BuildSourceDataTreeFromBuilder(ControlBuilder builder, Boolean fInTemplate, PropertySetterEntry pse) +353
    System.Web.Compilation.TemplateControlCompiler.BuildMiscClassMembers() +52
    System.Web.Compilation.PageCompiler.BuildMiscClassMembers() +10
    System.Web.Compilation.BaseCompiler.BuildSourceDataTree() +1291
    System.Web.Compilation.BaseCompiler.GetCompiledType() +129
    System.Web.UI.PageParser.CompileIntoType() +60
    System.Web.UI.TemplateParser.GetParserCacheItemThroughCompilation() +126

    [HttpException (0x80004005): Object reference not set to an instance of an object.]
    System.Web.UI.TemplateParser.GetParserCacheItemInternal(Boolean fCreateIfNotFound) +691
    System.Web.UI.TemplateParser.GetParserCacheItemWithNewConfigPath() +125
    System.Web.UI.TemplateParser.GetParserCacheItem() +88
    System.Web.UI.TemplateControlParser.CompileAndGetParserCacheItem(String virtualPath, String inputFile, HttpContext context) +120
    System.Web.UI.TemplateControlParser.GetCompiledInstance(String virtualPath, String inputFile, HttpContext context) +36
    System.Web.UI.PageParser.GetCompiledPageInstanceInternal(String virtualPath, String inputFile, HttpContext context) +43
    System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String path) +44
    System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, String path, String pathTranslated, Boolean useAppConfig) +699
    System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() +95
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +173

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Okay, forgive me I usually use C# (and get better error messages with a line number and the code that errored out, which is set in the config file somewhere), but I believe this is wrong.

    Dim Conn As New OdbcConnection("ConnStr")
    You're supposed to pass that a connection, not a string.

    Dim Conn As New OdbcConnection(ConnStr)
    Note: no quotes.

    So, no connection, which would make this line throw a nullpointerexception:
    Conn.Open()
    or maybe this one...
    Dim Cmd As New OdbcCommand("SELECT anstext FROM Ans_Options", Conn)

    No conn, no object. No object, nullpointerexception...

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •