Avatar billede sibga Nybegynder
12. februar 2012 - 03:26 Der er 7 kommentarer

Input string was not in a correct format"

Hello friends .. I need help after long time..

I'm trying to learn C# and trying to make a simple create student form...

I want to create students with the specifik data but the class where the student i'm going to add
I would like to chose this class from the combobox where im showing all classes from class table

my class combobox is showing all classes name so it works fine with showing classes by name.


Users table look like .. user_id, user_name, user_username, user_password and user_class_id
Class table look like .. class_id, class_name

Class table has all classes of school

Users table column user_class_id is a foreign key column which goes to class table's class_id

So basicly I want to chose a string value from combobox but as soon as I click on create user/student
It has to convert this string value as an int to the specifik class id...

but it shows error on this line ..

cl.setId = Convert.ToInt32(ucmanagement_cb_userclass.SelectedValue.ToString());

---------------------------------------------
Here is the Form Create user button code..

private void ucmanagement_btn_createuser_Click(object sender, System.EventArgs e)
        {
            // Creating a user with name added in the ucmanagement_tb_username
            Users use = new Users();
            DataSet ds = new DataSet();
            bool flag = true;

            use.setName = ucmanagement_tb_name.Text;
            use.setType = ucmanagement_cb_usertype.Text;
            use.setUserName = ucmanagement_tb_loginusername.Text;
            use.setPassword = ucmanagement_tb_loginpassword.Text;

            Cursor = Cursors.WaitCursor;

            // Looking for a class with the typed name if it exists set flag to false sa we do not create a new class
            use.searchUserLogin(ds, use.getUserName, "user_username");

            DataRowCollection drc = ds.Tables[0].Rows;

            if (drc.Count > 0)
            {
                flag = false;
            }
            // if flag is true create a class
            if (flag)
            {
                // Sets Class name and Id
                Class cl = new Class();
                cl.setId = Convert.ToInt32(ucmanagement_cb_userclass.SelectedValue.ToString());
                cl.setName = ucmanagement_cb_userclass.Text;
                use.setClas = cl;

                // Shows a dialog box with the entered
                string createtext = "Create user with :\n\nName: " + use.getName + "\n\nType: " + use.getType + "\n\nUsername: " + use.getUserName + "\n\nPassword: " + use.getPassword + "\n\nClass: " + use.getClass.getName + " ";
                DialogResult dl = MessageBox.Show(createtext, "Create User", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                switch (dl)
                {
                    case DialogResult.Yes:
                        use.createUser();
                        MessageBox.Show("There is created a new user.", "Information", MessageBoxButtons.OK);
                        ucmanagement_tb_name.Text = "Enter Name";
                        ucmanagement_cb_usertype.Text = "Chose Type";
                        ucmanagement_tb_loginusername.Text = "Enter UserName";
                        ucmanagement_tb_loginpassword.Text = "Enter Password";
                        ucmanagement_cb_userclass.Text = "Chose Class";
                        break;
                    case DialogResult.No:
                        break;
                }
            }
            else
            {
                MessageBox.Show("There has already created a user with user name " + use.getUserName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            Cursor = Cursors.Default;
        }
-----------------------------------
Here is the createUser class..

    public void createUser()
                {
                    // Creating Users to database
                    try
                    {
                        DataBase db = new DataBase();
                        // Sql statement to insert into the database
                        string sql = "";
                        sql = "INSERT INTO users VALUES(0,'" + getName + "','" + getUserName + "', SHA1('" + getPassword + "'),'" + getType + "',"+ getClass.getId + ")";

                        // Performing Query for inserting data into the database class
                        db.insertUpdateDeleteQuery(sql);
                    }
                    catch(Exception e)
                    {
                        // Showing messagebox with error message
                        string errortext = "";
                        errortext = e.Message.ToString();
                        MessageBox.Show(errortext,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
-----------------------------------
Here is the getClass class..

public void getClass(DataSet ds, int classId)
            {
                try
                {
                    // Getting Class with ID classId
                    DataBase db = new DataBase();
                   
                    // Sql statement to get from the database
                    string sql = "";
                    sql = "SELECT class_id AS ClassID, class_name AS ClassName FROM class WHERE class_id=" + classId;

                    // Here we retrieve the data out and putting into ds.
                    db.selectQuery(ds, sql);
                   
                    // DRC contains the extracted Rows
                    DataRowCollection drc = ds.Tables[0].Rows;

                    // We run all the Rows in the DRC through (the one who has come out) and set the attributes Id and Name
                    foreach(DataRow dr in drc)
                    {
                        setId = Convert.ToInt32(dr["ClassID"].ToString());
                        setName = dr["ClassName"].ToString();
                    }
                }
                catch(Exception e)
                {
                    // Showing messagebox with error message
                    string errortext = "";
                    errortext = e.Message.ToString();
                    MessageBox.Show(errortext,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }

please help me fast...
Avatar billede Broxigar Praktikant
12. februar 2012 - 11:13 #1
Instead of
cl.setId = Convert.ToInt32(ucmanagement_cb_userclass.SelectedValue.ToString());

Try:

cl.setId = Convert.ToInt32(ucmanagement_cb_userclass.SelectedItem.ToString());
Avatar billede Broxigar Praktikant
12. februar 2012 - 11:14 #2
From the official documents:

SelectedItem  Gets or sets currently selected item in the ComboBox.

SelectedValue  Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)
Avatar billede sibga Nybegynder
12. februar 2012 - 18:17 #3
I tried with that

cl.setId = Convert.ToInt32(ucmanagement_cb_userclass.SelectedItem.ToString());

but it giving same error .. :( please help ... there is some error by converting the string to int...
Avatar billede Broxigar Praktikant
12. februar 2012 - 18:21 #4
Could you do a Console.WriteLine(ucmanagement_cb_userclass.SelectedItem.ToString()); so we can see what the string contains?

Because if it contains letters or symbols other than 0-9 then the Convert.ToInt32() will fail.
Avatar billede sibga Nybegynder
12. februar 2012 - 18:52 #5
I'm new to C# so i dont know how to do console.writeline thing..

can u come on skype so i can send u the whole project its not so big so u can run and see the error it selv.. what u say??
Avatar billede Broxigar Praktikant
12. februar 2012 - 18:55 #6
Sure, my skype is anders.d.olsen.
Avatar billede sibga Nybegynder
12. februar 2012 - 19:07 #7
i have added u to skype u will get a invitation with name sibga.ashi
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester