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...