0

I am creating registration Form, here's the code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
        con.Open();
        string insCmd = "Insert into Registration(UserName, Password, EmailAdress, FullName, Country) vaues (@UserName, @Password, @EmailAddress, @FullName, @Country)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text);
        insertUser.Parameters.AddWithValue("@EmailAddress",TextBoxEA.Text );
        insertUser.Parameters.AddWithValue("@FullName",TextBoxFN.Text);
        insertUser.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString());

        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            Response.Write("<b>Something is not going good.. Please try again..</b>");
        }
        finally
        { 
         // Other Action
        }

    }
}

But when i want to register a new user it gives me the catch statement error.
Something is not going good.. Please try again..
please hep

Edited by Kleon: more specific

2
Contributors
6
Replies
21
Views
3 Years
Discussion Span
Last Post by Kleon
0

Any easy way to see the error if you aren't familiar with extracting the exception is to temporarily remove the try..catch blocks.

The app will fail and the error will be displayed assuming you allow the remote system to see the error (configured in the web.config)

0

yeah i know i am new in programming starting almost 2 months now.
Even if i remove the try.. catch block, it's not storing data to the database, please can you be more specific how to fix my problem how to handle with this.

0

Even if i remove the try.. catch block,

Yes, you should now see the actual error. The point of the try..catch is to catch the error and do something with it rather than letting the app crash and you user has a bad experience.

please can you be more specific how to fix my problem how to handle with this.

Well, I was hoping you were going to post the actual error message so we can see. It's easier to provide guidance when you know what the problem is.

Now that I'm at a PC and looking closer at your code, I dont see where you are filling the value for @UserName. There is no insertUser.Parameters.AddWithValue for @UserName.

Edited by JorgeM

0

Sir, its not giving me any error on compile , i can compile and run it but when i want to store the datas to the database like create a new user it is giving me this "<b>Something is not going good.. Please try again..</b>",
even if i remove the try catch block its doing the same.

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["RegConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select Count(*) from Registration where UserName='"+ TextBoxUN.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 /> Plese try another UserName");
            }
        }
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
            con.Open();
            string insCmd = "Insert into Registration (UserName, Password, EmailAdress, FullName, Country) values (@UserName, @Password, @EmailAddress, @FullName, @Country)";
            SqlCommand insertUser = new SqlCommand(insCmd, con);
            insertUser.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
            insertUser.Parameters.AddWithValue("@Password", TextBoxPass.Text);
            insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
            insertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text);
            insertUser.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString());

            try
            {
                insertUser.ExecuteNonQuery();
                con.Close();
                Response.Redirect("Login.aspx");
            }
            catch (Exception er)
            {
                Response.Write("<b>Something is not going good.. Please try again..</b>");
            }
            finally
            {
                // Other Action
            }


    }
}
0

even if i remove the try catch block its doing the same.

I am not saying to remove the whole block of code. What I meant was to instead of using the try...catch, just run the code within the try section. Remove lines 40-53 and replace it only with this code.

            insertUser.ExecuteNonQuery();
            con.Close();

The reason why you are getting the error: "Something is not good" is because the code in the try section failed. Maybe the database is not accessible, of its the wrong table name, or you are sending values incompatible with the data types for the fields.

I don't know because you are not letting that error be visible because you are telling your program to simple display something is not good.

0

Thank you JorgeM, i solved the Problem, i had a sintax error, instead of EmailAddress, i typed EmailAdress, was missing one d. any way thank for your help.

string insCmd = "Insert into Registration (UserName, Password, **EmailAdress**, FullName, Country) values (@UserName, @Password, @EmailAddress, @FullName, @Country)";
            SqlCommand insertUser = new SqlCommand(insCmd, con);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.