Solved

Update Access with C# .NET, using OleDbCommand Parameters to Pass Checkbox Value

Posted on 2004-11-01
5,207 Views
Last Modified: 2008-02-07
I figure this is simple but I can't find the answer...I am updating an access database using ASP.NET C#.  The form has several check box controls that I want to update in the DB.  I am not sure how to create the OleDbCommand parameters for checkbox.  I think access checkbox values are -1, 0, and NULL but I can't figure out how to specify the datatype (if it is even necessary) and the parameter in the UPDATE Statement.

void updateRecord(Object Source, EventArgs E){
      //set connetction objects
      OleDbConnection objConn = new OleDbConnection(strConnection);
      OleDbCommand objCmd;
      OleDbDataReader objRdr;
      objCmd = new OleDbCommand("UPDATE omniforms SET name=@name, new=@newStatus, dayNight=@dayNight, billingStatus=@billingStatus, signatureStatus=@signatureStatus WHERE " + Request.QueryString["id"] + "", objConn);
      objCmd.Parameters.Add("@name", txtName.Text);
      objCmd.Parameters.Add("@newStatus", ddlNewStatus.SelectedItem.Value);
       objCmd.Parameters.DbType("@dayNight", ??????);  //checkbox named chkDayNight
      objCmd.Parameters.Add("@billingStatus", ddlBillingStatus.SelectedItem.Value);
      objCmd.Parameters.Add("@signatureStatus", ddlSignatureStatus.SelectedItem.Value);                  
      objConn.Open();
      objCmd.ExecuteNonQuery();
      objConn.Close();
}
0
Question by:drliebs
    17 Comments
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Access should be looking for True/false or Yes/No depending on how you set it up.

    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    you may be able to pass the index, but it is not neccesary.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    I usually handle it with something like this
     If Me.cbNewsletter.Checked Then
                        Session("Newsletter") = "True"
                    End If

    where cbNewsletter is my checkbox
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    and no you don't have to specify a datatype
    0
     

    Author Comment

    by:drliebs
    Can you fill in the blank or write out the correct code?

    objCmd.Parameters.DbType("@dayNight", ??????);  //checkbox named chkDayNight
    0
     
    LVL 17

    Accepted Solution

    by:
    If Me.chkDayNight.Checked Then
                        Session("DayNight") = "True" ' or yes if you defined it that way
                    Else
    Session("DayNight") = "False" ' or no if you defined it that way
                    End If

    objCmd.Parameters.DbType("@dayNight", Session("DayNight"));  //checkbox named chkDayNight

    Aeros
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Why did I get a 'C'?  Did you have any further questions?  Since you did not post them I am assuming no, and ask that you change my grade.

    Aeros
    0
     

    Author Comment

    by:drliebs
    Thanks, I ended up doing something a little different but you got me in the right direction, this is the C# code I ended up using

          if (chkDayNight.Checked){
          objCmd.Parameters.Add("@dayNight", true);
          }
          else{
          objCmd.Parameters.Add("@dayNight", false);      
          }
    0
     

    Author Comment

    by:drliebs
    I asked for C# code, I don't see any reason to use a variable and the code you posted was giving me errors.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    I would have clarified or attempted to help if you had asked.
    0
     

    Author Comment

    by:drliebs
    I don't mean to sound rude and I don't mean for a "C" grade to belittle you or effect your score, but the choice was "average" and I thought it was an average answer, an exceptional answer would have been more complete.  Thank you again, I hope that others will find the thread usefull.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Ask 10 Experts this question, and you'll get 10 different answers. One thing is certain: Grading at Experts Exchange is NOT like school. Here's what EE says:

        Although we use an A-D scale here at Experts Exchange, it works differently than, say, school grades. If one or more Experts' proposals are accepted as answers, they should usually be given an A or B grade, since they have taken the time to provide you with a working solution. If a possible solution is incomplete - ask for clarification or details before accepting the answer and grading it. People should not be given lower grades because of incorrect grammar or because you just accepted their answer or comment to close the question. Keep in mind, your question and any follow-up comments should be focused so that there can be a specific answer. The following is a good guideline to follow when grading:

            * A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.
            * B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.
            * C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

        Remember, the Expert helping you today is probably going to be helping you next time you post a question. Give them a fair chance to earn an 'Excellent!' grade and they'll provide you with some amazing support. It's also true that a "C" is the lowest grade you can give, and the Experts know that -- so use it judiciously.

    Only the Moderators and Page Editors have the choice to give a D grade. Beyond that, in a practical sense, the grading guidelines have "softened" a bit over the last year or two; one might expect that the majority of grades would be Bs (a standard "bell" curve), but the fact is that the culture of the site has caused there to be an inordinately high percentage of As. The Moderators have been instructed to ensure that the As they award are actually "Excellent" answers. Similarly, the C grade is the lowest that can be given by a member, a fact which should be kept in mind when grading as well.

    The use of a C in a vindictive manner is likely to be changed by a Moderator. You may not like the answer you get, and in some cases, and you may not like the way it is delivered, but if it is deemed to be accurate, no less than a B is an acceptable grade.
    0
     

    Author Comment

    by:drliebs
    Your post was fast, I can't always count on that and I was able to figure it out easy enough.  Does a "C" make you look bad on this forum? I will change it to a B if that is what you are concerned about.  I appreciated the help, I really do.
    0
     

    Author Comment

    by:drliebs
    With that in mind I appologize and a B would be more appropriate...now how do I correct this?
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Yes it does, and I was more than willing to help.  I understand that you feel it was not the best answer, but since you did not ask for further help or clarification I request you post a message here and asked for the grade to be changed to a B.

    http://www.experts-exchange.com/Community_Support/askQuestion.jsp
    0
     

    Author Comment

    by:drliebs
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Thank you so much my friend.

    Aeros
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now