我得到这个NullReferenceException [英] I get this NullReferenceException

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

问题描述

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Web.Security;

public partial class MasterPage : System.Web.UI.MasterPage
{
    string baglantistringi = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/KitapVT.accdb") + ";Persist Security info=false";
    public static OleDbConnection Baglanti;// access veri tabanına bağlantı nesnesi
    public static OleDbDataAdapter Adaptor;


    void sistemeGiris(string OturumKlasoru)
    {
        FormsAuthenticationTicket OturumBileti = new FormsAuthenticationTicket(1, txt_email.Text,
            DateTime.Now, DateTime.Now.AddMinutes(30), false, OturumKlasoru,
            FormsAuthentication.FormsCookiePath);
        string SifrelenmisBilet = FormsAuthentication.Encrypt(OturumBileti);
        HttpCookie cerez = new HttpCookie(FormsAuthentication.FormsCookieName, SifrelenmisBilet);
        if (OturumBileti.IsPersistent)
        {
            cerez.Expires = OturumBileti.Expiration;
        }
        Response.Cookies.Add(cerez);
        string GidilecekAdres = OturumKlasoru + "/Default.aspx";
        if (GidilecekAdres == null)
        {
            GidilecekAdres = "/Default.aspx";

        }
        Response.Redirect(GidilecekAdres, false);

    }
    protected void btnGiris_Click(object sender, EventArgs e)
    {
        string kullaniciTipi;
        string IP = Context.Request.ServerVariables["REMOTE_HOST"].ToString();
        DataTable dtGirisKontrol = new DataTable();
        string txt = "select * from kullanici where Sifre=@Sifre and Email=@Email";
        OleDbCommand cmd = new OleDbCommand(txt, Baglanti);
        cmd.Parameters.Add("@Sifre", OleDbType.Char, 100).Value = txt_sifre.Text;
        cmd.Parameters.Add("@Email", OleDbType.Char, 50).Value = txt_email.Text;
        Adaptor = new OleDbDataAdapter(cmd);
        if (Baglanti.State == ConnectionState.Closed)
        {// I get error right here(Null Reference Exception was unhandled by user code). 
            Baglanti.Open();

        }
        Adaptor.Fill(dtGirisKontrol);
        if (Baglanti.State == ConnectionState.Open)
        {
            Baglanti.Close();

        }
        if (dtGirisKontrol.Rows.Count == 0)
        {
            lblSonuc.Visible = true;
            lblSonuc.Text = "Kullanici adi veya şifreniz hatalı";
            return;
        }
        else
        {
            try
            {
                Session.Timeout = 30;
                Session["Email"] = txt_email.Text;
                Session["IDKullanici"] = dtGirisKontrol.Rows[0]["IDKullanici"].ToString();
                Session["KullaniciTipi"] = dtGirisKontrol.Rows[0]["KullaniciTipi"].ToString();
                kullaniciTipi = dtGirisKontrol.Rows[0]["KullaniciTipi"].ToString();
                Session["AdiSoyadi"] = dtGirisKontrol.Rows[0]["AdiSoyadi"].ToString();
                Session["IP"] = IP;

                if (kullaniciTipi == "ADMIN")
                {
                    sistemeGiris("uAdmin");

                }
                else if (kullaniciTipi == "UYE")
                {
                    sistemeGiris("uUYE");
                }
            }
            catch (Exception ex)
            {
                lblSonuc.Text = ex.Message.ToString();
            }
        }


        
    }
}





如何修复此代码?



How can fix this code?

推荐答案

您声明一个OdbcConnection变量( Baglanti ),但是你没有创建该类的实例:

You declare an OdbcConnection variable (Baglanti), but you don't create an instance of the class:
public static OleDbConnection Baglanti;// access veri tabanına bağlantı nesnesi



代码中对 Baglanti 的唯一其他引用是在中使用它时btnGiris_Click



你需要为变量分配一个实例,可能在方法的顶部:


The only other reference to Baglanti in your code is when you use it in btnGiris_Click

You need to assign an instance to the variable, probably at the top of the method:

    protected void btnGiris_Click(object sender, EventArgs e)
    {
        string kullaniciTipi;
        Baglanti = new OleDbConnection(baglantistringi);   //**** ADD THIS
...
        if (Baglanti.State == ConnectionState.Open)
        {
            Baglanti.Close();
            Baglanti = null;                               //**** ADD THIS
...









}





}


只需写完整个代码尝试catch块并在catch块中给出一些异常错误消息并逐个检查alll值你可以轻松找到它
just write you whole code in try catch block and give some exception error msg in catch block and one by one check alll values you can easily find it


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

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