Reputation: 1112
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);
}
}
}
}
Upvotes: 0
Views: 1547
Reputation: 21881
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();
}
Upvotes: 2