zeetec 0 Newbie Poster

Hi,

I have a datalist which contains a linkbutton. The LinkButton is populated with templatename which is a field from my Database table.

My LinkButton has an onclick event which attempts to populate the clicked templatename's value into my textbox (called txttest).

Here is my front-end code:-

<form id="form1" runat="server">
    <div>
    <asp:TextBox ID="first" runat="server" Text="text"></asp:TextBox>


<asp:Label ID="lbltest" runat="server"></asp:Label>  

    <asp:Button ID="btnShow" runat="server" Text="Button" onclick="btnShow_Click" />



    
    <asp:DataList ID="DataGridShowRecord" runat="server">
    
    <HeaderTemplate>
    <table border ="1"> <td bgcolor="wheat" width ="80" align=center >Template Title</td></table> </HeaderTemplate>
    <ItemTemplate>
    <table border="1"><td width="80" align=center ><asp:LinkButton id="mylbl" runat="server" OnClick="mylbl_click" CommandName="test" text='<%# DataBinder.Eval(Container.DataItem, "TemplateTitle") %>'>
    </asp:LinkButton></td></table>
        </ItemTemplate>
        
    </asp:DataList>
    
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        
        <asp:TextBox ID="txttest" runat="server"></asp:TextBox>  
    </div>
    </form>

Here is the back end code:-

public partial class WebForm4 : System.Web.UI.Page
    {

        SqlDataAdapter da;

        DataSet ds = new DataSet();

        DataView dv;

        SqlCommand cmd = new SqlCommand();

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        public DataSet Save(string fname)
        {
            DataSet dsEmpInfo = new DataSet();

            SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["wsDBConn"].ToString());
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "SELECT * FROM Templates where TemplateTitle = @Email";
            cmd.Parameters.AddWithValue("@Email", (first.Text.Trim()));

            cnn.Open();

            DataGridShowRecord.DataBind();


            da = new SqlDataAdapter(cmd);

            da.Fill(ds);

            cmd.ExecuteNonQuery();



            //Here Storing the table in DataView

            dv = new DataView(ds.Tables[0]);



            //Here Sort Property we can show the record According to Dept No Either Ascending or Descnding order


            DataGridShowRecord.DataSource = dv;

            DataGridShowRecord.DataBind();




            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
            try
            {
                sqlDataAdapter.Fill(dsEmpInfo, "EmpInfo");
            }
            catch { }


            return dsEmpInfo;

        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            

            DataSet dsEmpInfo = Save(first.Text.ToString());

            if (dsEmpInfo.Tables[0].Rows.Count == 0)
            {
                //Here im displaying my panels based on the results. If no records are found then i present them with one panel to enter themselves.   
                Response.Redirect("badpassword.aspx");
            }
            else
            {
                Label1.Text = "Shown below is your record";

               // Enter your code here   
            }
        }


        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {    
            if (e.CommandName == "test")
            {
                txttest.Text = "this is some text";
            }
        
        }

        
        protected void mylbl_click(object sender, EventArgs e)
        {
            

            LinkButton l = (DataGridShowRecord.SelectedItem.FindControl("mylbl") as LinkButton);

            txttest.Text = l.Text.ToString(); 

        }


    }

When I click on my LinkButton I get an error:-

Object reference not set to an instance of an object. It's pointing to this line:-

LinkButton l = (DataGridShowRecord.SelectedItem.FindControl("mylbl") as LinkButton);

Thanks.