Ken Carter Ken Carter - 10 months ago 64
ASP.NET (C#) Question

TextBox Insertion not working

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">
<asp:TextBox Text='<%# Bind("CAssetID") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="CAssetIDTextBox" /><br />
<asp:TextBox Text='<%# Bind("Reason") %>' runat="server" ID="ReasonTextBox" Width="500" Height="500" TextMode="MultiLine" OnTextChanged="ReasonTextBox_TextChanged" /><br />
<asp:TextBox Text='<%# Bind("Source") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="SourceTextBox" /><br />
<asp:TextBox Text='<%# Bind("RejBy") %>' runat="server" BackColor="LightGray" ReadOnly="true" ID="RejByTextBox" /><br />
<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" />
<asp:TextBox Text='<%# Bind("CAssetID") %>' runat="server" ID="CAssetIDTextBox" /><br />
<asp:TextBox Text='<%# Bind("Reason") %>' runat="server" ID="ReasonTextBox" /><br />
<asp:TextBox Text='<%# Bind("Source") %>' runat="server" ID="SourceTextBox" /><br />
<asp:TextBox Text='<%# Bind("RejBy") %>' runat="server" ID="RejByTextBox" /><br />
<asp:TextBox Text='<%# Bind("RejDT") %>' runat="server" ID="RejDTTextBox" /><br />
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
<asp:Label Text='<%# Bind("CAssetID") %>' runat="server" ID="CAssetIDLabel" /><br />
<asp:Label Text='<%# Bind("Reason") %>' runat="server" ID="ReasonLabel" /><br />
<asp:Label Text='<%# Bind("Source") %>' runat="server" ID="SourceLabel" /><br />
<asp:Label Text='<%# Bind("RejBy") %>' runat="server" ID="RejByLabel" /><br />
<asp:Label Text='<%# Bind("RejDT") %>' runat="server" ID="RejDTLabel" /><br />
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" />
<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)">
<asp:SessionParameter SessionField="ChangeRecord" Name="CAssetID"></asp:SessionParameter>
<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>
<br />
<asp:Button runat="server" ID="RejectResetBtn" Visible="false" OnClick="RejectResetBtn_Click" Text="*** After Entering your Comment Click this bar!! ****" ForeColor="White" BackColor="Red" />

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!

Answer Source

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 Store 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;

            Updreader = Updcmd.ExecuteReader();