This is a migrated thread and some comments may be shown as answers.

[Solved] Object reference not set to an instance of an object.

1 Answer 171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 27 Oct 2009, 01:03 AM
hi

I am having this error:
Object reference not set to an instance of an object.

This is my aspx code:
  <asp:DataList ID="DataList1" runat="server"    
                        DataSourceID="sdsRoomSelect" RepeatColumns="7"   
                        RepeatDirection="Horizontal">  
                        <ItemTemplate> 
                           <br /> 
                            <p/> 
                            &nbsp;<asp:Button ID="Button1" CommandName="select" runat="server"  Text='<%# Eval("Room_No") %>' Width="50px" Height="50px" /> &nbsp;  
                            <br /> 
                            <br /> 
                        </ItemTemplate> 
                    </asp:DataList> 
 This is my Code-Behind:
Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.ItemCommand  
        If e.CommandName = "select" Then  
            Dim ddl As Button = TryCast(DataList1.FindControl("Button1"), Button)  
            Dim strValue As String = ddl.Text  
            Response.Cookies("roomno").Value = strValue 
          End If  
    End Sub 
 Room No is not a primary key. How do I get the value of the room no when click?. Thanks

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Oct 2009, 05:38 AM
Hello,

Give a try with following code in order to retrieve the text of clicked button in DataList.

VB:
 
Protected Sub DataList1_ItemCommand(ByVal source As ObjectByVal e As DataListCommandEventArgs) 
    Dim item As DataListItem = DirectCast(e.Item, DataListItem) 
    If e.CommandName = "select" Then 
        Dim ddl As Button = TryCast(item.FindControl("Button1"), Button) 
        Dim strValue As String = ddl.Text 
        Response.Cookies("roomno").Value = strValue; 
    End If 
End Sub 

-Shinu.
Tags
Grid
Asked by
L
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or