请帮助用户代码取消NullReferenceException [英] Please help NullReferenceException was unhelded by user code

查看:106
本文介绍了请帮助用户代码取消NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#代码出错了。基本上我想搜索并保存数据库中的条目。

我能够搜索但是当我点击保存按钮时它会给我一个错误。



I'm getting error in my C# code. Basically I want to search and save the entries in database.
I'm able to search but when I click on Save button it gives me an error.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class search_by_category : System.Web.UI.Page
{

    SqlConnection con = null;
    SqlDataAdapter adp = null;
    SqlCommand cmd = null;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            category();
            DropDownList2.Items.Insert(0, new ListItem("select", "0"));
        }
    }

    public void category()
    {
        con = new SqlConnection("Data Source=SAINATH-PC;Initial Catalog=C:\\USERS\\SAINATH\\Placement\\APP_DATA\\Placement.MDF;");
        con.Open();

        string query = "select * from Category";
        adp = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);

        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataTextField = "category_name";
        DropDownList1.DataValueField = "category_id";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("select", "0"));

        con.Close();
    }

    public void area()
    {
        con = new SqlConnection("Data Source=SAINATH-PC;Initial Catalog=C:\\USERS\\SAINATH\\Placement\\APP_DATA\\Placement.MDF;");
        con.Open();

        string query = "select * from Area where Area.category_id=" + DropDownList1.SelectedValue;
        adp = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);

        DropDownList2.DataSource = ds.Tables[0];
        DropDownList2.DataTextField = "area_name";
        DropDownList2.DataValueField = "area_id";
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, new ListItem("select", "0"));

        con.Close();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=SAINATH-PC;Initial Catalog=C:\\USERS\\SAINATH\\Placement\\APP_DATA\\Placement.MDF;");
        con.Open();

        string query = "select * from job_post where area_id=" + DropDownList2.SelectedValue;

        adp = new SqlDataAdapter(query, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=SAINATH-PC;Initial Catalog=C:\\USERS\\SAINATH\\Placement\\APP_DATA\\Placement.MDF;");
         con.Open();

         int cid = 0;
         cid = Convert.ToInt32(Session["Cid"].ToString());

         int rws = 0;
         rws = Convert.ToInt32(GridView1.Rows.Count.ToString());

        for (int i = 0; i < rws ; i++)
         {
             CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox3");

             if (chk.Checked == true)
             {
                 String a = GridView1.Rows[i].Cells[2].Text;
                 string query = "insert into Saved_jobs (candidate_id,jobpost_id) values (" + cid + "," + a + ")";
                 cmd = new SqlCommand(query, con);
                 cmd.ExecuteNonQuery();
             }
         }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        area();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

推荐答案

可能没有控件可用于名为CheckBox3的C#代码 - 这意味着FindControl将返回null,当您尝试使用它时会出现异常。



请注意 - 这是一些相当糟糕的代码:为什么要将整数转换为字符串才能将其转换回整数?
Probably, there is not control available to you C# code called "CheckBox3" - which means that FindControl will return null, and you will get an exception when you try to use it.

Mind you - that is some pretty horrible code: why are you converting an integer to a string in order to convert it back to an integer again?


另请参阅我最近的回答:对象引用未设置为对象的实例。 C#aspx页面错误 [ ^ ]。



-SA
See also my recent answer: Object reference not set to an instance of an object. Error IN C# aspx page[^].

—SA

这篇关于请帮助用户代码取消NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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