0

i'm a newbie in .NET and C# field. I'm creating a Registration page to a website im working on. i keep getting an error when registering to a website i'm creating. When entering my details, it doesn't register me to the database. I created the a table in the database(i created a connectionString), and yet i cannot register - i get an exception that says "error,please try registering again" (as i did). Does someone know what am i doing wrong?! Thanks!

here's my code and images:

    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["RegisConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "Select count(*) from Table where Username='" + TextBox1Username.Text + "'";
        SqlCommand userExist = new SqlCommand(cmdStr, con);
      //  int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
        con.Close();
       /* if (temp == 1)
        {
            Response.Write("username already exists");
        } */
    }
}


protected void TextBox2_TextChanged(object sender, EventArgs e)
{

}


protected void Submit_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegisConnectionString"].ConnectionStr        ing);
    con.Open();
    string insCmd = "Insert into Table (Username, Password, EmailAddress,Fullname, City) values (@Username, @Password, @EmailAddress, @Fullname, @City)";
    SqlCommand insertUser = new SqlCommand(insCmd, con);
    insertUser.Parameters.AddWithValue("@Username", TextBox1Username.Text);
    insertUser.Parameters.AddWithValue("@Password", TextBox2Password.Text);
    insertUser.Parameters.AddWithValue("@EmailAddress", TextBox4Email.Text);
    insertUser.Parameters.AddWithValue("@Password", TextBox2Password.Text);
    insertUser.Parameters.AddWithValue("@City", TextBox6City.Text);

    try
    {
        insertUser.ExecuteNonQuery();
        con.Close();
        Response.Redirect("Login.aspx");
    }
    catch (Exception er)
    {
        Response.Write("error,please try registering again");
    }

}

}

image:

[URL=http://imageshack.us/photo/my-images/4/os6b.jpg/][IMG=http://img4.imageshack.us/img4/2526/os6b.jpg][/IMG][/URL]

Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]

user2868855
  • 7
  • 2
  • 6
  • 1
    Your catch swallows the exception, You should add the er.Message to your Response to understand what is failing in your code. – Steve Oct 10 '13 at 21:50

2 Answers2

0

You pass two times the password parameter in the insert query. The second one is in place of the @fullname parameter.

This will cause your insert command to fail because you don't provide the @fullname parameter expected by the query.

SqlCommand insertUser = new SqlCommand(insCmd, con);
insertUser.Parameters.AddWithValue("@Username", TextBox1Username.Text);
insertUser.Parameters.AddWithValue("@Password", TextBox2Password.Text);
insertUser.Parameters.AddWithValue("@EmailAddress", TextBox4Email.Text);
insertUser.Parameters.AddWithValue("@FullName", TextBox???????.Text);   //<- Get from the correct textbox
insertUser.Parameters.AddWithValue("@City", TextBox6City.Text);

You could have found this problem yourself if, inside the catch, you had added the er.Message to your Response

try
{
    insertUser.ExecuteNonQuery();
    con.Close();
    Response.Redirect("Login.aspx");
}
catch (Exception er)
{
    Response.Write("error: " + er.Message + " ,please try registering again");
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

you are missing fullname in your parameter and you have password twice.fix that and run it again.

Also, in your catch block.. you should use exception message to debug or log somewhere where you can see the error clearly. the way you have it right now gives the error you typed. not actual message.

here is link where they are discussing about sqlexception. might be good idea to catch that.

SqlException catch and handling

Community
  • 1
  • 1
AJP
  • 2,125
  • 3
  • 16
  • 22