CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   ASP.NET (http://www.codingforums.com/forumdisplay.php?f=40)
-   -   trying to bind datagrid column text to radiobuttonlist (http://www.codingforums.com/showthread.php?t=66340)

speedy05 08-19-2005 11:56 PM

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:

Quote:

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

nikkiH 08-24-2005 02:34 PM

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.

speedy05 08-26-2005 10:37 PM

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 :o

Quote:

[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

nikkiH 08-27-2005 02:29 AM

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...


All times are GMT +1. The time now is 01:11 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.