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 : 2690 | Status : [Member] | Replies : 1
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: 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