I am at a total loss here! Perhaps someone will see something that I'm missing. I'm using this same code else where and it works just fine. The purpose is to seed an Edit mode FromView with data that I want to supply for the user. In this case I'm not allowing user to change the information that I'm supplying.
What I get when I try to run this is:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference
not set to an instance of an object.
My code behind checks for null reference so I'm not sure what I can do to correct this. And this works elsewhere in my code (variables different of course).
Here is a fragment from the aspx page and then the C# code behind that go with it.
<asp:Panel runat="server" ID="RejectDetailPnl" Visible="false">
<asp:Button ID="RejectBtn" runat="server" OnClick="RejectBtn_Click" BackColor="#ff0000" ForeColor="#ffffff" Text="Click To Reject" />
<asp:FormView ID="RejSubmittFV" runat="server" OnInit="RejSubmittFV_Init" DefaultMode="Edit" DataSourceID="RejSubmitDS">
<EditItemTemplate>
CAssetID:
<asp:TextBox Text='<%# Bind("CAssetID") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="CAssetIDTextBox" /><br />
Reason:
<asp:TextBox Text='<%# Bind("Reason") %>' runat="server" ID="ReasonTextBox" Width="500" Height="500" TextMode="MultiLine" OnTextChanged="ReasonTextBox_TextChanged" /><br />
Source:
<asp:TextBox Text='<%# Bind("Source") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="SourceTextBox" /><br />
RejBy:
<asp:TextBox Text='<%# Bind("RejBy") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="RejByTextBox" /><br />
RejDT:
<asp:TextBox Text='<%# Bind("RejDT") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="RejDTTextBox" /><br />
<asp:LinkButton runat="server" Text="Update" o CommandName="Update" ID="UpdateButton" CausesValidation="True" />
</EditItemTemplate>
<InsertItemTemplate>
CAssetID:
<asp:TextBox Text='<%# Bind("CAssetID") %>' runat="server" ID="CAssetIDTextBox" /><br />
Reason:
<asp:TextBox Text='<%# Bind("Reason") %>' runat="server" ID="ReasonTextBox" /><br />
Source:
<asp:TextBox Text='<%# Bind("Source") %>' runat="server" ID="SourceTextBox" /><br />
RejBy:
<asp:TextBox Text='<%# Bind("RejBy") %>' runat="server" ID="RejByTextBox" /><br />
RejDT:
<asp:TextBox Text='<%# Bind("RejDT") %>' runat="server" ID="RejDTTextBox" /><br />
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
</InsertItemTemplate>
<ItemTemplate>
CAssetID:
<asp:Label Text='<%# Bind("CAssetID") %>' runat="server" ID="CAssetIDLabel" /><br />
Reason:
<asp:Label Text='<%# Bind("Reason") %>' runat="server" ID="ReasonLabel" /><br />
Source:
<asp:Label Text='<%# Bind("Source") %>' runat="server" ID="SourceLabel" /><br />
RejBy:
<asp:Label Text='<%# Bind("RejBy") %>' runat="server" ID="RejByLabel" /><br />
RejDT:
<asp:Label Text='<%# Bind("RejDT") %>' runat="server" ID="RejDTLabel" /><br />
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource runat="server" ID="RejSubmitDS" ConnectionString="Data Source=gbaptccsr01;Initial Catalog=CInTracDB;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT CAssetID, Reason, Source, RejBy, RejDT FROM RejFailCtrl WHERE (CAssetID = #CAssetID)" UpdateCommand="UPDATE RejFailCtrl SET Reason = #Reason, Source = #Source, RejBy = #RejBy, RejDT = #RejDT WHERE (CAssetID = #CAssetID)">
<SelectParameters>
<asp:SessionParameter SessionField="ChangeRecord" Name="CAssetID"></asp:SessionParameter>
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Reason"></asp:Parameter>
<asp:Parameter Name="Source"></asp:Parameter>
<asp:Parameter Name="RejBy"></asp:Parameter>
<asp:Parameter Name="RejDT"></asp:Parameter>
<asp:Parameter Name="CAssetID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:Button runat="server" ID="RejectResetBtn" Visible="false" OnClick="RejectResetBtn_Click" Text="*** After Entering your Comment Click this bar!! ****" ForeColor="White" BackColor="Red" />
</asp:Panel>
The C# code behind and the choke point is:
TextBox uname = (TextBox)RejSubmittFV.Row.FindControl("RejByTextBox");
There are two other points I have tested by commenting out sections that also fail in this code block. Basically every time I repeat this code sequence it renders this error. Here is the full code block for your reference:
protected void RejectBtn_Click(object sender, EventArgs e)
{
TextBox uname = (TextBox)RejSubmittFV.Row.FindControl("RejByTextBox");
if (uname != null)
uname.Text = Session["RegUser"].ToString();
TextBox usource = (TextBox)RejSubmittFV.Row.FindControl("SourceTextBox");
if (usource != null)
usource.Text = "CInTrac";
TextBox udate = (TextBox)RejSubmittFV.Row.FindControl("RejDTTextBox");
if (udate != null)
udate.Text = DateTime.Now.ToString("MM/dd/yyyy");
}
Like I mentioned this procedure is working perfectly elsewhere so I don't have a clue why it isn't here.
Any ideas would be greatly appreciate!
I'm seeing two items that could be null which you are expecting to exist:
RejSubmittFV and the .Row property. They're not assigned, in the code you've provided, so we cannot be sure that you're initializing that object.
Session["RegUser"] which isn't tested for null before you're trying to call .ToString() on it.
If you're able to step through with a debugger, these are the kinds of operations to look for. You can either add a watch to these values or you can simply hover your mouse and see if each reference is null.
I never came up with a real answer for this issue! What I ended up doing to work around the problem was to eliminate the ForrView and simple sit the 5 data fields in a panel that I controlled the visibility with by ID which eliminated the issue of having to navigate the FindControl. This lead to abandoning the SqlDataSource and doing my update via a stored procedure and 100% with code behind and it was a LOT cleaner as a result.
// Update the Control file
SqlConnection UpdRejConnection = new SqlConnection("Data Source=GBAPTCCSR01;Initial Catalog=CInTracDB;Integrated Security=True");
SqlCommand Updcmd = new SqlCommand();
SqlDataReader Updreader;
Updcmd.Parameters.AddWithValue("#CAssetID", Session["ChangeRecord"]);
Updcmd.Parameters.AddWithValue("#Reason", ReasonTextBox.Text.ToString());
Updcmd.Parameters.AddWithValue("#Rejby", RejByTextBox.Text.ToString());
Updcmd.Parameters.AddWithValue("#Source", SourceTextBox.Text.ToString());
Updcmd.Parameters.AddWithValue("#RejDT", RejDTTextBox.Text.ToString());
Updcmd.CommandText = "usp_UpdRejCtrl";
Updcmd.CommandType = CommandType.StoredProcedure;
Updcmd.Connection = UpdRejConnection;
UpdRejConnection.Open();
Updreader = Updcmd.ExecuteReader();
UpdRejConnection.Close();
Related
I have a problem with placing RequiredFieldValidator into a DataList, and some assistance would be greatly appreciated.
Problem description:
When I place the <asp:RequiredFieldValidator>inside the <ItemTemplate> and run the page I get the following error page:
When I place it outside of it, the page works with no error, but obviously it cannot identify witch <asp:TextBox> does not have content in it, before it is posted back. And I would like to make where it is able to tell if a particular text box in the data list does not have text entered.
Here is the markup: (the Image src attribute is not implemented yet)
<asp:DataList ID="imageUploadRoster" runat="server"
DataSourceID="ImageUploadRosterDataSource" RepeatDirection="Horizontal" RepeatColumns="5" HorizontalAlign="Left">
<HeaderTemplate>
<h3>Set Image Names</h3>
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="180px" Width="180px" />
<ItemTemplate>
<div class="imageSetNameDiv">
<asp:HiddenField ID="ImageId" runat="server" Value='<%# Eval("ImageId") %>' />
<asp:HiddenField ID="ImageMimeTypeLabel" runat="server" Value='<%# Eval("ImageMimeType") %>' />
<asp:Image ID="ImageThumbnailLabel" runat="server" Src='<%# Eval("ImageThumbnail") %>' Width="120px" Height="120px" />
<br />
<asp:RequiredFieldValidator ID="imageNameRequired" runat="server"
ControlToValidate="ImageName" ErrorMessage="RequiredFieldValidator"
ValidationGroup="imageUploadValid">
</asp:RequiredFieldValidator>
<asp:TextBox ID="ImageName" runat="server" Text='<%# Eval("ImageName") %>' />
<br />
<hr />
<asp:Button ID="removeImage" runat="server" Text="Remove" CommandName="delete" CommandArgument='<%# Eval("ImageId") %>' />
</div>
</ItemTemplate>
</asp:DataList>
Thank you in advance for any assistance.
Peter
This error happens when ASP.NET encounters two controls with the same ID on the page. I guess for some reason in your case validators for each item get the same ID. Setting the following property:
ClientIDMode="Predictable"
for validator should fix it. This mode makes sure control in databound context gets correct ID.
I have a FormView where a row is "UserDate". My Formview can insert,edit, and delete records, but the UserDate field is supposed to be retrieved as the current date and time.
default.aspx (HTML Markup)
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# DateTime.Now %>' />
When I run it, and Select "Insert" on the formsview, the date automatically populates. However, when I add the record, it doesn't show in the gridview while every other column value does.
All other columns are user input like this though:
<asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
The gridview resembles my sql database table po_table.
How do I implement the date to the gridview / database table?
Edit:
I'm assuming I do it here in my cs file?
protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
}
So far I tried:
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# DateTime.Now %>' />
which gives the date in the formview but doesn't transfer to the gridview when adding.
I've tried:
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Eval(DateTime.Now) %>' />
which gives an error.
My files: (Class file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PO_1_5_15
{
public partial class _Webform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
}
HTML Markup
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test_DatabaseConnectionString %>" SelectCommand="SELECT [PO_ID], [PO_Title], [Date_Received], [Date_Completed], [Username], [PO_Note], [UserDate] FROM [PO_Table]"></asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="PO_AutoID" DataSourceID="SqlDataSource2" EnableModelValidation="True">
<EditItemTemplate>
PO_AutoID:
<asp:Label ID="PO_AutoIDLabel1" runat="server" Text='<%# Eval("PO_AutoID") %>' />
<br />
PO_ID:
<asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
<br />
PO_Title:
<asp:TextBox ID="PO_TitleTextBox" runat="server" Text='<%# Bind("PO_Title") %>' />
<br />
Date_Received:
<asp:TextBox ID="Date_ReceivedTextBox" runat="server" Text='<%# Bind("Date_Received") %>' />
<br />
Date_Completed:
<asp:TextBox ID="Date_CompletedTextBox" runat="server" Text='<%# Bind("Date_Completed") %>' />
<br />
Username:
<asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />
<br />
UserDate:
<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Eval(DateTime.Now) %>' />
<br />
PO_Note:
<asp:TextBox ID="PO_NoteTextBox" runat="server" Text='<%# Bind("PO_Note") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
If you want to have the same DateTime from the FormView to be displayed on the GridView, you will need to save that DateTime off somewhere. If you were to just simply use DateTime.Now in the GridView, it would always just display the current DateTime, not the time entered from the FormView.
My recommendation would be to have/add a column in your database table, as like a creation or update date. Then when you insert/update your database from the FormView, include the new DateTime in those SQL calls. Then just bind it like you are for your other columns in your GridView.
In the below code i have a grid view in which i have a textbox i am passing 0 and 1 to check box if it is 1 the checkbox to be checked.I tried but it is not working.Pls help me to solve the issue.
<asp:TemplateColumn HeaderText="Sales Price Ref" ItemStyle-Width="200px" HeaderStyle-VerticalAlign="Top">
<HeaderTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text="Sales Price"></asp:Label>
<br /><br />
<asp:TextBox runat="server" ID="txtSalesPriceRef" AutoPostBack="true" BorderStyle="Solid" BorderColor="#6495ED" BackColor="#B0C4DE" Height="20px" Width="90px" OnTextChanged="txtItem_TextChanged"></asp:TextBox>
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%#Eval("SalesPriceRef")=="1" ? true:false %>' />
<%-- <asp:TextBox ID="txtSalesPriceRef" Width="90px" runat="server" Text='<%#Eval("SalesPriceRef") %>'></asp:TextBox>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text='<%# Convert.ToString(Eval("SalesPriceRef"))== "1" ? "True" :"False" %>' > </asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
YES you can achieve it by binding it to the checkbox
as
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%# Bind("SalesPriceRef")%>' Enabled="false" />
if you allow to change checkbox value then make enable=true
I'm trying to bind the selected value from the date picker to an asp textbox but I have this Error: 'this._targetEl.value.length' is null or not an object.
Here's the code:
<InsertItemTemplate>
Book Title:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee PIN:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<asp:TextBox ID="dateborrowedTextBox" runat="server" Text='<%# Bind("dateborrowed") %>' />
<%--<input type="text" name="dateborrowedTextBox" readonly="readonly" id="dateborrowedTextBox">--%>
Date Picker
<br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
It was working when I
tried using <input type="text" name="dateborrowed" readonly="readonly" id="dateborrowedTextBox"> but when I tried using the asp:TextBox I cant pass the selected value from date picker to the textbox. So where did I go wrong? Is there a way to program the date picker link to call the popup calendar? (its in Java btw)
Help would be much appreciated!
Thanks in advance.
Nested controls have their ID/name attributes overwritten with an absolute unique id/name when rendered. When your javascript is attempting to reference the TextBox its name won't actually be 'dateborrowedTextBox' it will be something like '...$ctl00$dateborrowedTextBox'.
If your javascript is finding the control by name, this might clear up the issue:
Date Picker
Otherwise try this if its finding the control by id:
Date Picker
Found the solution. I think it can only read asp.net syntax not classic asp.
<asp:TextBox ID="reservedateTextBox" runat="server" Text='<%# Bind("reservedate") %>' />
<%--Date Picker--%>
Date Picker
I'm having some problem binding the value of a date picker to a textbox in formview asp.net. I've tried to a put a date picker in ASP.NET and JavaScript which calls on a class file in calendar.css so far it can display the date but if I tried to insert it to a record it always return null. So how can I bind it so it can add the date value to the record?
Help would be much appreciated.
Thanks in advance ;)
Here's a sample of my code. I want to bind the 'input text' to the 'dateborrowedTextBox'
<InsertItemTemplate>
Book Title:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee PIN:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<%--<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />--%>
<input type="text" name="dateborrowedTextBox" readonly="readonly" id="dateborrowedTextBox">
<a href="#" onclick="cdp1.showCalendar(this, 'dateborrowedTextBox'); return false;">Date Picker
</a>
<br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
The client-side IDs of the ASP.NET controls are not going to be translated as cleanly as you are hoping.
You need to do one of two things, both of which can be found here.
Date Picker
Or, you could add a Link control and set the actions that way (the link I gave you does it with an image control). It's up to you.
Just found my answer. ASP.NET cant read Javascript normally so CT100 and some dollar sign will do the trick ;)
Date Picker