如何修复这个特殊的NullReferenceException? [英] How to Fix This particular NullReferenceException?

查看:70
本文介绍了如何修复这个特殊的NullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebForm,它有一个UserControl,我想点击UserControl(它是一个GridView),即使是在UserControl外面的webform中的按钮。 UserControl中有一个方法来绑定我已声明为Public的GridView。



WebForm代码:



I have a WebForm which has a UserControl and i want to Bind the UserControl(It is a GridView) on click even of a button which in the webform outside the UserControl. There is a method in UserControl to Bind the GridView which i have declared Public.

WebForm Code:

protected void Unnamed1_Click(object sender, EventArgs e)
    {
        if (!object.Equals(Session["UserId"], null))
        {
            string postScrap = "Insert INTO Scrap (FromId,ToId,Message) VALUES('" + Session["UserId"].ToString() + "','" + Request.QueryString["Id"].ToString() + "','" + TextBoxScrap.Text + "')";
            dbClass.ConnectDataBaseToInsert(postScrap);           
            Controls_GetUserScraps obj = new Controls_GetUserScraps();
            obj.GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
        }





UserControl代码:





UserControl Code:

public void GetUserScraps(int Id)
    {
        string getUserScraps = "SELECT u.Id as UserId,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId FROM [tbl_user] as u, Scrap as s WHERE u.Id=s.FromId AND s.ToId='" + Id + "' order by senddate desc";
        dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
        if (dt.Rows.Count > 0)
        {
            GridViewUserScraps.DataSource = dt;   //this is where i am getting the exception. eventhough  dt has values.
            GridViewUserScraps.DataBind();
        }
    }

推荐答案

您还没有为GridViewUserScraps分配GridView - 它是一个空值,所以当您尝试访问它的DataSource属性时,您会收到异常。查看代码中的其他位置,找到声明的位置 - 然后找出您的意图! :笑:



请不要这样做:在尝试使用它们之前检查你的查询字符串,不要连接字符串来构建一个SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。这在处理基于Web的代码时特别重要,当您的数据库可以从世界的另一端销毁时。
You have not assigned a GridView to GridViewUserScraps - it is a null value, so when you try to access it''s DataSource property, you get an exception. Look elsewhere in your code, and find where it is declared - then work out what you meant to do with it! :laugh:

And please, don''t do things like that: Check your query strings before you try to use them, and do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. This is particularly important when dealing with web based code, when your database could be destroyed from the other side of the world.


我自己通过不创建UserControl的对象来修复它。并用
取代它
GetUserScraps1.GetUserScraps(int.Parse(Request.QueryString [Id]。ToString()));
Well i fixed it myself by not creating an object of UserControl. And Replacing that with
GetUserScraps1.GetUserScraps(int.Parse( Request.QueryString["Id"].ToString()));


这篇关于如何修复这个特殊的NullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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