I am using visual studio 2010. I had created a registration page and login in page. In the registration page, user had to key in username and password and it will be store in the database. How do I link the database from registration page to login page so that I can check whether the user had key in the correct password or whether the user had registered aldready.
I'm using c# and Microsoft SQL management studio.
Thank you in advance.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conDatabase = null;
string username = TextBox1.Text;
string password = TextBox2.Text;
SqlConnection connection = null;
SqlCommand command = null;
SqlDataReader dataReader = null;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
connection = new SqlConnection(connectionString);
connection.Open();
//prepare sql statements
string sql = "SELECT * from Staff where username='" + username + "'And Password='" + password + "'";
command = new SqlCommand(sql, connection);
dataReader = command.ExecuteReader();
while (dataReader.Read())
{
username = dataReader.GetString(3);
Session.Add("username", username);
}
dataReader.Close();
}
catch catch (IndexOutOfRangeException)
{
username.Text = "";
password.Text = "";
{
Response.Write(ex.Message);
}
May I know if this is correct or do I need to add more things?
Loading
Satyapriya NayakPosted Oct 16, 2011, 10:02 AM
First check whether the user exits in the database or not.
Run the attachments.
Here is the code
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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com ;
string str = null;
protected void Button1_Click(object sender, EventArgs e)
{
namecheck();
}
public void namecheck()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*)from employee where ename='" + txtUserName.Text + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
if (count > 0)
{
lblMessage.Text = "Sorry! you can't take this username";
}
else
{
lblMessage.Text = "You can take this username";
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Thanks
If this post helps you mark it as answer