Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MS-Access
Hello frndz i m new to asp .net
 
i am creating an login page in asp .net ,c# with MS-ACCESS database
 
i am having username,empcode,password texboxes
 
divisioncode,sba,and unitmaster dropdown list, data for this dropdown list comes from three different tables
 
i have written code but it does not work i pasting my c# code below plz help me frndz
 
and i also want to use session concept and want to display userid,unitcode ,divisioncode ,login date ,and login year as label in next page
 

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        populatedd1();
        populatedd2();
        populatedd3();
    }
    public void populatedd1()
    {
        dddivision.Items.Add(new ListItem("-Select-", ""));
        dddivision.AppendDataBoundItems = true;
 
        OleDbConnection con1 = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
        string query = "SELECT division_id FROM division_master";
        OleDbCommand cmd1 = new OleDbCommand();
        cmd1.CommandType = CommandType.Text;
        cmd1.CommandText = query;
        cmd1.Connection = con1;
 
        con1.Open();
        dddivision.DataSource = cmd1.ExecuteReader();
        dddivision.DataTextField = "division_id";
        dddivision.DataValueField = "division_id";
        dddivision.DataBind();
 
        cmd1.Connection.Close();
        cmd1.Connection.Dispose();
 
    }
    public void populatedd2()
    {
        ddunitcode.Items.Add(new ListItem("-Select-", ""));
        ddunitcode.AppendDataBoundItems = true;
 
        OleDbConnection con2 = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
        string query1 = "SELECT unitcode FROM unit_master";
        OleDbCommand cmd2 = new OleDbCommand();
        cmd2.CommandType = CommandType.Text;
        cmd2.CommandText = query1;
        cmd2.Connection = con2;
 
        con2.Open();
        ddunitcode.DataSource = cmd2.ExecuteReader();
        ddunitcode.DataTextField = "unitcode";
        ddunitcode.DataValueField = "unitcode";
        ddunitcode.DataBind();
 
        cmd2.Connection.Close();
        cmd2.Connection.Dispose();
 
    }
    public void populatedd3()
    {
        ddsba.Items.Add(new ListItem("-Select-", ""));
        ddsba.AppendDataBoundItems = true;
 
        OleDbConnection con3 = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
        string query2 = "SELECT pc_code FROM sba_master";
        OleDbCommand cmd3 = new OleDbCommand();
        cmd3.CommandType = CommandType.Text;
        cmd3.CommandText = query2;
        cmd3.Connection = con3;
 
        con3.Open();
        ddsba.DataSource = cmd3.ExecuteReader();
        ddsba.DataTextField = "pc_code";
        ddsba.DataValueField = "pc_code";
        ddsba.DataBind();
 
        cmd3.Connection.Close();
        cmd3.Connection.Dispose();
 
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string div = dddivision.SelectedValue.ToString();
            string unit = ddunitcode.SelectedValue.ToString();
            string sba = ddsba.SelectedValue.ToString();
            OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
            string query = "SELECT user_id,passwd,dm.division_id,u.unitcode,sm.pc_code,empcode from register,division_master dm,unit_master u,sba_master sm ";
            int resilt = 0;
            using (con)
            {
                con.Open();
                using (OleDbCommand cmd = new OleDbCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("user_id", txtuserid.Text);
                    cmd.Parameters.AddWithValue("passwd", txtpass.Text);
                    cmd.Parameters.AddWithValue("empcode", txtemp.Text);
                    cmd.Parameters.AddWithValue("division_id", div);
                    cmd.Parameters.AddWithValue("unitcode", unit);
                    cmd.Parameters.AddWithValue("pc_code", sba);
 
                 
                    resilt = (int)cmd.ExecuteScalar();
 

                }
                con.Close();
            }
            if (resilt > 0)
            {
                Response.Redirect("Home.aspx");
            }
            else
            {
                Response.Redirect("Error.aspx");
            }
        }
        catch (Exception ex)
        {
            Response.Write("Error In An Application :" + ex.StackTrace + ex.Message);
        }
    }
    protected void reset_Click(object sender, EventArgs e)
    {
 
    }
 
    protected void txtuserid_TextChanged(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection myConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
           
 
            string qw = "select empcode from user_master where user_id = '" + txtuserid.Text + "'";
 
            
            OleDbCommand com = new OleDbCommand();
            com.Connection = myConn;
            
            com.CommandType = CommandType.Text;
            com.CommandText = qw;
            com.Connection = myConn;
            myConn.Open();
 
            txtemp.Text = com.ExecuteScalar().ToString();
 
            myConn.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
Posted 16-Apr-13 8:13am
Edited 16-Apr-13 8:24am
Nelek63K
v2
Comments
ryanb31 at 16-Apr-13 13:26pm
   
What's wrong?
Devendra Dighe at 16-Apr-13 13:42pm
   
no error but it does no login and redirect to home.aspx
ryanb31 at 16-Apr-13 13:53pm
   
OK, there's a lot of code. Can you narrow it down to exactly what is happening?
Member 9581488 at 16-Apr-13 13:26pm
   
are you getting any error?
Devendra Dighe at 16-Apr-13 13:42pm
   
no error but does no redirect to home.aspx
Member 9581488 at 16-Apr-13 13:48pm
   
Have you tried debugging with break-point?
Devendra Dighe at 16-Apr-13 13:50pm
   
no
Member 9581488 at 16-Apr-13 13:51pm
   
Try it and you will at least get hint where is the error or why its not redirecting!
Devendra Dighe at 16-Apr-13 15:26pm
   
i debugged got i got exception in ex-specified cast is not valid for the line 118 resilt = (int)cmd.ExecuteScalar(); for this line
Member 9581488 at 16-Apr-13 13:55pm
   
Moreover, I think you have SQL statement just for selecting records from table! You dont have WHERE clause in your SQL query and you are passing arguments with some textbox and dropdown values.
If you want to verify the username and password you have to use WHERE clause in SQL Query !
Devendra Dighe at 16-Apr-13 14:05pm
   
which condition should i put in where clause
Member 9581488 at 16-Apr-13 14:09pm
   
Where username=@user_id and so on(rest of the fields here to get the exact match)...
and change the code
cmd.Parameters.AddWithValue("user_id", txtuserid.Text);
to
cmd.Parameters.AddWithValue("@user_id", Convert.ToString(txtuserid.Text));
Devendra Dighe at 16-Apr-13 15:12pm
   
Here is the code which i updated , but no use still same problem it does not take me anywhere
 
string query = "SELECT user_id,passwd,empcode,dm.division_id,u.unitcode,sm.pc_code from register,division_master dm,unit_master u,sba_master sm where user_id=@user_id and passwd = @passwd and empcode = @empcode and division_id = @division_id and unitcode =@unitcode and pc_code =@pc_code";

int resilt = 0;
using (con)
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
cmd.Parameters.AddWithValue("@user_id", Convert.ToString(txtuserid.Text));
cmd.Parameters.AddWithValue("@passwd", Convert.ToString(txtpass.Text));
cmd.Parameters.AddWithValue("@empcode", Convert.ToString(txtemp.Text));
cmd.Parameters.AddWithValue("division_id", Convert.ToString(div));
cmd.Parameters.AddWithValue("unitcode", Convert.ToString(unit));
cmd.Parameters.AddWithValue("pc_code", Convert.ToString(sba));
 

resilt = (int)cmd.ExecuteScalar();
 

}
con.Close();
Member 9581488 at 16-Apr-13 15:16pm
   
