Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help with some c# Code

  • 11-01-2008 11:57pm
    #1
    Registered Users, Registered Users 2 Posts: 325 ✭✭


    Hi

    I'm using VS 2005 and MySQL.

    I've connected to the database and I have a few RadioButtonLists.

    With this code only one of the Radio buttons records its value into the DB. When I mess around with it I can only get two records going into the DB rather than one record and the two values going into seperate columns.

    try
    {
    myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=project;User=root;Password=*****;");

    if (RadioButtonList1.SelectedValue != "")

    myConn.Open();

    string sql = "INSERT INTO lancasterhall (Location) VALUES ('" + RadioButtonList1.SelectedValue + "')";

    OdbcCommand cmd = new OdbcCommand(sql, myConn);
    cmd.ExecuteNonQuery();
    }


    Could someone let me know how to get it to enter 1 record with values from two different Radio buttons


Comments

  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    You're only referring to one value in the code.
    Try something like this:

    if(radiobuttonlist1.selectedvalue!="" && radiobuttonlist2.selectedvalue != "")
    ...
    string sql = "INSERT INTO lancasterhall (Location) VALUES ('" + RadioButtonList1.SelectedValue + "', '" + radiobuttonlist2.selectedvalue + "')";


  • Closed Accounts Posts: 413 ✭✭sobriquet


    doyler442 wrote: »
    only one of the Radio buttons records its value into the DB.
    That'll be because that's all this line does:
    string sql = "INSERT INTO lancasterhall (Location) VALUES ('" + RadioButtonList1.SelectedValue + "')";
    
    If you want to insert two values into a single row, you'd have to do something of the form
    "INSERT INTO TableName (Column1, Column2) VALUES (Value1, Value2)"
    
    where Value1 would be RadioButtonList1.SelectedValue and Value2 is the other piece of data you want to insert into Column2, maybe RadioButtonList2.SelectedValue?

    It's not terribly clear what you're trying to do, but hope that helps.

    Another problem could arise from where you're calling this code. If it's in the OnClick (or whatever equivalent) handler for each RadioButtonList, then you'll want to do each one on its' own, or if it's in the handler for a button click then do them all together.

    Hah, just seen pwds' post. You wait 16 hours and two replies come along at once.


  • Registered Users, Registered Users 2 Posts: 325 ✭✭doyler442


    Thank you so so much pwd - my problem was I couldnt find how I was to add the two together. I was trying things like + and , but I just tried your && and it works perfectly. I know stupid by me but unfor I've never really used this language so I'm finding some simple things hard and Google hasnt been particulary kind to me.


    Cheers also sobriquet for your help. Sorry I wasn't very clear but I made the thread late at night and I had been working on the prj for the day so I couldnt really write it very clear haha.


  • Registered Users, Registered Users 2 Posts: 325 ✭✭doyler442


    Can I just ask another quick question that someone might be able to help me with?


    I have another page where I have Textboxes and I want to do something similar as above but switch the radiobuttonlist1.selectedvalue for textbox1.text.

    Is textbox1.text the right bit of code or should it be something else as it doesn't seem to be working for me?


  • Closed Accounts Posts: 413 ✭✭sobriquet


    doyler442 wrote: »
    I have another page where I have Textboxes and I want to do something similar as above but switch the radiobuttonlist1.selectedvalue for textbox1.text.

    Is textbox1.text the right bit of code or should it be something else as it doesn't seem to be working for me?
    INSERT INTO TableName (ColumnName) VALUES ('" + textBox1.Text + "')
    
    That should work, so long as your TextBox is actually called textBox1. Wouldn't see why not. Perhaps that's not your actual problem and you've misled yourself.


  • Advertisement
Advertisement