Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello,

I know my issue is one of the big issues of the type of program I am developing. I am currently trying (emphasis on TRYING) to develop my first C# program for my company which is a modernized time clock program (our current one was developed over 10 years ago and was built in Access, which we are trying to get away from), that writes to a SQL database on our server. I have the interface and the database built, and it functions. But the only thing it currently cannot do is write the information from the program to the database. Right now the interface allows the users to pull up their name, and their job (this may change) and press the clock in button which returns a dialog box with their name, job and current time as verification.

Where I am currently stuck is taking the name (which is setup using a ComboBox and a Data Source), which in the table utilizes the PKey as the link to the employee table (so the name has to push the PKey into the coding of the program for future steps), the job (same setup as the name) and add it to a record in the SQL Database with the current time.

There also is a Clock Out Button that does the same thing (except instead of Add, it uses update) where it searches for the PKey attached to the name, the PKey attached to the job and updates the record with a clock out time.

All the while, the Clock Out button looks for a record with an open Clock Out time, and if it doesn't find one, it errors out, telling them that they have performed an incorrect function. In addition the Clock In Button looks for any record for the employee and the job code that has a blank clock out time and tells them that the program is incapable of creating a new record because there is still an open one out there.

I need some help in trying to figure this out. (I have been coding in C# for about 3 months now.)

This is the code that I currently have and it errors out with:
The SQLParameterCollection only accepts non-null SQLParameter type objects, not string objects.

Which I believe on my sqlClkIn.Parameters.Add, I would have to add the data type that it is (nvarchar, int, bit). Is that correct?

I have posted my button code.

C#
<pre>        private void btnClockIn_Click(object sender, EventArgs e)
        {
            //Grab the Current Date and Time & Setup Strings for Return Message
            DateTime theDate;
            theDate = DateTime.Now;
            string strEmployee = cmbEmployee.GetItemText(cmbEmployee.SelectedItem);
            string strJob = cmbJobCode.GetItemText(cmbJobCode.SelectedItem);

            //Send the Current Date and Time to SQL Server
            SqlConnection objConnect = new SqlConnection(sqlConnect);

            string sqlClkin = "Insert Into tblTimeclock (tcEID, tcJID, tcClockIn) Values (@tcEID, @tcJID, @tcClockIn)";

            objConnect.Open();
            try
            {
                SqlCommand sqlClkIn = new SqlCommand(sqlClkin, objConnect);
                sqlClkIn.Parameters.Add("@tcEID, strEmployee");
                sqlClkIn.Parameters.Add("@tcJID, strJob");
                sqlClkIn.Parameters.Add("@tcClockIn, theDate");
                sqlClkIn.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                objConnect.Close();
            }
            //Display Message Box
            MessageBox.Show("Hello, " + strEmployee.ToString() + ".\n" + "You are clocking in at: " + theDate.ToString() + "." + "\nYou will be working job " + strJob.ToString() + ".", "Clock In", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
        }


Any help that can be provided would be greatly appreciated!

Thanks!
Posted
Comments
[no name] 24-Jun-13 15:40pm    
Try AddWithValue instead. And in your current code, you are missing double quotes after your parameter names on the call to Add.
OriginalGriff 24-Jun-13 15:57pm    
:OFixed...thank you! :thumbsup:
[no name] 24-Jun-13 16:03pm    
np, 5
RedDk 24-Jun-13 15:46pm    
... "type objects" ... "not string objects" ...
I think sqlClkIn.Parameters.Add("@tcEID, strEmployee") and the similar pre-execution input calls are not objects but strings. Try removing the parenthesises ... to make them "objects"? Probably all wet on this ...

Change it slightly:
C#
sqlClkIn.Parameters.AddWithValue("@tcEID", strEmployee);
sqlClkIn.Parameters.AddWithValue("@tcJID", strJob);
sqlClkIn.Parameters.AddWithValue("@tcClockIn", theDate);
SQL and the framework will sort out the correct datatype.

[edit]That'll teach me to copy 'n' paste from the OP - quotes fixed! :O - OriginalGriff[/edit]
 
Share this answer
 
v2
Make this:

C#
sqlClkIn.Parameters.Add("@tcEID, strEmployee");
sqlClkIn.Parameters.Add("@tcJID, strJob");
sqlClkIn.Parameters.Add("@tcClockIn, theDate");


into this:

C#
sqlClkIn.Parameters.Add("@tcEID", strEmployee);
sqlClkIn.Parameters.Add("@tcJID", strJob);
sqlClkIn.Parameters.Add("@tcClockIn", theDate);


The DbParameterCollection.Add() method expects both a parameter name and a value, and you were including the value within the parameter name string and passing an incorrect number of parameters.

The DbParameterCollection.AddWithValue() method will also work just as well, and is not flagged with ObsoleteAttribute. It's recommended to use AddWithValue instead of Add now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900