You forgot to put @ in division_id,unitcode,pc_code!
use breakpoint and see what is the value of resilt.
Member 9581488 at 16-Apr-13 15:18pm
   
SELECT user_id,passwd,empcode,dm.division_id,u.unitcode,sm.pc_code from register,division_master dm,unit_master u,sba_master sm where user_id=@user_id and passwd = @passwd and empcode = @empcode and division_id = @division_id and unitcode =@unitcode and pc_code =@pc_code
 
change the above query to following one:
SELECT COUNT(*) from (TabelName) where user_id=@user_id and passwd = @passwd and empcode = @empcode and division_id = @division_id and unitcode =@unitcode and pc_code =@pc_code
 
The count will return 1 if it finds match!
Devendra Dighe at 16-Apr-13 15:31pm
   
i debugged got i got exception in ex-specified cast is not valid for the line 118 resilt = (int)cmd.ExecuteScalar(); for this line
Member 9581488 at 16-Apr-13 15:45pm
   
select COUNT(*)as Result
try putting this!
Devendra Dighe at 16-Apr-13 15:50pm
   
Yesssss.............
Got it .............
Thank You very much Member9581488
Member 9581488 at 16-Apr-13 15:53pm
   
Glad to help you!
for session variables:-
please refer this link
http://msdn.microsoft.com/en-us/library/ms178581(v=vs.100).aspx

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Rather than re-invent the wheel, is there a good reason why you aren't using Membership[^] - it is very easy to use, provides good security, and can be extended very simply by
Implementing a custom Membership Provider
[^]
  Permalink  
Comments
Devendra Dighe at 16-Apr-13 14:08pm
   
Hello OriginalGriff, how to impliment membership can u help me in it
OriginalGriff at 16-Apr-13 14:19pm
   
Follow the first link - it's a full tutorial and will guide you through the process a lot better than I could is a little textbox! :laugh:

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 319
2 CPallini 275
3 DamithSL 214
4 Maciej Los 185
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100