Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:31:48+00:00 2026-05-22T23:31:48+00:00

How can I get the selected value from DetailsView to a textbox? So far

  • 0

How can I get the selected value from DetailsView to a textbox? So far I’m using this TextBox.Text = DetailsView1.SelectedValue.String(); 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.

Help would be greatly appreciated. Thanks in advance!

Attached the rest of the code behind same code from my previous post:

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 < 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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:31:49+00:00Added an answer on May 22, 2026 at 11:31 pm

    DetailsView1.SelectedValue.String() will throw a null reference exception if SelectedValue is null, i.e. there is no selected value, which in the context of a DetailsView i think means it contains no data. You want to do:

    if (DetailsView1.SelectedValue != null)
    {
        MyTextbox.Text = DetailsView1.SelectedValue.ToSTring();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently facing a problem. How to get the latest selected value from
I'm pulling my hair out trying to get a jQuery script working that will
I am writing a GTK+ application in C (though the principle here is widely
I got the next situation: NSOutlineView <-> NSTreeController <-> MyClass I bind the NSTreeController
I am just trying to make a timer. I would like to use UIDatePickerModeCountDownTimer
SOLUTION BELOW I've been looking all over the net to find a solution for
I worked out an XSL template that rewrites all hyperlinks on an HTML page,
ASP.NET MVC Model Binding is still new to me and I'm trying to understand
I'm making an app that has a file name field, an upload file field
Okay I hear that thing all the time on internet when reading an article

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.