Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
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;


namespace WebPage
{
public partial class reg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)

{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["regConnectionString"].ConnectionString);
conn.Open();
string checkuser = " select count(*) from UserDetail where UserName='" + TextBoxUN + "'";
SqlCommand com = new SqlCommand(checkuser,conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User already exists");
}
conn.Close();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["regConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into UserDetail (UserName,E-Mail,Password,Age,Gender,Address,Qualification,Country) values (@Uname ,@email ,@password ,@age ,@gender ,@address ,@qualification ,@country)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Uname",TextBoxUN.Text);
com.Parameters.AddWithValue("@email", TextBoxEmailid.Text);
com.Parameters.AddWithValue("@password", TextBoxpass.Text);
com.Parameters.AddWithValue("@age", DropDownListAge.SelectedItem.ToString());
com.Parameters.AddWithValue("@gender", DropDownListGender.SelectedItem.ToString());
com.Parameters.AddWithValue("@address", TextBoxAddress.Text);
com.Parameters.AddWithValue("@qualification", TextBoxQualification.Text);
com.Parameters.AddWithValue("@country", DropDownListCountry.SelectedItem.ToString());
com.ExecuteNonQuery();

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

}

}

}
Posted

1 solution

There are so many places there that could be wrong, but lets start with teh obvious stuff..
C#
string checkuser = " select count(*) from UserDetail where UserName='" + TextBoxUN + "'";
Won't do what you want; the string will be:
SQL
select count(*) from UserDetail where UserName='System.Web.UI.WebControls.TextBox'
Which won't match anything...unless you have a really dull user. You need TextBoxUN.Text instead, but...Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

C#
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
Why?
Why are you getting an interger retirn value, converting it to a string, in order to convert it back to an integer, and put it into an integer variable? Doesn't that seem a little...um...pointless to you?
C#
int temp = com.ExecuteScalar();
Does the job without the rubbish...

C#
string insertQuery = "insert into UserDetail (UserName,E-Mail,Password,Age,Gender,Address,Qualification,Country) values (@Uname ,@email ,@password ,@age ,@gender ,@address ,@qualification ,@country)";

This causes teh problem you complain about: and it's pretty obvious why. Why did you put a hyphen in E-Mail? Didn't it occurr to you that SQL would see that as "E minus Mail" and complain? The best solution is remove teh hyphen form teh column name, but you could use delimiters round it:
C#
string insertQuery = "insert into UserDetail (UserName,[E-Mail],Password,Age,Gender,Address,Qualification,Country) values (@Uname ,@email ,@password ,@age ,@gender ,@address ,@qualification ,@country)";


And one more thing: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
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