I wrote code to insert data into the database, after I click the insert button, nothing happens and there is no error. Please I need help.
If I click the insert button from the front end, it is supposed to populate the database.
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;
namespace insertAdo
{
public partial class TableInsert : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString;
SqlConnection cnn;
connectionString = @"Data Source=DESKTOP-5JPBK1L;Database=StudentDB; Integrated Security=SSPI";
cnn = new SqlConnection(connectionString);
cnn.Open();
{
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "";
sql = "INSERT INTO student_table (Student_id, name, Email, Join_date) VALUES (102, 'Jaja Peter', '[email protected]', '09/30/2021')";
command = new SqlCommand(sql, cnn);
adapter.InsertCommand = new SqlCommand(sql, cnn);
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
}
Response.Write("Insert successful");
cnn.Close();
}
}
}
Response.Write("Insert Successful");this line of code executes?command.ExecuteNonQuery(). You don't needadapter.usinginstead of manually Disposing the command.