Hi!
These codes allow me to save without problem.
But what I want to know it's after saving my record. I would like to see the latest record (only one culumn as I specify in my code) appear on my label just AFTER SAVING and ON TIME.
Please have a look:
private void btnGravar_Facturacao_Click(object sender, EventArgs e)
{
{
conn = new SqlConnection(connstr);
comm = new SqlCommand();
conn.Open();
SqlParameter numberprint = new SqlParameter("@numberprint", SqlDbType.NChar);
SqlParameter data = new SqlParameter("@datafact", SqlDbType.Date);
comm.Parameters.Add(numberprint);
comm.Parameters.Add(data);
numberprint.Value = lblNumberprint.Text;
data.Value = DateTime.Today;
comm.Connection = conn;
comm.CommandText = "insert into facturacao1 values(@numberprint,@datafact)";
//////////////////////////////////It's here where I try to write the code to show the latest record saving and every time on time//////////////////
string connectionString = @"Data Source=localhost\sqlexpress;Initial Catalog=master;Integrated Security=True";
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "select * from facturacao1 where numberprint ='" + lblNumberprint.Text + "'";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader read = sqlCmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
lblNumberprint.Text = read["numberprint"].ToString();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
try
{
comm.ExecuteNonQuery();
MessageBox.Show("Saved");
}
catch (Exception)
{
MessageBox.Show("Not saved");
}
finally
{
conn.Close();
}
}
}
}
12 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 24, 2014, 3:34 PM
That's what this is doing:
Do you want the 'datafact' column value to be shown as well?
IsraelPosted Jul 24, 2014, 4:11 PM
IsraelPosted Jul 24, 2014, 11:28 AM
1. this line of code works without problem.
2. I just want see the last record of this column appear on this label "lblNumberprint.Text". THAT'S ALL
3. when the last record should appear? Just when after saving.
VulpesPosted Jul 19, 2014, 3:27 PM
Are you saying that it isn't working?
IsraelPosted Jul 19, 2014, 2:14 PM
IsraelPosted Jul 16, 2014, 9:15 AM
VulpesPosted Jul 10, 2014, 7:58 PM
You can then sort in descending order on this field and select the 'top' 1 to get the latest added.
IsraelPosted Jul 10, 2014, 7:05 PM
VulpesPosted Jul 10, 2014, 4:32 PM
IsraelPosted Jul 10, 2014, 1:05 PM
VulpesPosted Jul 10, 2014, 12:02 PM
However, it's not really necessary to check that the record has been saved in this way because the ExecuteNonQuery() method returns the number of rows affected. So if this is more than zero, then the record has been saved.
IsraelPosted Jul 10, 2014, 11:14 AM