0

The insert query is not giving any error but the data is not getting inserted into the DB. When I check the database in sql server, there seems to be no data insertion. I'm not sure which part of the code has an error.

The following code:

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

namespace PayrollSystem05
{
    public partial class employee : System.Web.UI.Page
    {
      protected void Page_Load(object sender, EventArgs e)
        {

        }


protected void btnAdd_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP-M0G68DT\\SQLEXPRESS;Initial Catalog=PRS;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(@"INSERT INTO [payslip].[employee]
           ([emp_id]
           ,[emp_name]
           ,[emp_ic]
           ,[emp_address]
           ,[emp_mobile]
           ,[emp_email]
           ,[emp_startdate])
     VALUES
           ('"+ txtID.Text +"', '"+ txtName.Text +"', '"+ txtIC.Text +"', '"+ txtAdd.Text + "', '"+ txtMob.Text +"', '"+ txtEmail.Text +"', '" + txtStart.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("<script>alert('Employee added successfully.')</script>");
        }
    }
}```

3
  • Can you please check where your database is located whether it is in SQL Server orSQL Server Express? Commented May 19, 2020 at 23:59
  • It's in SQL Server, just figured out there's errors in the SQL queries. Commented May 24, 2020 at 5:46
  • use store procedure instead of inline which help to debug this easily Commented May 26, 2020 at 6:21

1 Answer 1

1

You can take the code below it's the same that your code, i make just some changes to be more clear and readable.

var connectionString = @"Data Source=DESKTOP-M0G68DT\SQLEXPRESS;Initial Catalog=PRS;Integrated Security=True";
var queryString = @"INSERT INTO [payslip].[employee]
           (
              [emp_id]
             ,[emp_name]
             ,[emp_ic]
             ,[emp_address]
             ,[emp_mobile]
             ,[emp_email]
             ,[emp_startdate]
            )
          VALUES
            (
              @emp_id
             ,@emp_name
             ,@emp_ic
             ,@emp_address
             ,@emp_mobile
             ,@emp_email
             ,@emp_startdate
            )";

        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            using (SqlCommand command = sqlConnection.CreateCommand())
            {
                command.CommandText = queryString;
                command.Parameters.AddWithValue("@emp_id", txtID.Text);
                command.Parameters.AddWithValue("@emp_name", txtName.Text);
                command.Parameters.AddWithValue("@emp_ic", txtIC.Text);
                command.Parameters.AddWithValue("@emp_address", txtAdd.Text);
                command.Parameters.AddWithValue("@emp_mobile", txtMob.Text);
                command.Parameters.AddWithValue("@emp_email", txtEmail.Text);
                command.Parameters.AddWithValue("@emp_startdate", txtStart.Text);

                command.ExecuteNonQuery();
            } 
        }
        Response.Write("<script>alert('Employee added successfully.')</script>");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.