Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I Need Help For This Below And I Not Under Stand How To Solve This Executereader: Connection Property Has Not Been Initialized.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class frmlgn : Form
    {
        OleDbCommand cmd;
        
        public frmlgn()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            cmd = new OleDbCommand("select * from login where id='" + textBox1.Text + "'", Class1.con);
            OleDbDataReader dr;
            dr = cmd.ExecuteReader();
            dr.Read();
            if (dr.HasRows == true)
            {
                if (dr["password"].ToString() != textBox2.Text)
                {
                    MessageBox.Show("Password Not Matched!");
                    textBox2.Focus();
                }
                else
                {
                    MessageBox.Show("successfuly login");
                    this.Close();
                    MDIParent2 mdi=new MDIParent2();
                    
                                                       

                }
                }
            else
                MessageBox.Show("Invalid User id");
            dr.Close();
        }




I used this connection string in class



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    class Class1
    {
        public static OleDbConnection con;
        public static void Connection()
        {

           con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= e:\db1.mdb");
            con.Open();
        }
    }
}
Posted
Updated 22-Feb-14 16:28pm
v3
Comments
Garth J Lancaster 22-Feb-14 20:16pm    
Im a bit mystified about the relationship of Class1 to the rest of the code, particularly where you use it in your button1_Click() handler - especially since you don't
a) show where /how you instantiate Class1
b) in button1_Click() test that Class1.con is b1) not null, b2) open
PIEBALDconsult 22-Feb-14 20:33pm    
Class1.con is static, but the Connection method is never called.
Garth J Lancaster 22-Feb-14 20:49pm    
ok, ta - I thought C# classes had to have the static modifier to be such, but I defer to higher wisdom :-)

sir, please check as in the code above you have made the Connection function but you haven't used it before
cmd = new OleDbCommand("select * from login where id='" + textBox1.Text + "'", Class1.con);
 
Share this answer
 
There are a number of problems with your code. The main one is you never call Class1.Connection() , that's why Class1.con is null.

0) Class1 is not only poorly implemented, but doesn't offer your code anything, so you should get rid of it.
1) <SHOUTING> DO NOT STORE PLAIN-TEXT PASSWORDS !!! </SHOUTING>
2) The test of the password should occur on the database side.
3) Do not use concatenation to form the query; use a parameterized query -- it's very easy.
4) Don't test HasRows; it's needless and not all providers implement it -- just test the result of Read.
5) Don't Open the Connection until you need to use it.
6) Use Using statements.
 
Share this answer
 
Hello and thank you for all the help in solving the problem in the code has been modified and is now running successfully

As shown below


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    public partial class frmlgn : Form
    {
       OleDbCommand cmd = new OleDbCommand();
        OleDbConnection con = new OleDbConnection();
        OleDbDataReader dr;
  public frmlgn()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.ConnectionString = Class1.con;
                con.Open();
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from login where id='" + textBox1.Text + "'";
                OleDbDataReader dr;
                dr = cmd.ExecuteReader();
                dr.Read();
                if (dr.HasRows == true)
                {
                    if (dr["password"].ToString() != textBox2.Text)
                    {
                        MessageBox.Show("Password Not Matched!");
                        textBox2.Focus();
                    }
                    else
                    {
                        MessageBox.Show("successfuly login");

                       
                        
                        Form1 a = new Form1();
                        this.Hide();
                        a.Show();
                        


                    }
                }
                else
                    MessageBox.Show("Invalid User id");
                dr.Close();
            }


and connection string   Class1 

As shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    class Class1
{

        internal static string con = (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= d:\db1.mdb");
      
        }
    }
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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