0

I have created a table tblAttendence in the database that has 2 columns: Date (datetime) and RegNo (int). I have to insert the current date & time and a registration number from a label in the form.

C# code:

private void btnMarkAtt_Click(object sender, EventArgs e)
{
    using (SqlConnection sqlCon = new SqlConnection(connectionString))
    {
        sqlCon.Open();

        SqlCommand sqlCmd = new SqlCommand("MarkAtt", sqlCon);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        sqlCmd.Parameters.AddWithValue("@Date", DateTime.Now);
        sqlCmd.Parameters.AddWithValue("@RegNo", int.Parse(lblRegNo.Text));

        sqlCmd.ExecuteNonQuery();

        MessageBox.Show("Attendance marked successfully!");
    }
}

Stored procedure MarkAtt:

ALTER PROCEDURE [dbo].[MarkAtt]
    @Date DATETIME,
    @RegNo INT
AS
    INSERT INTO tblAttendence(Date, RegNo)
    VALUES (@Date, @RegNo)

There is no error shown in the code. The debugging doesn't stop to show an error. When I press the button, just nothing happens (neither the data is inserted, nor the message box is shown).

I can't seem to find out what is going on. The connection string is correct (I have used it in the same form and it works). Is there something wrong with the connection? Or the stored procedure? Or anything else that I am missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Naman
  • 1
  • 3
  • This might be helpful https://stackoverflow.com/questions/31604951/sql-command-insert-is-working-but-the-data-not-appear-in-table – Salah Akbari Jul 07 '18 at 09:08
  • Where is your database located? Is this as a file in your project or separately with SQL Server? – Vikram Kumar Jul 07 '18 at 09:14
  • 1
    can u monitor your sql server with with sql server profiler? it will show you what queries are sent for witch database and what happens there. – yekanchi Jul 07 '18 at 09:21
  • +1 for the sql server profiler. It would show you all the execution depending on the request. On the other hand, your tblAttendance table might have a trigger of some sort. You might want to check that out. – rockin' Jul 07 '18 at 09:32
  • @VikramKumar The database is on a server. I created the tables using ssms – Naman Jul 07 '18 at 09:33
  • 1
    Are you certain that the function `btnMarkAtt_Click` and the click event of the button are tied together? – JayV Jul 07 '18 at 09:42
  • @JayV Yes, I checked that. They are bound together. – Naman Jul 07 '18 at 10:03
  • @yekanchi I checked sql server profiler. It doesn't show any new event when I press the button. – Naman Jul 07 '18 at 10:09
  • @rockin' Sql server profiler doesn't show that any new event is created. What do you mean by _a trigger of some sort_ ? – Naman Jul 07 '18 at 10:25
  • @Psyikik tables have "Triggers" such as "insert","update","delete". I thought it might be a good idea to check if there's an "insert" trigger that manipulates / removes the newly inserted row. But since the Profiler didn't show any events, my suggestion is obselete. – rockin' Jul 07 '18 at 10:28
  • 2
    @yekanchi: it's **which** - not "witch" (that's an old lady on a broomstick flying through the air .....) – marc_s Jul 07 '18 at 10:32
  • My guess is that, lblRegNo.Text is not parsable as an integer and the error is catched and silenced in your code. Can you try your code isolated in LinqPad, with a manually input RegNo.Text like "99". Code looks to be perfectly valid. – Cetin Basoz Jul 07 '18 at 10:58
  • Could it be as simple as btnMarkAtt_Click not being hooked up or not hooked up to the expected button? Try placing a breakpoint on top of that method to see that your method is being called.. – Adam G Jul 07 '18 at 11:34
  • I see several possibilities: 1) the eventhandler for the button click is not properly registered. Thus your code is not called at all. 2) Your DB connection is failing and the respective exception is caught somewhere else and not reported 3) lbRegNo.Text is not a valid integer and the respective exception is caught somewhere else. Try setting a breakpoint in your method and step through it line by line – derpirscher Jul 07 '18 at 12:46
  • _Nothing_ happens? Put a breakpoint on the very first line of your code and step through. – Nick.Mc Jul 07 '18 at 14:49

0 Answers0