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();
}
}
}
}

VulpesRank #55 · 29.2K reputationPosted Jul 24, 2014, 11:34 AM EDT
That's what this is doing:
Do you want the 'datafact' column value to be shown as well?
IsraelRank #1,223 · 1.5K reputationPosted Jul 10, 2014, 7:14 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2014, 8:02 AM EDT
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.
IsraelRank #1,223 · 1.5K reputationPosted Jul 10, 2014, 9:05 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2014, 12:32 PM EDT
IsraelRank #1,223 · 1.5K reputationPosted Jul 10, 2014, 3:05 PM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2014, 3:58 PM EDT
You can then sort in descending order on this field and select the 'top' 1 to get the latest added.
IsraelRank #1,223 · 1.5K reputationPosted Jul 16, 2014, 5:15 AM EDT
IsraelRank #1,223 · 1.5K reputationPosted Jul 19, 2014, 10:14 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 19, 2014, 11:27 AM EDT
Are you saying that it isn't working?
IsraelRank #1,223 · 1.5K reputationPosted Jul 24, 2014, 7:28 AM EDT
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.
IsraelRank #1,223 · 1.5K reputationPosted Jul 24, 2014, 12:11 PM EDT