zaven
zaven

Reputation: 27

Whenever i submit the form i'm having this runtime error

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Registration

{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //checking if there is a user with the same username
        protected void Page_Load(object sender, EventArgs e)
        {
            if(IsPostBack)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                conn.Open();
                string checkuser = "select count(*) from Table where UserName='" +FullName.Text +"'";
                SqlCommand cmd = new SqlCommand(checkuser, conn);
   //SqlException was unhandled of type"System.Data.SQlClient.SqlException" 
    //was not handled in user code
                int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                if (temp == 1)
                    Response.Write("User Already Exists");


                conn.Close();
            }



        }

        protected void Button1_Click(object sender, EventArgs e)
        {
         //Insert into my database table
            try
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                conn.Open();
                string insertuser = "insert into Table (Username,Email,Passwrod,Age,Degree) values (@username,@email,@password,@age,@degree)";
                SqlCommand cmd = new SqlCommand(insertuser, conn);
                cmd.Parameters.AddWithValue("@username" , FullName.Text);
                cmd.Parameters.AddWithValue("@email", Email.Text);
                cmd.Parameters.AddWithValue("@password", Password.Text);
                cmd.Parameters.AddWithValue("@age", Age.Text);
                cmd.Parameters.AddWithValue("@degree", Degree.Text);
                cmd.ExecuteNonQuery();
                Response.Write("Registration Successfull");

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

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

Upvotes: 0

Views: 58

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

Table is reserved keyword in sql, you need to wrap it properly like [Table] if you have named really your table as Table, and i also suggest you to name your table meaningful, right now it is vague name.

You also have to put try catch block for preventing your application to crash, and do not polute your every method with connection database access code instead of that create a helper class and move common functionalities there and just call it all other places for re-usability that would be easy to maintain.

Upvotes: 2

Related Questions