Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I'm new to asp.net. I'm designing a new login form that takes username and password.I also have a register option and how can I write my code so that It checks weather the username available or not. I'm using SQL server and C#.
Posted
Comments
Sergey Alexandrovich Kryukov 2-Dec-14 2:51am    
What do you mean "how"? By executing appropriate SQL query, according to your database schema. Where did you stuck?
—SA
syed shanu 2-Dec-14 2:56am    
Check this links ,This will help you to solve your problem.

http://www.mikesdotnetting.com/article/75/simple-login-and-redirect-for-asp-net-and-access

http://www.aspsnippets.com/Articles/Simple-User-Login-Form-example-in-ASPNet.aspx

You Can use sql Stored Proc.

SQL
Create proc_UserNameExist
@UserName as Varchar(50)
as
declare @Count int

select @Count=Count(*) from tblUser where userName=@UserName
if(@Count>0)
begin
---User Found
end
else
begin
---User Not Found
end
 
Share this answer
 
web.Config // Connect to database
XML
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>



//Action Your Button
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password",con);
cmd.Parameters.AddWithValue("@username", txtUserName.Text);
cmd.Parameters.AddWithValue("@password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
Response.Redirect("Details.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}


If you using Sql Server Authentication, you add UserId="" and Password="" in Web.Config
 
Share this answer
 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900