locked
No value given for one or more required parameters. RRS feed

  • Question

  • User-269982159 posted
    public void write(bool write_face)
            {
                try
                {
                    OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
                    //OleDbConnection odcConnection = new OleDbConnection(Con);
                    Con.Open();
                    
                    if (write_face == false)
                    {
                        OleDbCommand Insert = new OleDbCommand ("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (win_user_name,DateTimeIn,DateTimeOut)",Con);
                        //OleDbCommand command = new OleDbCommand(command_str);
                        //command.Connection = odcConnection;
                        //command.ExecuteNonQuery();
                    }
                    else
                    {
                        //command_str = "INSERT INTO UserLogs " + "(WindowsUsername, DateTimeIn, DateTimeOut) VALUES ('" + win_user_name + "','" + DateTimeIn + "','" + DateTimeOut + "', @UserImage )";
                        OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (win_user_name,DateTimeIn,DateTimeOut,@UserImage)", Con);
                        FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                        Byte[] image_to_write = new byte[image_stream.Length];
                        image_stream.Read(image_to_write, 0, image_to_write.Length);
                        image_stream.Close();
    
                        OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);
                        //OleDbCommand command = new OleDbCommand(command_str);
                        //command.Connection = odcConnection;
                        Insert.Parameters.Add(oleP);
                        oleP.Value = image_to_write;
                        //command.Parameters.Add(oleP);
                        //command.ExecuteNonQuery();
                        Insert.ExecuteNonQuery();
                    }
                    Con.Close();
                    //odcConnection.Close();
                    eventLog1.WriteEntry("Insert log succeed!");
                }
                catch (OleDbException e)
                {
                    eventLog1.WriteEntry(e.Message);
                }
            }


     I have errors in my Insert statement...

     

    Monday, August 9, 2010 11:38 PM

Answers

  • User-1199946673 posted

    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (win_user_name,DateTimeIn,DateTimeOut,@UserImage)", Con);   
     

    The problem is very simple. You're entering 4 parameters:

    win_user_name
    DateTimeIn
    DateTimeOut
    @UserImage

    But you're only providing 1 parameter (@Userimage). Not only did you forgot to provide the first 3 parameters (whis is exactly what the error is saying), but since OleDb Parameters are recognized by position and not by their name, you try to enter the value of the UserImage into the WindowsUserName!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 10, 2010 3:13 AM
  • User-1199946673 posted

    OK, now we're on the right track Smile

    1. Since you're using OleAutomation Dates in the two Date parameters, can you confirm that you changed the datatype of the 2 field to datatime?
    2. You added a new field status. Make sure that the datatype in the database is yes/no (A boolean), please confirm? In the 2 commandTexts, you enter the value of this field. That means that it ain't a parameter, so you don't need to add a parameter for this field. And when the datatype is Yes/No, you need to remove the single quotes in the commandText (True instead of 'True')
    3. You're using the Add Method, however, I would advice to use the AddWithValue Method instead, which allows you to work with parameters in just 1 single line

    So, then it will be something like this:

    "INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,Status,UserImage) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, TRUE, @UserImage)"

    Insert.Parameters.AddWithValue("WindowsUsername", win_user_name);
    Insert.Parameters.AddWithValue("DateTimeIn", DateTime.Now.ToOADate());
    Insert.Parameters.AddWithValue("DateTimeOut", DateTime.Now.ToOADate());  
    <STRIKE>Insert.Parameters.Add("@Status", OleDbType.Char).Value = "TRUE";</STRIKE>
    Insert.Parameters.AddWithValue("UserImage", image_to_write);

    When this doesn't work, please specify which error you're getting, becaue you keep saying 'the same error', but in the course of this thread, you encountered different errors.

    If your error is still 'No value given for one or more required parameters.', please check if you didn't misspelled one (or more) fieldnames in the commandtext?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 11, 2010 11:48 AM

All replies

  • User-245554983 posted

    hi,

    please provide some more details like what error message are you getting ?

    thanks 

    Tuesday, August 10, 2010 12:57 AM
  • User-269982159 posted

    hey the error message is : No value given for one or more required parameters 

    Tuesday, August 10, 2010 2:00 AM
  • User-245554983 posted

    hi rickngjh , 

    It looks like in your database table you are not allowing nulls for some column and not passing value for it. Can you please check it ?

    Tuesday, August 10, 2010 2:20 AM
  • User-269982159 posted

    hey i have check ... but i dont knw where is the problem be ... i think is along the insert statement ... can u help me take alook

    thanks

    Tuesday, August 10, 2010 2:56 AM
  • User-245554983 posted

    hi,

    I have not used OleDbCommand but your code looks fine to me. Still it looks like issue is with your database table UserLogs.  Please check this table it looks like you have set some column not to accept null and from your c# code you are not passing value for that column and this is causing "No value given for one or more required parameters"

     

    Tuesday, August 10, 2010 3:05 AM
  • User-245554983 posted

    You can also place a break point and check what values are being passed to table from above pasted code, is there any null ? If no that again will indicate my above mentioned view. And if you found some null, handle it from code or modify your table accordingly.

    Let me know your finding.

     

    Tuesday, August 10, 2010 3:12 AM
  • User-1199946673 posted

    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (win_user_name,DateTimeIn,DateTimeOut,@UserImage)", Con);   
     

    The problem is very simple. You're entering 4 parameters:

    win_user_name
    DateTimeIn
    DateTimeOut
    @UserImage

    But you're only providing 1 parameter (@Userimage). Not only did you forgot to provide the first 3 parameters (whis is exactly what the error is saying), but since OleDb Parameters are recognized by position and not by their name, you try to enter the value of the UserImage into the WindowsUserName!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 10, 2010 3:13 AM
  • User-1199946673 posted

    One more thing that hasn't to do with your question:

  •                     Byte[] image_to_write = new byte[image_stream.Length];   
  •                     image_stream.Read(image_to_write, 0, image_to_write.Length);   

 

