Question OnClick

Knutsford

New member
Joined
Jan 17, 2017
Messages
4
Programming Experience
10+
C#:
<%@ Page Title="Default3" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default3.aspx.cs" Inherits="test5.Default3" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
   
         <asp:Label ID="Username_Label" runat="server" Text="Username   "></asp:Label>
        <asp:TextBox ID="Username" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Password_Label" runat="server" Text="Password"></asp:Label>
        <asp:TextBox ID="Password" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  
 
</asp:Content>



This is my Code Behind


C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

namespace test5
{
    public partial class Default3 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\test.accdb";
            if (Username.Text == "" || Password.Text == "")
            {
                MessageBox.Show("Please provide UserName and Password");
                return;
            }
            try
            {
                OleDbConnection con = new OleDbConnection(cs);

                OleDbCommand cmd = new OleDbCommand("Select * from Admin where UserName=@username and Password=@password", con);

                cmd.Parameters.AddWithValue("@username", Username.Text);
                cmd.Parameters.AddWithValue("@password", Password.Text);
                con.Open();
                OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);

                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                int count = ds.Tables[0].Rows.Count;
                //If count is equal to 1, than show frmMain form
                if (count == 1)
                {
                    MessageBox.Show("Login Successful!");

                    Session["LoggedIn"] = "Yes";
                    Response.Redirect("LoggedIn");
                    
                }
                else
                {
                    MessageBox.Show("Login Failed!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

}




It is a web application


In protected void Button1_Click(object sender, EventArgs e)


I am getting "The name 'username' does not exist in the current context and the the same for Password and I have no idea why


Thanks
 
Where exactly in that method? Are you talking about here:
if (Username.Text == "" || Password.Text == "")

If so then it appears that you're trying to refer to a couple of TextBox controls that you never actually added to your form.
 
The text boxes were in the form. It was the designer file that was the problem. They weren't in there for some reason - I had to delete it and recreate it then it worked. Thanks
 

Latest posts

Back
Top Bottom