在Asp.Net中执行Connectins我遇到错误,在执行Codi时我得到空参考异常 [英] Excuting Connectins In Asp.Net I Am Getting Error, I Am Getting Null Reference Exception When Excuting The Codi

查看:63
本文介绍了在Asp.Net中执行Connectins我遇到错误,在执行Codi时我得到空参考异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void BindGridView()
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("Select eid,ename,age from emp", con);
        con.Open();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        TextBox txt_name = new TextBox();
        TextBox txt_age = new TextBox();

        TextBox txt_id = new TextBox();
        txt_id = (TextBox)GridView1.FooterRow.FindControl("txt_eid");
        txt_name = (TextBox)GridView1.FooterRow.FindControl("txt_ename");
        txt_age = (TextBox)GridView1.FooterRow.FindControl("txt_eage");

        SqlCommand cmd = new SqlCommand("insert into emp(eid,ename,age)values('" + txt_id.Text + "','" + txt_name.Text + "','" + txt_age.Text + "')");

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindGridView();
    }
        
}

推荐答案

首先,你应该使用参数化查询以避免 SQL注入攻击。



接下来,它提供 NullReference 异常,因为 TextBoxes 本身是 null 。这意味着它无法为您找到 TextBoxes
First of all, you should use Parameterized query to avoid SQL Injection attacks.

Next, it is giving NullReference Exception, because the TextBoxes are itself null. That means it failed to find the TextBoxes for you.


这篇关于在Asp.Net中执行Connectins我遇到错误,在执行Codi时我得到空参考异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