samedi 11 avril 2015

Visual Studio OleDB Exception unhandled || I am writing a code to validate my form from access database in Visual Studio.


Code:
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 Form1 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form1()
{

InitializeComponent();
connection.ConnectionString=@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bin\Debug\Login.accdb";


/* I think this connection string is weird. It is giving invalid path name when I run the code */ }



private void label1_Click(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
connection.Open();
if(String.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("The username cannot be blank! Please Enter again","Invalid User Name",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
textBox1.Select();
}

if (String.IsNullOrWhiteSpace(textBox2.Text))
{
MessageBox.Show("The password cannot be blank! Please Enter again", "Invalid Password", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
textBox2.Select();
}
else if (textBox2.TextLength < 8)
{
MessageBox.Show("The password cannot be less than 8 characters. Please Enter again", "Invalid Password", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
textBox2.Select();
}

else
{
DialogResult abc = MessageBox.Show("Hello! Login Successful", "Welcome box", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (abc == DialogResult.Cancel)
{
this.Close();

}
else
{
Form3 frm = new Form3(textBox1.Text);
frm.Show();
}
}
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from LoginData where Username='" + textBox1.Text + "'and Password='" + textBox2.Text + "'";
OleDbDataReader reader = command.ExecuteReader();
int count = 0;
while (reader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Login Successfull!");
}
else
MessageBox.Show("Invalid username and password");
connection.Close();
}


private void button1_Click(object sender, EventArgs e)
{
ForgotPwd frm = new ForgotPwd();
frm.Show();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SignUp frm = new SignUp();
frm.Show();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
connection.Open(); // the exception is unhandled here
connection.Close();
}
catch (Exception ex)
{
}

}

}
}


/* the code basically validates data entered from the database. The database is stored in the debug folder of the project of visual studio*/


Aucun commentaire:

Enregistrer un commentaire