Since a bytearray is zero based, you should declare the length of the array  length - 1, (the first byte is stored at position 0, the second on 1, etc....)

  • Byte[] image_to_write = new byte[image_stream.Length-1];   
  • image_stream.Read(image_to_write, 0, image_to_write.Length);

    In most cases, this won't matter, but you'll get a problem when you save for example Office 2007 and try to retrieve them....

  • Tuesday, August 10, 2010 3:20 AM
  • User-1199946673 posted

    The problem is very simple. You're entering 4 parameters:

    win_user_name
    DateTimeIn
    DateTimeOut
    @UserImage

     

    Also,

    since the DateTimeIn and DateTimeOutr are named exactly like their columnname, you should place a @ in front of the parameters as well:

    "INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (@win_user_name,@DateTimeIn,@DateTimeOut,@UserImage

     

     

    Tuesday, August 10, 2010 3:24 AM
  • User-269982159 posted

    hey there will u mind typing the answer out .. instead of part by part .... cos i have done wat u told me too .. but the data still not insert into the table ... (:

     

    thanks alot ,,, n sorry for the trouble 

    Tuesday, August 10, 2010 3:50 AM
  • User-1199946673 posted

    hey there will u mind typing the answer out .. instead of part by part
     

    Sorry that I díd notice another mistake later on!

    cos i have done wat u told me too

    Really? Show your code?

    but the data still not insert into the table ...

    And what error do you get now? 

    Tuesday, August 10, 2010 4:00 AM
  • User-269982159 posted

    still the same error : no value given for one or more required parameters ... 

    nah is okay .. jus tt i got abit blur on wat to correct ..

    here is the code tt i have make changes to ...

    public void write(bool write_face)
            {
                try
                {
                    OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
                    
                    Con.Open();
                    
                    if (write_face == false)
                    {
                        OleDbCommand Insert = new OleDbCommand ("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (@win_user_name, @DateTimeIn, @DateTimeOut)",Con);
                        
                    }
                    else
                    {
                        OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (@win_user_name, @DateTimeIn, @DateTimeOut, @UserImage)", Con);
                        FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                        Byte[] image_to_write = new byte[image_stream.Length];
                        image_stream.Read(image_to_write, 0, image_to_write.Length);
                        image_stream.Close();
    
                        OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);
                                            
                        Insert.Parameters.Add(oleP);
                        oleP.Value = image_to_write;
                        Insert.ExecuteNonQuery();
                    }
                    Con.Close();
                    eventLog1.WriteEntry("Insert log succeed!");
                }
                catch (OleDbException e)
                {
                    eventLog1.WriteEntry(e.Message);
                }
            }


     

    Tuesday, August 10, 2010 4:11 AM
  • User1491597376 posted

    well one more thing................first you set the value and then add parameters to your oledbcommand.


    Mark as answer if it helps you.

    Tuesday, August 10, 2010 4:12 AM
  • User-1199946673 posted

    still the same error
     

    Off course, you only changed the name of the parameters, but you don't provide a value for the first 3 like I already stated in my first post!

    In line 25, you add the fourth parameter (also note the remark of the previous poster), before that, you need to provide the value for the first 3 and add them to the command....

    Read this:

    http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    Tuesday, August 10, 2010 4:23 AM
  • User-269982159 posted

    hey there .. i have add the parameter for the three ... but still the data is not insert the database ... pls help me take alook

     

     public void write(bool write_face)
            {
                try
                {
                    OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
                    Con.Open();
                    
                    if (write_face == false)
                    {
                        OleDbCommand Insert = new OleDbCommand ("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (@win_user_name, @DateTimeIn, @DateTimeOut)",Con);
                              
                    }
                    else
                    {
                        OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (@win_user_name, @DateTimeIn, @DateTimeOut, @UserImage)", Con);
                        FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                        Byte[] image_to_write = new byte[image_stream.Length];
                        image_stream.Read(image_to_write, 0, image_to_write.Length);
                        image_stream.Close();
    
                        OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);
                        Insert.Parameters.Add("WindowsUsername", OleDbType.Char).Value = win_user_name;
                        Insert.Parameters.Add("DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;
                        Insert.Parameters.Add("DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
                        Insert.Parameters.Add(oleP);
                        oleP.Value = image_to_write;
                        Insert.ExecuteNonQuery();
                    }
                    Con.Close();
                    eventLog1.WriteEntry("Insert log succeed!");
                }
                catch (OleDbException e)
                {
                    eventLog1.WriteEntry(e.Message);
                }
            }


     

    Tuesday, August 10, 2010 4:34 AM
  • User1491597376 posted

    hey add parameters inside if also............in your case..............u are not providing parameters if  controls go to the "if" statement.


    So add parameters inside if statemkent and everything will work.


    Mark as answer if it helps you.

    Tuesday, August 10, 2010 5:14 AM
  • User-1199946673 posted

     

    but still the data is not insert the database ...

    It would help if you tell us everytime you change something which error you're getting?

    well one more thing................first you set the value and then add parameters to your oledbcommand.
     

     This is still not solved, place line 16 before line 15!

    hey add parameters inside if also............in your case..............u are not providing parameters if  controls go to the "if" statement.
     

    Correct

    And the the other 3 values. Where do they come from, and do they have a value?

    Note the 2 date parameters. Access cannot deal with datetimes that contain milliseconds resulting in a 'Data type mismatch in criteria expression' Error. To avoid this, Convert datetimes into OleAutomation Dates:

    Insert.Parameters.Add("DateTimeIn", OleDbType.DBTime).Value = DateTimeIn.ToAODate();

    More information

    http://www.mikesdotnetting.com/Article/92/MS-Access-Date-and-Time-with-ASP.NET

    Tuesday, August 10, 2010 5:25 AM
  • User-269982159 posted

    hey eh i can add ToAODate(); to it .. it give me this error ...

    'string' does not contain a definition for ToAODate  and no extension method 'ToAODate' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

     

    here is my coding for the DATEIN and DATEOUT .. pls let me knw if i m wrong ... thanks alot (:

     string DateTimeIn = string.Format("{0:G}", DateTime.Now.ToString());
     string DateTimeOut = string.Format("{0:G}",DateTime.Now.ToString());
     

    Tuesday, August 10, 2010 5:41 AM
  • User1491597376 posted

    Did my suggestion helped you?

    well which database you are using?????????????


    Tuesday, August 10, 2010 5:46 AM
  • User-269982159 posted

    hey there .. i have add inside my if statement ... however is nt working ... here is my code ... pls help me see ...

     public void write(bool write_face)
            {
                OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
    
    
                if (write_face == false)
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (?, ?, ?)", Con);
                    Insert.Parameters.Add("WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("DateTimeIn", OleDbType.DBTime).Value = DateTimeIn.ToAo;
                    Insert.Parameters.Add("DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
                }
                else
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (?, ?, ?, ?)", Con);
                    FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                    Byte[] image_to_write = new byte[image_stream.Length];
                    image_stream.Read(image_to_write, 0, image_to_write.Length);
                    image_stream.Close();
    
                    OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);
                    Insert.Parameters.Add("WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;
                    Insert.Parameters.Add("DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
                    Insert.Parameters.Add(oleP);
                    oleP.Value = image_to_write;
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
    
                    }
                }
            }


     

     

     

     

     

    Tuesday, August 10, 2010 5:48 AM
  • User-269982159 posted

    hahah ... nope

    eh i using Microsoft Access Database   

    Tuesday, August 10, 2010 5:53 AM
  • User1491597376 posted

    change your if else  with this one


    1.  if (write_face == false)  
    2.            {  
    3.                OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut)", Con);  
    4.                Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;  
    5.                Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTimeIn.ToAo;  
    6.                Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTimeOut; 
    1.   try  
    2.                {  
    3.                    Con.Open();  
    4.                    int x = Insert.ExecuteNonQuery();  
    5.                    Con.Close();  
    6.                    eventLog1.WriteEntry("Insert log succeed!");  
    7.                }  
    8.                catch (OleDbException ex)  
    9.                {  
    10.                    eventLog1.WriteEntry(ex.Message);  
    11.   
    12.                }
    1.            }  
    2.            else  
    3.            {  
    4.                OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, @UserImage)", Con);  
    5.                FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);  
    6.   
    7.                Byte[] image_to_write = new byte[image_stream.Length];  
    8.                image_stream.Read(image_to_write, 0, image_to_write.Length);  
    9.                image_stream.Close();  
    10.   
    11.                OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);  
    12.                Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;  
    13.                Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;  
    14.                Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTimeOut; 
    15.  oleP.Value = image_to_write; 
    16.                Insert.Parameters.Add(oleP);  
    17.              
    18.   
    19.                try  
    20.                {  
    21.                    Con.Open();  
    22.                    int x = Insert.ExecuteNonQuery();  
    23.                    Con.Close();  
    24.                    eventLog1.WriteEntry("Insert log succeed!");  
    25.                }  
    26.                catch (OleDbException ex)  
    27.                {  
    28.                    eventLog1.WriteEntry(ex.Message);  
    29.   
    30.                }  
    31.            } 


    Please ignore the line number..................Mark as answer if it helps you


    Tuesday, August 10, 2010 5:57 AM
  • User-1199946673 posted

    here is my coding for the DATEIN and DATEOUT .. pls let me knw if i m wrong ... thanks alot (:

     string DateTimeIn = string.Format("{0:G}", DateTime.Now.ToString());
     string DateTimeOut = string.Format("{0:G}",DateTime.Now.ToString());

     

    Don't convert a datetime into a string if you want to insert a datetime!!!!!

     

    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTime.Now.ToOADate());
    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTime.Now.ToOADate());

    Tuesday, August 10, 2010 6:39 AM
  • User-1199946673 posted

    Did my suggestion helped you?

    Maybe you should read my posts also, because I think you completly missing what's going on!!!

    well which database you are using?????????????
     

    First of all, we're in the Access Forums. But even better, in the code you can see the connectionstring, which is pointing to a mdb file!!!!

    Tuesday, August 10, 2010 6:42 AM
  • User-269982159 posted

    hey i have change to the link that u have give me .. but i still receive the same error .. and information is not save inside database ....

    i have change my date n time no longer string .. which i have paste earlier ...

     

    here are my code ... ** note the bold is the part where i change

     

     public void write(bool write_face)
            {
                OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
    
    
                if (write_face == false)
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (?, ?, ?)", Con);
                    
                    Insert.Parameters.AddWithValue("DateTimeIn", DateTime.Now.AddHours(36).ToOADate());
                    Insert.Parameters.AddWithValue("DateTimeOut", DateTime.Now.AddHours(36).ToOADate());
                    Insert.Parameters.Add("WindowsUsername", OleDbType.Char).Value = win_user_name;
                }
                else
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,CapturedImage) VALUES (?, ?, ?, ?)", Con);
                    FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                    Byte[] image_to_write = new byte[image_stream.Length];
                    image_stream.Read(image_to_write, 0, image_to_write.Length);
                    image_stream.Close();
    
    
    
                    OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);
                    Insert.Parameters.Add("WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.AddWithValue("DateTimeIn", DateTime.Now.AddHours(36).ToOADate());
                    Insert.Parameters.AddWithValue("DateTimeOut", DateTime.Now.AddHours(36).ToOADate());
                    Insert.Parameters.Add(oleP);
                    oleP.Value = image_to_write;
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
    
                    }
                }
            }


     

    Tuesday, August 10, 2010 10:55 AM
  • User-1199946673 posted

     Once again, You still add the value to the oleP parameter AFTER you add it to the command! Why not always use AddWithValue?

    Also, in the if part, you add the parameters in the wrong order. You need to add them in the same order they appear in the SQL command, because OleDb Parameters are recognized by position, not by name.... (I mentioned this is my first replay already!!!!)

     

    Tuesday, August 10, 2010 12:28 PM
  • User-269982159 posted

    hey there sorry to trouble u .. however i kinda dont understand wat u mean ... if u dont mind cn copy n paste the part of the coding i should change and wat i have to change to? cos is kinda confusing ... thanks alot ... sorry to trouble u  

    Tuesday, August 10, 2010 9:02 PM
  • User1491597376 posted

    replace with my code.................after seeing your code i can say that you are still using your old if and else.


    Wednesday, August 11, 2010 1:08 AM
  • User-1199946673 posted

    hey there sorry to trouble u .. however i kinda dont understand wat u mean ... if u dont mind cn copy n paste the part of the coding i should change and wat i have to change to? cos is kinda confusing ... thanks alot ... sorry to trouble u  

     

    Confusing? I think I'm very clear...

    Once again, You still add the value to the oleP parameter AFTER you add it to the command!
     

    Take a look at your code:

  •     Insert.Parameters.Add(oleP);   
  •     oleP.Value = image_to_write;

  •  

    As already identified by dk_4_asp, you're adding a parameter without a value, because you set the value after you add it. Just change the order of these lines or.....

    Why not always use AddWithValue?

    Instead of changing the order, you can use AddWithValue rather than Add, by replacing this 2 lines with:

    Insert.Parameters.AddWithValue("userImage", image_to_write); 


     

    In that case, you can also skip this line:

    <STRIKE>OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary);</STRIKE>

    Also, in the if part, you add the parameters in the wrong order. You need to add them in the same order they appear in the SQL command, because OleDb Parameters are recognized by position, not by name.... (I mentioned this is my first replay already!!!!)

    This is very clear also. Your command is:

    INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut) VALUES (?, ?, ?)

    So the order of the parameters should be:

    WindowsUsername
    DateTimeIn
    DateTimeOut

    However, you insert the parameters as

    Insert.Parameters.AddWithValue("DateTimeIn", DateTime.Now.AddHours(36).ToOADate());   
    Insert.Parameters.AddWithValue("DateTimeOut", DateTime.Now.AddHours(36).ToOADate());
    Insert.Parameters.Add("WindowsUsername", OleDbType.Char).Value = win_user_name;


    The order is

    DateTimeIn
    DateTimeOut
    WindowsUsername

    Resulting in an error because you try to enter DateTimeIn (a Datetime) in the WindowsUsernameColumn (Text), DateTimeOut in the DateTimeIn Column and WindowsUserName (a string) into the DateTimeOut Column (a datetime)

    Once again, why not always use AddWithValue?

    Insert.Parameters.AddWithValue("WindowsUsername", win_user_name);
    Insert.Parameters.AddWithValue("DateTimeIn", DateTime.Now.AddHours(36).ToOADate());   
    Insert.Parameters.AddWithValue("DateTimeOut", DateTime.Now.AddHours(36).ToOADate());
    


    Finally, you seem to have ignored my other remark:

    One more thing that hasn't to do with your question:

  •                     Byte[] image_to_write = new byte[image_stream.Length];   
  •                     image_stream.Read(image_to_write, 0, image_to_write.Length);   

  •  

    Since a bytearray is zero based, you should declare the length of the array  length - 1, (the first byte is stored at position 0, the second on 1, etc....)

  • Byte[] image_to_write = new byte[image_stream.Length-1];   
  • image_stream.Read(image_to_write, 0, image_to_write.Length);

    In most cases, this won't matter, but you'll get a problem when you save for example Office 2007 and try to retrieve them....

  •  

     

    Wednesday, August 11, 2010 1:37 AM
  • User-269982159 posted

    hello hahah .. i have done what you told me too ... however is still the same error .... the data is still not insert in to it ... pls help ,,,, i will be pasting two coding ... one is the part tt i change the other one is the whole coding of mine ... maybe is somewhere tt is wrong .. dont mind help me take alook ... thanks alot ...

     

     

    [HERE IS THE INSERT PART]

    public void write(bool write_face)
            {
                OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
    
    
                if (write_face == false)
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs ([WindowsUsername], [DateTimeIn], [DateTimeOut],[Status]) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, Absent)", Con);
                    Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;
                    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
                    }
    
                }
                else
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,Status,UserImage) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, Presnt, @UserImage)", Con);
                    FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                    Byte[] image_to_write = new byte[image_stream.Length];
                    image_stream.Read(image_to_write, 0, image_to_write.Length);
                    image_stream.Close();
    
    
    
                    Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;
                    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
                    OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary, image_to_write.Length);
                    
                    oleP.Value = image_to_write;
                    Insert.Parameters.Add(oleP);
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
    
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
    
                    }
                }
            }


     

     

     

    [THIS IS MY WHOLE CODING]

     

    using System;
    using System.Windows.Forms;
    using System.Timers;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Data.OleDb;
    using Luxand;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Security.Permissions;
    
    
    namespace PatsWinService
    {
    
        public struct TFaceRecord
        {
            public byte[] Template; //Face Template;
            public FSDK.TFacePosition FacePosition;
            public FSDK.TPoint[] FacialFeatures; //Facial Features;
            public bool have_face;
    
            public string ImageFileName;
            public UInt32 ImageHandle; // FSDK Image Handle
            public IntPtr ImageBmp; //HBitMap
    
            public UInt32 FaceImageHandle;
            public IntPtr FaceImageBmp;
        }
    
        public partial class PatsWinService : ServiceBase
        {
    
            string Camera;
            [DllImport("gdi32.dll")]
            static extern bool DeleteObject(IntPtr hObject);
            static System.Timers.Timer timer;
            string sPath = ("C:\\Users\\hp\\Desktop\\PatsWinService\\PatsWinService\\bin\\Debug");
            public static float FaceDetectionThreshold = 3;
            public float Similarity = 0.0f;
            public float Threshold = 0.2f;
            
            
            
            public PatsWinService()
            {
                InitializeComponent();
            }
    
            
            public string path = "C:\\Users\\hp\\Desktop\\PatsWinService\\PatsWinService\\bin\\Debug\\Configuration.txt";
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
    
            //Testing 
            private string DateTimeIn;
            private string DateTimeOut;
            //private string capintcomboBoxText;
    
            //string pic_time = "";
            TFaceRecord face_taken;
            TFaceRecord face_from_database;
            string observe = "C:\\Users\\hp\\Desktop\\PatsWinService\\PatsWinService\\bin\\Debug\\";
            string tempFileName = "C:\\Users\\hp\\Desktop\\PatsWinService\\PatsWinService\\bin\\Debug\\";
            //string DateTimeIn = string.Format("{0:G}", DateTime.Now.ToString());
            //string DateTimeOut = string.Format("{0:G}",DateTime.Now.ToString());
            public string win_user_name;
            
            protected override void OnStart(string[] args)
            {
    
    
    
    
                init_FACE_SDK();
                init_timer();
                start_cam();
                shooting();
                read_username();
                
    
                FileStream fs = new FileStream(@"C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\bin\Debug\Config.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamReader m_streamReader = new StreamReader(fs);
                WritePrivateProfileString("Service", "ServiceStatus", "ON", path);
                WritePrivateProfileString("Service", "NeedRestart", "FALSE", path);
               
            }
    
            protected override void OnStop()
            {
                WritePrivateProfileString("Service", "ServiceStatus", "OFF", path);
                WritePrivateProfileString("Service", "ServiceStatus", "TRUE", path);
    
            }
    
            public void init_FACE_SDK()
            {
                if (FSDK.FSDKE_OK !=
                    FSDK.ActivateLibrary("36CEAA8FA2CFC51FA01D9DE0B654146FE7983CBD8F95CD568FA62C81445EA7276BB76E2E11AE198E0E6690BCE3F5307BE9CB3235344736EFD3E86B58C593D44D"))
                {
                    eventLog1.WriteEntry("SDK activation failed.");
                }
                if (FSDK.Initialize("") != FSDK.FSDKE_OK)
                {
                    eventLog1.WriteEntry("Error initializing SDK");
                }
            }
    
            public int start_cam()
            {
                string s = File.ReadAllText(dir);
                i = int.Parse(s);
                FSDKCam.InitializeCapturing();
    
                string[] cameraList;
                int size;
                FSDKCam.GetCameraList(out cameraList, out size);
    
                if (size == 0)
                {
                    eventLog1.WriteEntry("Please attach a camera");
                    return 1;
    
                }
    
                FSDKCam.VideoFormatInfo[] formatList;
                FSDKCam.GetVideoFormatList(cameraList[0], out formatList, out size);
                Camera = cameraList[0];
                return 0;
            }
            int i;
            string dir = "C:\\Users\\hp\\Desktop\\PatsWinService\\PatsWinService\\bin\\Debug\\Text.txt";
            public int shooting()
            {
                try
                {
                    int cameraHandle = 0;
    
                    int r = FSDKCam.OpenVideoCamera(Camera, ref cameraHandle);
                    if (r != FSDK.FSDKE_OK)
                    {
                        eventLog1.WriteEntry("Error opening the first camera");
                        return 1;
                    }
                    UInt32 imageHandle = 0;
                    if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle))
                    {
                        eventLog1.WriteEntry("Error grabbing the image");
                        return 1;
                    }
    
                    //pic_time = DateTime.Now.ToShortDateString() + "_"
                    //+ string.Format("{0:d2}", DateTime.Now.Hour.ToString()) + "_"
                    //+ string.Format("{0:d2}", DateTime.Now.Minute.ToString()) + "_"
                    //+ string.Format("{0:d2}", DateTime.Now.Second.ToString());
    
                    DateTimeIn = string.Format("{0:G}", DateTime.Now.ToString());
                    DateTimeOut = string.Format("{0:G}",DateTime.Now.ToString());
                    //tempFileName = System.DateTime.Now.Ticks.ToString();
    
    
    
                    if (FSDK.FSDKE_OK != FSDK.SaveImageToFile(imageHandle, tempFileName + win_user_name + i.ToString() + ".jpg"))
                    {
                        eventLog1.WriteEntry("error saving image");
                        return 1;
    
                    }
                    i++;
                    File.WriteAllText(dir, i.ToString());
                    face_taken = read_take(imageHandle);
                    FSDK.FreeImage(imageHandle);
                    GC.Collect();
    
                    FSDKCam.CloseVideoCamera(cameraHandle);
                    FSDKCam.FinalizeCapturing();
                }
                catch (Exception e)
                {
                    eventLog1.WriteEntry(e.Message);
                }
                return 0;
            }
    
            public void init_timer()
            {
                //StreamReader s = File.OpenText("C:\\Users\\crazy-stupid-boy\\Desktop\\zoe project\\PATS_084623Q\\PATS_084623Q\\bin\\Debug\\Config.txt");
                //string read = null;
                //while ((read = s.ReadLine()) != null)
                //{
                //  Console.WriteLine(read);
                //}
                //s.Close();
    
                string[] f = File.ReadAllLines("C:\\Users\\hp\\Documents\\Visual Studio 2008\\Projects\\PATS_084623Q\\PATS_084623Q\\bin\\Debug\\Config.txt");
                string str = f[5];
    
                File.WriteAllText("C:\\Users\\hp\\Documents\\Visual Studio 2008\\Projects\\PATS_084623Q\\PATS_084623Q\\bin\\Debug\\ConfigText.txt", str);
                //double capintcomboBox = Convert.ToString(RetrieveValueFromConfig("Capture Intervals"));
                //string str = Convert.ToDouble();
                double.Parse(str);
                double val = Convert.ToDouble(str);
                timer = new System.Timers.Timer();
                timer.Interval = val * 1000;
                timer.AutoReset = true;
                timer.Elapsed += new ElapsedEventHandler(timer1_Elasped);
                //timer.Interval = str.ToString;
                timer.Enabled = true;
                timer.Start();
    
            }
    
            public void timer1_Elasped(object sender, System.Timers.ElapsedEventArgs e)
            {
    
                if (start_cam() == 0)
                    if (shooting() == 0)
                    {
                        eventLog1.WriteEntry("Taking photo succeeded");
                    }
                    else
                        eventLog1.WriteEntry("Error taking");
    
            }
    
    
    
            public void readUser()
            {
                try
                {
                    string Con = @"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb";
                    OleDbConnection odcConnection = new OleDbConnection(Con);
                    odcConnection.Open();
    
                    OleDbCommand command = new OleDbCommand("SELECT [UserCapturedImage] FROM [RegisteredUserParticulars]" + "WHERE [WindowsUserName] = '" + win_user_name + "'");
    
                    command.Connection = odcConnection;
                    OleDbDataReader odr = command.ExecuteReader();
    
                    if (odr.Read())
                    {
                         
                        Byte[] image_to_read = (Byte[])odr.GetValue(0);
                        FileStream fs = new FileStream(sPath + win_user_name + "_face.jpg", FileMode.Create);
                        BinaryWriter bw = new BinaryWriter(fs);
                        bw.Write(image_to_read);
                        bw.Close();
                        fs.Close();
                    }
                    odcConnection.Close();
                    eventLog1.WriteEntry("Face Read OK!");
                }
                catch (OleDbException e)
                {
                    eventLog1.WriteEntry(e.Message);
                }
            }
    
            public TFaceRecord read_user_image()
            {
                TFaceRecord fr = new TFaceRecord();
                fr.ImageHandle = 0;
                fr.ImageBmp = (IntPtr)0;
                fr.FacePosition = new FSDK.TFacePosition();
                fr.FaceImageHandle = 0;
                fr.FaceImageBmp = (IntPtr)0;
                fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT];
                fr.Template = new byte[92480];
    
                try
                {
                    string _face = (sPath + win_user_name + "_face.jpg");
    
                    if (FSDK.LoadImageFromFile(ref fr.ImageHandle, _face) != FSDK.FSDKE_OK) 
                        eventLog1.WriteEntry("Error displaying image : " + _face);
    
                    if (FSDK.SaveImageToHBitmap(fr.ImageHandle, ref fr.ImageBmp) != FSDK.FSDKE_OK)
                        eventLog1.WriteEntry("Error displaying image!");
    
                    if (FSDK.DetectFace(fr.ImageHandle, ref fr.FacePosition) != FSDK.FSDKE_OK)
                        eventLog1.WriteEntry("No faces found");
                    else
                    {
                        FSDK.CreateEmptyImage(ref fr.FaceImageHandle);
                        FSDK.CopyRect(fr.ImageHandle, (int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)),
                            (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)),
                            (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)),
                            (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5)), fr.FaceImageHandle);
                        FSDK.SaveImageToHBitmap(fr.FaceImageHandle, ref fr.FaceImageBmp);
                        if (FSDK.DetectFacialFeaturesInRegion(fr.ImageHandle, ref fr.FacePosition, fr.FacialFeatures) != FSDK.FSDKE_OK)
                            eventLog1.WriteEntry("Error detecting facial features!");
                        else
                        {
                            if (FSDK.GetFaceTemplateUsingFeatures(fr.ImageHandle, fr.FacialFeatures, fr.Template) != FSDK.FSDKE_OK)
                                eventLog1.WriteEntry("Error retrieving face!");
                        }
                    }
                    eventLog1.WriteEntry("Face taking done!");
                }
                catch (Exception e)
                {
                    eventLog1.WriteEntry(e.Message);
                }
                DeleteObject(fr.ImageBmp);
                DeleteObject(fr.FaceImageBmp);
                FSDK.FreeImage(fr.ImageHandle);
                FSDK.FreeImage(fr.FaceImageHandle);
    
                return fr;
            }
    
            private TFaceRecord read_take(UInt32 imageHandle_in)
            {
                TFaceRecord fr = new TFaceRecord();
                fr.ImageHandle = imageHandle_in;
                fr.ImageBmp = (IntPtr)0;
                fr.FacePosition = new FSDK.TFacePosition();
                fr.FaceImageHandle =0;
                fr.FaceImageBmp = (IntPtr)0;
                fr.FacialFeatures = new FSDK.TPoint[FSDK.FSDK_FACIAL_FEATURE_COUNT];
                fr.Template = new byte [92480];
                fr.have_face = true;
    
                try
                {
                    FSDK.SetFaceDetectionParameters(false,true,384);
                    FSDK.SetFaceDetectionThreshold((int)FaceDetectionThreshold);
                    
                    if(FSDK.SaveImageToHBitmap(fr.ImageHandle, ref fr.ImageBmp) != FSDK.FSDKE_OK)
                        eventLog1.WriteEntry("Error Displaying Picture");
                    if(FSDK.DetectFace(fr.ImageHandle, ref fr.FacePosition) != FSDK.FSDKE_OK)
                    {
                        fr.have_face = false;
                        eventLog1.WriteEntry("No faces found");
                    }
                    else
                    {
                        FSDK.CreateEmptyImage(ref fr.FaceImageHandle);
                        FSDK.CopyRect(fr.ImageHandle,(int)(fr.FacePosition.xc - Math.Round(fr.FacePosition.w * 0.5)),
                            (int)(fr.FacePosition.yc - Math.Round(fr.FacePosition.w * 0.5)),
                            (int)(fr.FacePosition.xc + Math.Round(fr.FacePosition.w * 0.5)),
                            (int)(fr.FacePosition.yc + Math.Round(fr.FacePosition.w * 0.5)), fr.FaceImageHandle);
                        FSDK.SaveImageToHBitmap(fr.FaceImageHandle, ref fr.FaceImageBmp);
                        
                        FSDK.SaveImageToFile(fr.FaceImageHandle, tempFileName + "_face.jpg");
                        FSDK.SaveImageToFile(fr.FaceImageHandle,tempFileName + "temp_face.jpg");
                        
                        if(FSDK.DetectFacialFeaturesInRegion(fr.ImageHandle,ref fr.FacePosition,fr.FacialFeatures)!= FSDK.FSDKE_OK)
                            eventLog1.WriteEntry("Error detecting facial features");
                        else
                        {
                            if(FSDK.GetFaceTemplateUsingFeatures(fr.ImageHandle, fr.FacialFeatures,fr.Template)!= FSDK.FSDKE_OK)
                                eventLog1.WriteEntry("Error retrieving face templates");
                        }
                    }
                }
                catch(Exception e)
                {
                    eventLog1.WriteEntry("Cannot open image" + e.Message.ToString());
                }
                DeleteObject(fr.ImageBmp);
                DeleteObject(fr.FaceImageBmp);
                FSDK.FreeImage(fr.ImageHandle);
                FSDK.FreeImage(fr.FaceImageHandle);
                eventLog1.WriteEntry("Take face templates finish");
                return fr;
            }
    
            public void write(bool write_face)
            {
                OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
    
    
    
                if (write_face == false)
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs ([WindowsUsername], [DateTimeIn], [DateTimeOut],[Status]) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, Absent)", Con);
    
                    Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;
                    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
                    }
    
                }
                else
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,Status,UserImage) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, Presnt, @UserImage)", Con);
                    FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                    Byte[] image_to_write = new byte[image_stream.Length];
                    image_stream.Read(image_to_write, 0, image_to_write.Length);
                    image_stream.Close();
    
                    Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTimeIn;
                    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTimeOut;
                    OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary, image_to_write.Length);
    
                    
                    oleP.Value = image_to_write;
                    Insert.Parameters.Add(oleP);
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
    
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
    
                    }
                }
            }
    
    
            public bool match_face(byte[] right_face, byte[] check_face, ref float Similarity)
            {
                FSDK.MatchFaces(right_face, check_face, ref Similarity);
                if (Similarity >= Threshold)
                {
                    eventLog1.WriteEntry("face matched !");
                    eventLog1.WriteEntry("Similarity = " + (Similarity * 100).ToString());
                    return true;
    
                }
                else
                {
                    eventLog1.WriteEntry("Cannot matched the face!");
                    eventLog1.WriteEntry("Similarity = " + (Similarity * 100).ToString());
                    return false;
                }
            }
    
            public void check_attendence()
            {
                if (face_taken.have_face == true)
                {
                    if (match_face(face_from_database.Template, face_taken.Template, ref Similarity) == true)
                    {
                        write(true);
                        eventLog1.WriteEntry("attend");
                    }
                    else
                    {
                        write(false);
                        eventLog1.WriteEntry("absent");
                    }
                }
            }
    
    
            //[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
            //public void observe_1()
            //{
                //FileSystemWatcher watch = new FileSystemWatcher();
                //watch.Path = observe;
                //watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                //watch.Filter = "";
                //watch.Changed += new FileSystemEventHandler();
                //watch.EnableRaisingEvents = true;
                //eventLog1.WriteEntry("Observe start!");
            //}
    
            //DONT WANT ALREADY!!
            //private void OnChange(object source, FileSystemEventArgs e)
            //{
                //read_username();
                //StringBuilder value_Restart = new StringBuilder(500);
                //GetPrivateProfileString("Service", "NeedRestart", "", value_Restart, 500, path);
    
                //if (value_Restart.ToString() == "TRUE")
                //{
                    //WritePrivateProfileString("Service", "NeedRestart", "TRUE", path);
                    //eventLog1.WriteEntry("Service is going to be restart");
                //}
                //else
                //{
                    //WritePrivateProfileString("Service", "NeedRestart", "FALSE", path);
                //}
            //}
    
            //done OKAY!!
            public void read_username()
            {
                StringBuilder value_LoginName = new StringBuilder(500);
                StringBuilder value_HaveRegistered = new StringBuilder(500);
                GetPrivateProfileString("User", "LoginName", "", value_LoginName, 500, path);
                GetPrivateProfileString("User", "HaveRegistered", "", value_HaveRegistered, 500, path);
    
                if (value_HaveRegistered.ToString() == "TRUE")
                {
                    if (value_LoginName.ToString() != "")
                    {
                        eventLog1.WriteEntry("Login user have been register, camera monitoring start");
                        win_user_name = value_LoginName.ToString();
                        eventLog1.WriteEntry("LoginName is" + win_user_name);
                        readUser();
                        face_from_database = read_user_image();
                        init_timer();
                    }
                }
                else
                {
                    timer.Enabled = false;
                    eventLog1.WriteEntry("camera monitoring has stopped");
                }
            }
                        
    
    
    
    
    
    
                    
        }
    }
    
        
    
                
    
    
        
    
    

    THANKS ALOT !! FOR UR HELP

    Wednesday, August 11, 2010 2:03 AM
  • User-1199946673 posted

    i have done what you told me too
     

    No, You have not. Everytime you change something, you put back another error. Once again you're using a string for the Dates!!!!

    Also, you seem to totally ignore my remarks on the length of the Byte array....

    It would be nice if you would pay some more attention and try to understand what you're doing and not just copy and  paste, hope that it works and if not, return to the forum and ask help again. Start reading this thread form the beginning.

    however is still the same error .... the data is still not insert in to it

    And once again, when you return to the forum, be clear on the error you're getting, because in this thread, you encounterd two different errors....

    And off ocurse, if you get an error, there's no data in the database.

    Just to make sure, what datatypes you're using in the table? 

    Wednesday, August 11, 2010 2:12 AM
  • User1491597376 posted

    well well well 

    In your first code and inside if statement I cant see any parameter given for absent column...........insert some parameter for it.

    Wednesday, August 11, 2010 2:14 AM
  • User-269982159 posted

    if i don put it as a string... a error wil say tat datetimein and datetimeout is not in the project... i am using text for all except ole object for capturedimage..

    Wednesday, August 11, 2010 2:24 AM
  • User1491597376 posted

    just provide parameters for absent..............inside your if statement.

    Wednesday, August 11, 2010 2:27 AM
  • User-1199946673 posted

    if i don put it as a string... a error wil say tat datetimein and datetimeout is not in the project...
     

    I really can't follow you anymore. Like I Said, read this thread from the beginning and start understanding what you're doing, rather than copy and paste and hope that it will suddenly work!!!!!

    i am using text for all except ole object for capturedimage..

    You should use a DateTime DataType for the date fields....

    And are you sure they're not any more required fields in this table?????

    Wednesday, August 11, 2010 2:28 AM
  • User-1199946673 posted

    just provide parameters for absent..............inside your if statement.

     

    Although you're identifying some errors, you're totally ignoring many other problems that have been identified during this thread. So please stop asking him to copy and paste your code, because it doesn't solve the problems withfor example  the datetimes.

    Yes, you're right that all of a sudden an extra parameter is needed in the If part, but I think he's trying the Else part. If there's apparently a absent column in the table, if this is a required field then this value needs to be included in the 'else' command also and the parameter should be added there also!!!!

    Wednesday, August 11, 2010 2:36 AM
  • User-269982159 posted

    the prob here is i cant use datetimein and datetime out if i don declare them as a string... 

    Wednesday, August 11, 2010 2:39 AM
  • User1491597376 posted

    I know that I am slightly unaware of some thread part...................all I am doing is trying to help him to make a exception free web application.............I guess that wont be a problem for you.


    And I never asked him to paste his code over here.


    Wednesday, August 11, 2010 2:49 AM
  • User-269982159 posted

    i only hav tis error in insertin the records..after tis is done i wil be very grateful to both of u..so pls help me...i am v new to this C# programming..thx.. 

    Wednesday, August 11, 2010 3:07 AM
  • User-1199946673 posted

    ...................all I am doing is trying to help him to make a exception free web application.............I guess that wont be a problem for you.
     

    No that's fine by me, but it would help if you would also read my posts (like I do yours) and incorporate my remarks in your code also!

    And I never asked him to paste his code over here.

     

    Then you must be short of memory!

    replace with my code
     

    Wednesday, August 11, 2010 3:10 AM
  • User-1199946673 posted

    the prob here is i cant use datetimein and datetime out if i don declare them as a string... 
     

    I really don't understand?

    I showed you how to enter datetimes in a datetime field in the database. Why is this a problem?

    i only hav tis error in insertin the records..after tis is done i wil be very grateful to both of u..so pls help me...i am v new to this C# programming..thx.. 

     

    I think we're helping you a lot. It's up to you to read carefully an try to understand what you're told!

    Wednesday, August 11, 2010 3:14 AM
  • User1491597376 posted

    well i told him to replace his code by mine...................but never asked him to paste HIS codein this forum.

    Wednesday, August 11, 2010 3:14 AM
  • User-269982159 posted

    would u mind tellin me more specifically...lik i said i am very new to C# programming...thx alot.. 

    Wednesday, August 11, 2010 3:20 AM
  • User1491597376 posted


    The first and basic thing is that you should provide values to all the parameters.............as you have added new parameters to your if and else both query string.


    And secondly you should type cast values properly according to your column datatype

    Wednesday, August 11, 2010 3:32 AM
  • User-1199946673 posted

    mind tellin me more specifically...lik i said i am very new to C# programming...thx alot.. 
     

    I told you many times already. Start reading this complete thread all over. All the answers are in there more then one time!!!!!!

    Wednesday, August 11, 2010 3:39 AM
  • User-269982159 posted

    jux to double confirm wit u this are the things tat u ask me to change...

    <DIR>

    1. Byte[]image_to_write = new byte[image_stream.Length-1];

    2.image_stream.Read(image_to_write,0,image_to_write.Length);

    <DIR>

    3.("INSERT INTO UserLogs (WindowsUsername, DateTimeIn,DateTimeOut,CapturedImage) VALUES (@win_user_name,@DateTimeIn,@DateTimeOut,@UserImage)",Con);

    <DIR>

    4.*Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTime.Now.ToOADate());

    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTime.Now.ToOADate());

    </DIR>
    </DIR>

     

    </DIR>
    Wednesday, August 11, 2010 4:17 AM
  • User-269982159 posted

    hey .. i have change the following already ... and i have try to understand .. and change .. however i still having the same error ...

    this is wat i have change ... pls let me knw if i miss out anything ...

     public void write(bool write_face)
            {
                OleDbConnection Con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0; Data Source=C:\Users\hp\Documents\Visual Studio 2008\Projects\PATS_084623Q\PATS_084623Q\Database\PATS.mdb");
                //OleDbConnection odcConnection = new OleDbConnection(Con);
    
    
                if (write_face == false)
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs ([WindowsUsername], [DateTimeIn], [DateTimeOut],[Status]) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, 'FALSE')", Con);
    
                    Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTime.Now.ToOADate();
                    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTime.Now.ToOADate();
                    Insert.Parameters.Add("@Status", OleDbType.Char).Value = "FALSE";
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
                    }
    
                }
                else
                {
                    OleDbCommand Insert = new OleDbCommand("INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,Status,UserImage) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, 'TRUE', @UserImage)", Con);
                    FileStream image_stream = new FileStream(sPath + "\\temp_face.jpg", FileMode.Open, FileAccess.Read);
    
                    Byte[] image_to_write = new byte[image_stream.Length-1];
                    image_stream.Read(image_to_write, 0, image_to_write.Length);
                    image_stream.Close();
    
    
    
    
    
                    Insert.Parameters.Add("@WindowsUsername", OleDbType.Char).Value = win_user_name;
                    Insert.Parameters.Add("@DateTimeIn", OleDbType.DBTime).Value = DateTime.Now.ToOADate();
                    Insert.Parameters.Add("@DateTimeOut", OleDbType.DBTime).Value = DateTime.Now.ToOADate();
                    Insert.Parameters.Add("@Status", OleDbType.Char).Value = "TRUE";
                    OleDbParameter oleP = new OleDbParameter("@UserImage", OleDbType.LongVarBinary, image_to_write.Length);
    
                    
                    oleP.Value = image_to_write;
                    Insert.Parameters.Add(oleP);
    
    
                    try
                    {
                        Con.Open();
                        int x = Insert.ExecuteNonQuery();
                        Con.Close();
                        eventLog1.WriteEntry("Insert log succeed!");
    
                    }
                    catch (OleDbException ex)
                    {
                        eventLog1.WriteEntry(ex.Message);
    
                    }
                }
            }


     

    Wednesday, August 11, 2010 9:03 AM
  • User-1199946673 posted

    OK, now we're on the right track Smile

    1. Since you're using OleAutomation Dates in the two Date parameters, can you confirm that you changed the datatype of the 2 field to datatime?
    2. You added a new field status. Make sure that the datatype in the database is yes/no (A boolean), please confirm? In the 2 commandTexts, you enter the value of this field. That means that it ain't a parameter, so you don't need to add a parameter for this field. And when the datatype is Yes/No, you need to remove the single quotes in the commandText (True instead of 'True')
    3. You're using the Add Method, however, I would advice to use the AddWithValue Method instead, which allows you to work with parameters in just 1 single line

    So, then it will be something like this:

    "INSERT INTO UserLogs (WindowsUsername, DateTimeIn, DateTimeOut,Status,UserImage) VALUES (@WindowsUsername, @DateTimeIn, @DateTimeOut, TRUE, @UserImage)"

    Insert.Parameters.AddWithValue("WindowsUsername", win_user_name);
    Insert.Parameters.AddWithValue("DateTimeIn", DateTime.Now.ToOADate());
    Insert.Parameters.AddWithValue("DateTimeOut", DateTime.Now.ToOADate());  
    <STRIKE>Insert.Parameters.Add("@Status", OleDbType.Char).Value = "TRUE";</STRIKE>
    Insert.Parameters.AddWithValue("UserImage", image_to_write);

    When this doesn't work, please specify which error you're getting, becaue you keep saying 'the same error', but in the course of this thread, you encountered different errors.

    If your error is still 'No value given for one or more required parameters.', please check if you didn't misspelled one (or more) fieldnames in the commandtext?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 11, 2010 11:48 AM
  • User-269982159 posted

    1. yup i have confirm that i have change to datetime for my field for the two date parameters...

    2. i have change my status to datatype (yes/no)...

    3. i have change my .Add .. to .AddWithValue already ..

     

    happy to see some of the record is inserted inside the database ... however ... my status dont not show wheather a TRUE OR FALSE ... i jus show a checked box .. tt all ...

    thanks alot for the help

    Wednesday, August 11, 2010 9:53 PM
  • User-269982159 posted

    how do i make the records keep inserting into the database? 

    Thursday, August 12, 2010 2:58 AM
  • User1491597376 posted

    your status is not set in database because you are not passing any values for your status field.

    Thursday, August 12, 2010 3:06 AM
  • User-1199946673 posted

    my status dont not show wheather a TRUE OR FALSE ... i jus show a checked box
     

    A checked checkbox means True, if it is not checked it is false)

    your status is not set in database because you are not passing any values for your status field.
     

    PLEASE!!!! I explained already that no paremeter needs to be provided, because it is hardcoded in the SQL string, it's NOT a parameter. If this was the problem, he would still have the 'No value given for one or more required parameters' error, and no record would be inserted!!!!

    Thursday, August 12, 2010 3:48 AM
  • User-1199946673 posted

    how do i make the records keep inserting into the database? 
     

    I don't understand what you mean by this?

    Thursday, August 12, 2010 3:49 AM
  • User1491597376 posted

    hey m so sorry i dint see that.

    Thursday, August 12, 2010 3:53 AM
  • User-269982159 posted

    i mean..my service is abt taking attendance by a webcam device...and the time and date wil be recorded into the access database however my current service only record once and stopped..i want it to keep recording every interval...

    Thursday, August 12, 2010 4:57 AM
  • User-1199946673 posted

    i mean..my service is abt taking attendance by a webcam device...and the time and date wil be recorded into the access database however my current service only record once and stopped..i want it to keep recording every interval...

     

    This really has nothing to do with the original question, not even with database access. If I understand it correctly you want to perform this action in regular intervals. If you can; t figure this out, you need to ask a new question in a different forum, but I think this thread is solved if for now....

    Thursday, August 12, 2010 5:06 AM