3 Replies - 14664 Views - Last Post: 18 January 2011 - 11:46 PM Rate Topic: -----

#1 katelva  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 04-July 08

How do I check user input with SQL database?

Post icon  Posted 04 July 2008 - 11:29 PM

Hi,

I am newbie in asp.net C#. I created a login.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Main" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Navigation" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Footer" Runat="Server">
</asp:Content>

<asp:Content ID="Content5" runat="server" 
	contentplaceholderid="RightTopContent">
	<asp:Image ID="Image1" runat="server" Height="26px" Width="162px" />
	<br />
	Username :<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
	<br />
	Password :<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
	<br />
	<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
		Text="Login" />
	<br />
</asp:Content>
<asp:Content ID="Content6" runat="server" 
	contentplaceholderid="RightMiddleContent">
</asp:Content>


But, I do not know the way of writing code(syntax) in login.aspx.cs which I would like to check the user input(username,password) match with the SQL database or not? If the user input is correct in SQL DB then it will redirect to user information page,if not will appear an error page.
I just know how to connect SQL DB in C# asp.net but I do not know the syntax of checking the user input is matching/correct with the SQL DB or not?

Anyone can help?

Thanks.

Regards,
Katelva

Is This A Good Question/Topic? 1

Replies To: How do I check user input with SQL database?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How do I check user input with SQL database?

Posted 08 July 2008 - 01:54 PM

look into ADO.Net and using Stored Procedures.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How do I check user input with SQL database?

Posted 09 July 2008 - 09:33 AM

Here is a tutorial on SQL Basics in C# that will walk you through interacting with a SQL database with C#.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Mufaddal Saifee*


Reputation:

Re: How do I check user input with SQL database?

Posted 18 January 2011 - 11:46 PM

1. Go to solution explorer right click add new item SQL database..
2. Then go to solution explorer open database add table with fields username,password..as mentioned in the code below..

///////////////////////////////////////////////////

Default.aspx Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/MasterPage.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                                             <a href="default12.aspx">Contact us</a>
<marquee behavior="scroll">
                                      <img src="images/1.bmp" width="100" height="100"> 
                                      <img src="images/2.bmp" width="100" height="100"> 
                                      <img src="images/3.bmp" width="100" height="100"> 
                                      <img src="images/4.bmp" width="100" height="100"> 
                                      </marquee>
<pre>
 <font color="maroon">
 
    Id       <input type="text" id="txtId" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtId" Display="Dynamic" ErrorMessage="Please Enter Id"></asp:RequiredFieldValidator><br/>
     
    Password <input type="password" id="txtPassword" runat="server" visible="true" style="width: 148px" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="Please Enter Password"></asp:RequiredFieldValidator><br/>
             <input type="submit" value="Sign_In" id="b1" runat="server" onserverclick="b1_ServerClick" />   <a href="default2.aspx">Sign_up</a>
                   
         
 </font>
</pre>
<img src="images/4.bmp" />
</asp:Content>



///////////////////////////////////////////////////////

Default.aspx.cs Code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;
using System.Data.SqlClient;
//using System.Windows.Forms;


public partial class _Default : System.Web.UI.Page
{
    //OdbcConnection con;
    //OdbcCommand com;
    //OdbcDataReader dr;

    SqlCommand cmd;
    SqlConnection con;
    SqlDataReader dr;
    

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void b1_ServerClick(object sender, EventArgs e)
    {
        string Id, Password;

        Id = Convert.ToString(txtId.Value);
        Password = Convert.ToString(txtPassword.Value);

        con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Major Project Data new1\Visual Studio 2005\WebSites\Mufaddal\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        con.Open();

        cmd = new SqlCommand("select * from form where Id='" + Id + "' and Password='" + Password + "'", con);
        dr = cmd.ExecuteReader();
        //MessageBox.Show(cmd.ExecuteScalar().ToString());


        //con = new OdbcConnection("DSN=f9");
        //con.Open();
        //com = new OdbcCommand("select * from form where Id='" + Id + "' and Password='" + Password + "'", con);
        //dr = com.ExecuteReader();

            if (dr.Read())
            {
                Response.Redirect("Default4.aspx");

            }
            else
            {
                Response.Write("User Account Invalid");

            }

        
    }
    
}


Edited by Dogstopper: :code:!
Was This Post Helpful? 0

Page 1 of 1