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