hi guys,

i have a piece of code i wrote in VB (login system) and it works great, but i want to have the same code in C# but i can't seem to get it to convert, does anyone know how to do this?

many thnaks

Just knowing the syntax of languages helps alot I guess.
Example: in C# an array can be defined as : int[] MyIntarray = new int[5]; in VB this can be MyIntarray = New Int(5) {}
MSDN is a good resource to help you out with this. Success! In trouble? Just Come back and pose a question in a new thread.

heres my code in VB

  For Each DataRow In dt.Rows                              'starts a loop to match entered text 'from textboxes' to the employees login details

            If tbox_Username.Text = DataRow.item(0) And tbox_Password.Text = DataRow(1) Then  'if login details are matched then username.tbox = first column in datarow, password = the second
                Position = DataRow(2)                                                         'position vairable = 3rd column
                Fname = DataRow(3)                                                            'Fname variable = 4th column
                Lname = DataRow(4)                                                            'Lname variable = 5th column
                con.Close()                                                                   'Closes connection
                Return True                                                                   'if all matched, returns as true
            End If                                                                            'ends the if statment
        Next                                                                                  'goes to next loop until conditions are met

        con.Close()                                                                           'closes connection
        Return False                                                                          ' if conditions cannot be met then returns false

but that doesnt work in C# :/ i can't even figure out what the syntax is lol

Assuming you've declared all the variables correctly

foreach(DataRow row in dt.Rows) {
    if (tbox_Username.Text == row.Item[0] && tbox_Password.Text == row.Item[1]) {
        Position = row.Item[2];
        Fname = row.Item[3];
        Lname = row.Item[4];
        con.Close();
        return true;
    }
}
con.Close();
return false;

But there are better ways of checking for valid login that don't involve loading every user into a datatabel (what if you have 10,000 users? Or 1,000,000?)

Edited 3 Years Ago by Momerath

yea i declared everything the same as i did in VB.net

but the code above doesn't work, it won't let me use 'item' keeps using Itemarray' instead

Hi mate,
ive sussed it, i was being a pleb (over thinking things again) heres the code i needed

public bool Login()
        {            
            //**************************//
            //creates a boolean function//
            //**************************//
            conn = new OleDbConnection(ConString);  //conn brings a new oledb connection
            conn.Open();                            //opens connection
            DataTable dt = new DataTable();         //creates new datatable
            DataSet ds = new DataSet();             //creates new dataset
            ds.Tables.Add(dt);                      //ready to populate the datatable with tables from the dataset

            //creates a new dataadapter and populates it with the SQL query (SELECT query) and links the connection
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM trustedEmployee", conn);

            da.Fill(dt);        //fills the datatable from the dataadapter

                //loop 'if' function//
                foreach (DataRow r in dt.Rows)
                {
                    // asks if the username and password textboxes match the rows from the datatable
                    if (r[0].ToString() == tbox_Username.Text && r[1].ToString() == tbox_Password.Text)
                    {
                        Position = r[2].ToString();         //fills the position variable with the 3rd coloum field (once login credentials are correct)
                        lbl_Position.Text = Position;       //fills the label with the position variable
                        return true;                        //returns true if credentials match
                    }
                }           

                return false;       //returns false if credentials don't match
This question has already been answered. Start a new discussion instead.