I have created a registration page for new user's login.....means the new user wil create his username and password and will access to the site by using his login username and password...
here is my code behind for registering for new user(.aspx.cs)
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 con = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentEntConnectionString"].ConnectionString.ToString());
con.Open();
string cmdstr = "Select count(*) from Reg_Db where UserName = '" + TextBox1.Text + "'";
SqlCommand userExist = new SqlCommand(cmdstr, con);
int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
con.Close();
if (temp == 1)
{
Response.Write("Username Already Exists <br />Please Choose Another username ");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentEntConnectionString"].ConnectionString.ToString());
con.Open();
string inscmd = "Insert into Reg_Db (UserName,EmailAdd,Password)values(@UserName,@EmailAdd,@Password)";
SqlCommand insertUser = new SqlCommand(inscmd, con);
insertUser.Parameters.AddWithValue("@UserName", TextBox1.Text);
insertUser.Parameters.AddWithValue("@EmailAdd", TextBox2.Text);
insertUser.Parameters.AddWithValue("@UserName", TextBox3.Text);
try
{
insertUser.ExecuteNonQuery();
con.Close();
Response.Redirect("Login.aspx");
}
catch (Exception e1)
{
Response.Write(e1.Message);
//Response.Write("<b>Something Really Bad Happened...Please Try Again</b>");
}
//finally
//{
//}
}
}
In the My database the registration table(Reg_Db) the fiels are
1) UserId (datatype: int,Is identity on=yes,primary key set)
2) UserName (datatype: varchar(50))
3) EmailAdd (datatype: varchar(50))
4) Password (char(10))
But when i run this code it catches an exception and shows me the following error message
The variable name '@UserName' has already been declared. Variable names must be unique within a query batch or stored procedure. Must declare the scalar variable "@Password".
Plzzzzz reply me what is the exact problem and if possible rply me the code.....
As soon as Possible....