Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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?
Posted
Updated 17-Dec-13 8:27am
v2
Comments
joginder-banger 17-Dec-13 14:27pm    
I think you can't provide a memory your Baglanti <pre lang="c#">Baglanti.State</pre>
you can used the public OleDbConnection Baglanti= new OleDbConnection();
XaxPRoy 17-Dec-13 14:38pm    
No still doesn't work but thanks.
Matt T Heffron 17-Dec-13 14:47pm    
Baglanti doesn't appear to be initialized anywhere, so it is null.
You must assign a value to Baglanti before it is referenced, as joginder-banger indicated, above.
Change to:
public static OleDbConnection Baglanti= new OleDbConnection();

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

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:
C#
    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
...




}
 
Share this answer
 
Comments
XaxPRoy 17-Dec-13 15:07pm    
Thanks you. Now its working no problem. Thanks again :)
OriginalGriff 17-Dec-13 15:37pm    
You're welcome!
Matt T Heffron 17-Dec-13 16:38pm    
This also means that the public static declaration of Baglanti is pointless and should be removed also, right?
It is now just a local variable within the methods that need it...
In fact a
using (var Baglanti = new OleDbConnection(baglantistringi))
{
...
}
wrapping the .Open and Adaptor.Fill calls would eliminate the need for the explicit Baglanti.Close().
OriginalGriff 17-Dec-13 16:57pm    
Yes - I just wasn't sure if he reused it later or I'd have suggested a using block to limit the scope as well.
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900