Can't make it work to check existing username in database please help

Posted by New2world under ASP.NET on 5/21/2014 | Points: 10 | Views : 923 | Status : [Member] | Replies : 3
Hi, I have made registration page, everything works fine and inserting in to database. But what it doesn't work is check for the existing username exist or not in the database and show the message. Kindly please help me in this where I am doing wrong Thanks.

Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string checkuser = "SELECT UserName FROM UserData where UserName= '" + TextBoxUN + "'";
SqlCommand com = new SqlCommand(checkuser, conn);
com .Parameters.Add(new SqlParameter("@UserName", TextBoxUN.Text));

int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 0)
{
Session["UserName"] = TextBoxUN.Text;
Response.Write("Users already Exist");
}
conn.Close();
}
}


protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
{

}

protected void Button1_Click1(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into UserData (Username,Email,Password,Phone,Country,Company,Address) values (@Uname ,@email ,@password ,@phone ,@country ,@company ,@address)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.Add(new SqlParameter("@Uname", TextBoxUN.Text));
com.Parameters.Add(new SqlParameter("@email", TextBoxEmail.Text));
com.Parameters.Add(new SqlParameter("@password", TextBoxPass.Text));
com.Parameters.Add(new SqlParameter("@phone", TextBoxphone.Text));
com.Parameters.Add(new SqlParameter("@country", DropDownListCountry.SelectedItem.ToString()));
com.Parameters.Add(new SqlParameter("@company", DropDownListCompany.SelectedItem.ToString()));
com.Parameters.Add(new SqlParameter("@address", TextBoxAddress.Text));
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");

conn.Close();

}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}


}





Responses

Posted by: Satyapriyanayak on: 5/22/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Table 

create table employee (eid int primary key identity,ename varchar(50),eaddress varchar(50))


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_ifnot_exits._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:Button ID="Button1" runat="server" Text="Availability of name from the database"
BackColor="#FF99FF" Font-Bold="True" Width="329px"
onclick="Button1_Click" /><br />
<asp:Label ID="Labelcheck" Text="Ename" runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server" Width="197px"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName" ErrorMessage="*Name Required"></asp:RequiredFieldValidator><br />


<asp:Label ID="Label1" Text="Eaddress" runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="txtUserAddress" runat="server" Width="197px"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtUserAddress" ErrorMessage="*Address Required"></asp:RequiredFieldValidator>

<br />
<asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"
ForeColor="Black"></asp:Label>
</form>
</body>
</html>
using System;
using System.Collections;
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;
namespace Insert_data_ifnot_exits
{
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());
con.Close();
if (count > 0)
{
lblMessage.Text = "Sorry! you can't take this username";
}
else
{
lblMessage.Text = "You can take this username";
con.Open();
str = "insert into employee(ename,eaddress) values('" + txtUserName.Text + "','" + txtUserAddress.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();

}

}
}
}



If this post helps you mark it as answer
Thanks

New2world, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 5/22/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Table

create table employee (eid int primary key identity,ename varchar(50),eaddress varchar(50))


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Insert_data_ifnot_exits._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:Button ID="Button1" runat="server" Text="Availability of name from the database"
BackColor="#FF99FF" Font-Bold="True" Width="329px"
onclick="Button1_Click" /><br />
<asp:Label ID="Labelcheck" Text="Ename" runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server" Width="197px"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserName" ErrorMessage="*Name Required"></asp:RequiredFieldValidator><br />


<asp:Label ID="Label1" Text="Eaddress" runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="txtUserAddress" runat="server" Width="197px"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtUserAddress" ErrorMessage="*Address Required"></asp:RequiredFieldValidator>

<br />
<asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"
ForeColor="Black"></asp:Label>
</form>
</body>
</html>
using System;
using System.Collections;
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;
namespace Insert_data_ifnot_exits
{
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());
con.Close();
if (count > 0)
{
lblMessage.Text = "Sorry! you can't take this username";
}
else
{
lblMessage.Text = "You can take this username";
con.Open();
str = "insert into employee(ename,eaddress) values('" + txtUserName.Text + "','" + txtUserAddress.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();

}

}
}
}



If this post helps you mark it as answer
Thanks

New2world, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: New2world on: 5/22/2014 [Member] Starter | Points: 25

Up
0
Down
It's giving me this error : Error 1 Could not load type 'Insert_data_ifnot_exits._Default'.

Any help please?


New2world, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response