Issue encountered when attempting to retrieve text input from text box within a C# (ASP.NET gridview) segment

Every time I attempt to retrieve a value from a textbox, I encounter this error (as shown in the image) and I am at a loss as to how to resolve it. My code is as follows:

protected void Button3_Click(object sender, EventArgs e)
    {
        _controller = new Controller();

        //Variables
        string appointment ="";

        foreach (GridViewRow row in GridView1.Rows)
        {
            appointment = ((TextBox)row.FindControl("txtEditTabel")).Text;               
        }

I am using a combination of javascript and ASP.NET, and the code appears like this; additional information about the JavaScript: EDITABLE LABEL

JAVASCRIPT (which changes a label to a textbox when clicked)

$(function () {
//Loop through all Labels with class 'editable'.
$(".editable").each(function () {
    //Reference the Label.
    var label = $(this);

    //Add a TextBox next to the Label.
    label.after("<input type = 'text' style = 'display:none' />");

    //Reference the TextBox.
    var textbox = $(this).next();

    //Set the name attribute of the TextBox.
    var id = this.id.split('_')[this.id.split('_').length - 1];
    //textbox[0].name = id.replace("Label","Textbox"); //tis dit hier van die id's
    textbox[0].name = "txtEditTabel";
    //Assign the value of Label to TextBox.
    textbox.val(label.html());

    //When Label is clicked, hide Label and show TextBox.
    label.click(function () {
        $(this).hide();
        $(this).next().show();
    });

    //When focus is lost from TextBox, hide TextBox and show Label.
    textbox.focusout(function () {
        $(this).hide();
        $(this).prev().html($(this).val());
        $(this).prev().show();
    });
});

});

ASP.NET

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1000px" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField HeaderText="IDAfspraken">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("IDAfspraken") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
          </asp:GridView>

I have double-checked my SQL code and believe it to be correct. Is there something I am overlooking? Any assistance would be greatly appreciated!

Error: https://i.sstatic.net/QoCoY.png

Answer №1

It appears that the

((TextBox)row.FindControl("txtEditTabel"))
value is returning null or undefined, resulting in a NullPointerException when attempting to dereference the .Text property.

Based on your specific C# code scenario, it seems more tailored to C# than JavaScript. My suggestion would be to try adding an id property to your control instead of a name. Referring to the specifications found here about the FindControl method, you should access the control by ID rather than name.

Edit: In case your input is within a form, consider accessing it using the input's name property with syntax like: Request.Form["txtName"]; instead of attempting to use FindControl.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Angular 2's *ngFor to conditionally wrap elements can be compared to organizing a layout with three columns in a Bootstrap row, then starting a

Currently I am facing a challenge with using *ngFor and it has me puzzled. My goal is to incorporate UIkit, but the same concept would apply to Bootstrap as well. <div *ngFor="let device of devices" > <div class="uk-child-width-expand@s uk-te ...

Generating fresh line with knockout effect

Currently in the process of developing a Single Page Application (SPA). Utilizing knockout and observable array to iterate through a json array. Encountering an issue where there are br tags present within the text, but when using data-bind="text: myVar" ...

Guide to connecting two geometric shapes together with the help of three.js

Is there a way to link two spheres together with a line? I'm looking for a solution that mimics two balls connected by a rope or something elastic. Does anyone have any examples to share? ...

Performing a series of MySql queries within a foreach statement in PHP

Within my PHP code, I am trying to extract the content id for each "listing" using a foreach statement: <?php $i = 2; $j = 0; foreach($listings AS $listing): $i = $i == 2 ? 1 : 2;?> ... <?php $db =& JFactory::getDBO(); $query = "SEL ...

Ways to store information using VueJS lifecycle hooks

I am currently working on setting a data property using the created lifecycle hook within my component. The issue I'm encountering is receiving a "TypeError: Cannot read property 'summary' of undefined" in the console as I run the code. This ...

I'm encountering problems when attempting to display the image/png response from an xmlHTTPRequest. Instead of the correct data, I

I have implemented the following code to integrate a captcha generating web service. The response data is successfully obtained, but when I attempt to display the result within a div element, the image appears as distorted text. var xmlHttp = new XMLHtt ...

Do you have any queries regarding JavaScript logical operators?

I'm struggling with understanding how the && and || operators work in my code. I created a small program to help myself, but it's just not clicking for me. The idea is that when you click a button, the startGame() function would be triggered. va ...

Retrieving data from a <span> element within a webpage created using ReactJS

I am completely new to the world of web design and development, so there may be some mistakes in my request. Essentially, I have a webpage that contains numerous span tags like the following example: These span tags are part of a significantly large DOM t ...

Add information to a database by incorporating dynamically created textboxes via c# programming language

I need help with generating dynamic textboxes using data from 2 tables: dynamic empdetail The empdetail table acts as the master table where dynamically generated columns are inserted. I am facing an issue where, after inserting the data in these dynam ...

What is the best way to filter out and combine one array from two arrays in lodash based on their unique properties?

Lodash v 4.17.15 Consider the scenario where two arrays are involved: var users = [{ id: 12, name: Adam },{ id: 14, name: Bob },{ id: 16, name: Charlie },{ id: 18, name: David } ] var jobs = [ ...

Generate a JSON (Jquery) structured table matrix outlining various roles and corresponding permissions such as read, write, delete, and write special

I would like to create a table matrix that displays roles and permissions (read, write, delete, write special) using jQuery with JSON data. The table should contain checkboxes for each permission type, and the checkboxes for read, write, delete, and write ...

Query a list of JSON objects as if they are a single entity using JSON.net

I have a variety of JSON strings stored in a list. While each one has different IDs and values, they share a common structure. For instance, JSON 1: { "sold":true, "Stock":{ "1":{ "id":"11", "amount":"1", }, "2 ...

Creating a new row in a Gridview using client-side scripting

I have a GridView called gvTopic which contains a TextBox and a Button. When the button is clicked, I want to display the newly added comment on the client side in another GridView called gvComment, while also storing the data in the database (possibly thr ...

angularjs identifies the ng-click and href attributes within my event

md-list md-list-item.md-2-line ng-repeat="document in ctrl.documents" div.md-list-item-text ng-click="ctrl.getDocument($event, document)" span ng-bind-html="(document.content | trustedHtml)" Hey there, I've encountered an issue with my md ...

Obtaining the automatically generated ID using the insert_id method

After a new user registers, I need to retrieve the auto-incremented ID from my 'users' table and use it in another query immediately. Specifically, I want to utilize the 'id' from the 'users' table as the 'user_id' i ...

Creating Angular Modal on ng-click

I'm facing a challenge with a seemingly simple task. Within an ng-repeat, I have a table of events. My goal is to enable users to click on the event name and view detailed information in a modal window. To achieve this, I have set up an ng-click="ev ...

Using database queries to create a relationship between two tables

I am currently working with a MySql Table that contains information about different categories: -------------------------------------------- Table 'category' -------------------------------------------- category_id name parent_id 1 ...

Exploring the World of jQuery Caching and Navigating Through Objects

I'm interested in learning more about jQuery caching and how it can enhance performance. Can you explain how to properly utilize this technique? From what I understand, when using a jQuery selector, you're essentially searching the DOM to create ...

The use of Electron require modules may lead to IntelliSense not functioning properly due to the discrepancy in working directories during runtime

Currently working on an electron-app using vs-code. I encountered some issues initially with requiring local files as modules but managed to resolve them. However, now the problem lies in the fact that I have lost intellisense for these local modules. Her ...

Tips for ensuring a module.export function completes its request before assigning the returned value to a variable in another JavaScript file

Within my Node.js application, I am utilizing two JS files. One of these files, latlng.js, contains the following code: async function getPlaceDetails(input) { var locationUrl = 'https://www.google.com'; request(locationUrl, (error, respo ...