Getting the selected value from DetailsVIew to TextBox
<p>How can I get the selected value from DetailsView to a textbox? So far I'm using this <code>TextBox.Text = DetailsView1.SelectedValue.String();</code> But returns an error: Object reference not set to an instance of an object. I could do it in OnDataBound and ItemInserted in formview with no problem but this time I want to get the selected value from detailsview to paste it in a textbox on Page_Load event. </p>

<p>Help would be greatly appreciated. Thanks in advance!</p>

<p>Attached the rest of the code behind same code from my previous post:</p>

<pre><code>using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibraryManagementSystemC4.User
{
    public partial class Reserving : System.Web.UI.Page
    {
        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["LibrarySystemConnectionString"].ConnectionString;
        }
        //string reservationid
        private void ExecuteInsert(string bookid, string EmployeeID, string reservedate)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());

            string sql = "INSERT INTO BookReservation (bookid, EmployeeID, reservedate) VALUES " + " (@bookid, @EmployeeID, @reservedate)";
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[3];

                //param[0] = new SqlParameter("@reeservationid", SqlDbType.Int, 50);
                param[0] = new SqlParameter("@bookid", SqlDbType.BigInt, 50);
                param[1] = new SqlParameter("@EmployeeID", SqlDbType.NVarChar, 50);
                param[2] = new SqlParameter("@reservedate", SqlDbType.DateTime, 10);

                //param[0].Value = reservationid;
                param[0].Value = bookid;
                param[1].Value = EmployeeID;
                param[2].Value = reservedate;

                for (int i = 0; i &lt; param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }

                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }

            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Insert error";
                msg += ex.Message;
                throw new Exception(msg);
            }

            finally
            {
                conn.Close();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (bookidTextBox != null)
            {
                ExecuteInsert(bookidTextBox.Text, EmployeeIDTextBox.Text, reservedateTextBox.Value);

                ClearControls(Page);
            }

            else
            {
                Response.Write("Please input ISBN");
                bookidTextBox.Focus();
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            {

                bookidTextBox.Text = DetailsView1.SelectedValue.ToString();
                EmployeeIDTextBox.Text = HttpContext.Current.User.Identity.Name.ToString();
            }
        }

        public static void ClearControls(Control Parent)
        {
            if(Parent is TextBox)
            {
                (Parent as TextBox).Text = string.Empty;
            }

            else
            {
                foreach (Control c in Parent.Controls)
                ClearControls(c);
            }
        }
    }
}
</code></pre>
