Khemchand Verma
Khemchand Verma

Reputation: 1

Error in insert the value into database in asp.net ,c# of registration form

Code show as follow:

nection cnn = new SqlConnection("Data Source=USER-PC\\KHEMCHAND;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
    cnn.Open();
}

protected void Button1_Click(object sender, EventArgs e)
{  
    SqlCommand cmd = new SqlCommand("insert into tbl_ragistration values(@f_name,@l_name,@email_id,@pass_word,@[date of brth],@add_ress,@gender)", cnn);
    cmd.Parameters.AddWithValue("@f_name", Texfname.Text);
    cmd.Parameters.AddWithValue("@l_name", Texlname.Text);
    cmd.Parameters.AddWithValue("@email_id", Texemail.Text);
    cmd.Parameters.AddWithValue("@pass_word", Texpwd.Text);
    cmd.Parameters.AddWithValue("@[date o birth]", Texdbt.Text);
    cmd.Parameters.AddWithValue("@add_ress", Texadd.Text);
    cmd.Parameters.AddWithValue("@gender", DropDownList1.Text);
    cmd.ExecuteNonQuery();
    cnn.Close();
}

Upvotes: 0

Views: 951

Answers (4)

Zo Has
Zo Has

Reputation: 13018

  1. Open the connection while executing the command & not on page Load & wrap your code inside a try catch block to log the error.
    protected void Button1_Click(object sender, EventArgs e)
    {  
SQLConnection cnn = new SqlConnection(WebConfigurationManager.ConnectionStrings["yourConnectionString"].ConnectionString);

 SqlCommand cmd = new SqlCommand("insert into tbl_ragistration values(@f_name,@l_name,@email_id,@pass_word,@[date of brth],@add_ress,@gender)", cnn);
    cmd.Parameters.AddWithValue("@f_name", Texfname.Text);
    cmd.Parameters.AddWithValue("@l_name", Texlname.Text);
    cmd.Parameters.AddWithValue("@email_id", Texemail.Text);
    cmd.Parameters.AddWithValue("@pass_word", Texpwd.Text);
    cmd.Parameters.AddWithValue("@[date o birth]", Texdbt.Text);
    cmd.Parameters.AddWithValue("@add_ress", Texadd.Text);
    cmd.Parameters.AddWithValue("@gender", DropDownList1.Text);
    try
    {
       cnn.Open();
       cmd.ExecuteNonQuery();
    }
    catch(SQLException ex)
    {  // Log your error
       lblStatus.Text="An error occured"+ ex.Message; 
       throw ex;
    }
    finally
    {
      if(cnn!=null)
      {
          cnn.Close();
      }
    }
}

Upvotes: 0

BizApps
BizApps

Reputation: 6130

make sure you declare also the column names on your insert statement:

protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection cnn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd =new SqlCommand(
            "insert into tbl_ragistration (f_name,l_name,email_id,pass_word,[date of brth],add_ress,gender) values (@f_name,@l_name,@email_id,@pass_word,@dateofbrth,@add_ress,@gender)"
            ,cnn))
        {
            cmd.Parameters.AddWithValue("@f_name", Texfname.Text);
            cmd.Parameters.AddWithValue("@l_name", Texlname.Text);
            cmd.Parameters.AddWithValue("@email_id", Texemail.Text);
            cmd.Parameters.AddWithValue("@pass_word", Texpwd.Text);
            cmd.Parameters.AddWithValue("@dateofbirth", Texdbt.Text);
            cmd.Parameters.AddWithValue("@add_ress", Texadd.Text);
            cmd.Parameters.AddWithValue("@gender", DropDownList1.Text);
            cnn.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

Regards

Upvotes: 1

Elias Hossain
Elias Hossain

Reputation: 4469

You can try with below way, I hope this is good approach (though database execution should put in Data Access Layer), thanks for your time.

public string ConnectionString 
{
    get
    {
        return "Data Source=USER-PC\\KHEMCHAND;Integrated Security=True";
    }
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection cnn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd =new SqlCommand(
            "insert into tbl_ragistration values(@f_name,@l_name,@email_id,@pass_word,@[date of brth],@add_ress,@gender)"
            ,cnn))
        {
            cmd.Parameters.AddWithValue("@f_name", Texfname.Text);
            cmd.Parameters.AddWithValue("@l_name", Texlname.Text);
            cmd.Parameters.AddWithValue("@email_id", Texemail.Text);
            cmd.Parameters.AddWithValue("@pass_word", Texpwd.Text);
            cmd.Parameters.AddWithValue("@[date o birth]", Texdbt.Text);
            cmd.Parameters.AddWithValue("@add_ress", Texadd.Text);
            cmd.Parameters.AddWithValue("@gender", DropDownList1.Text);
            cnn.Open();
            cmd.ExecuteNonQuery();
        }
    }
} 

Upvotes: 1

Change cmd.Parameters.AddWithValue("@[date o birth]", Texdbt.Text); to cmd.Parameters.AddWithValue("@[date of birth]", Texdbt.Text); in Insert parameters value assigning. And also dont open the connection in the page load event. Befor executing the query open the connection, Execute the query and then close the connection for a better approach

       cnn.Open();
       cmd.ExecuteNonQuery();
       cnn.Close(); 

Upvotes: 1

Related Questions