SQL Command Insert Is Working But the Data Not Appear in Table

SQL command INSERT is working but the data not appear in table

If your c# code executes without any exceptions, it updates the database too. You have probably used AttachDbFilename=|DataDirectory|\yourDB.mdf in your ConnectionString, that means the databse that is updated is located in the subfolder BIN\DEBUG folder of your project. If you want to see the updated data just attach the database located in the bin/debug folder in ssms.
for more details read this post.
Also make sure your table in server explorer is not already open, if it is already open you must refresh it to show updated data. Please note:as mentioned in the comments you should always use parameterized queries to avoid Sql Injection.

no error on sql insert, yet table has no data

SQL uses single quotes. Javascript uses either.

You want the resulting SQL to be as if you'd written

INSERT info foo (a,b) values ('a value', 'b value')

The simplest more-correct equivalent would be:

var theData = db.execute("INSERT INTO events (gCal_uid, title, content, location, startTime, endTime, published, updated, eventStatus) VALUES('"+calUid+"','"+title+"','"+content+"','"+location+"','"+startTime+"','"+endTime+"','"+published+"','"+updated+"','"+eventStatus+"')");

But you really want to use parameter substitution to avoid injection issues and quoting errors, e.g.

var theData = db.execute("INSERT INTO events (gCal_uid, title, content, location, startTime, endTime, published, updated, eventStatus) values (?,?,?,?,?,?,?,?,?)", calUid, title, content, location, startTime, endTime, published, updated, eventStatus);

C# SQL INSERT not appearing in database table no errors or exceptions thrown

Hay guys thanks you all for the response after a couple of hours i finally figured out there was nothing wrong in my code. The database that was being update was the one in the bin/debug folder not the one i being referenced in my solution. I did not realize that all changes while working on a project are done to the copy in debug folder.

Data is not inserting into table?

As I've said before on this site - the whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. VictoryDatabase)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=VictoryDatabase;Integrated Security=True

    and everything else is exactly the same as before...

Insert and Registration data not show in database table visual studio 2019

Based on my test, I find that you defined the SqlCommand.CommandText two times.

Please try to modify your code to the following.

SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "insert into Student(ID,StudentName,DateOfBirth)values(@ID,@StudentName,@DateOfBirth)";
command.Parameters.AddWithValue("@ID", Convert.ToInt32(ID));
command.Parameters.AddWithValue("@StudentName", textBox2.Text);
command.Parameters.AddWithValue("@DateOfBirth", DateOfBirth

);

Also, please note that we should place the MessageBox.Show after the code com.ExecuteNonQuery();.

Here is a code example you could refer to, based on my test, it works well.

 private void button1_Click(object sender, EventArgs e)
{
try
{
string ID = textBox1.Text;
string StudentName = textBox2.Text;
DateTime DateOfBirth = dateTimePicker1.Value;
string constr = "sttr";
SqlConnection connection = new SqlConnection(constr);
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "insert into Student(ID,StudentName,DateOfBirth)values(@ID,@StudentName,@DateOfBirth)";
command.Parameters.AddWithValue("@ID", Convert.ToInt32(ID));
command.Parameters.AddWithValue("@StudentName", textBox2.Text);
command.Parameters.AddWithValue("@DateOfBirth", DateOfBirth);
command.ExecuteNonQuery();
MessageBox.Show("success inserted");
connection.Close();

}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}

}

private void button2_Click(object sender, EventArgs e)
{
string ID = textBox1.Text;
string constr = "str";
SqlConnection connection = new SqlConnection(constr);
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT COUNT(*) AS [Count] FROM Student WHERE ID =@ID";
command.Parameters.AddWithValue("@ID", Convert.ToInt32(ID));
SqlDataReader sdr = command.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr.Read())
{
if (Convert.ToInt32(sdr["count"]) == 1)
{
button2.Enabled = false;
button1.Enabled = true;
}
else
{
button2.Enabled = true;
button1.Enabled = false;
}
}
MessageBox.Show("Record Updated Successfully!!");

}


Related Topics



Leave a reply



Submit