Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have form and dataGridView connected to table in the database, when I add records It shows up in the dataGridView, but when I stop running the table returns empty!

here is the code:

        string employeeName =  "person";
        string employeeUserName ="person1";
        string emplpyeeNotes = "empty";
        string employeePassword = "123";
        int employeeSalary = 4000;
        try
        {

           SqlConnection cn = new SqlConnection(Properties.Settings.Default.StudentManagementDBConnectionString);
           cn.Open();

           string sql = "INSERT INTO Employee (Name,Salary,UserName,Password,Notes) VALUES(@name, @salary, @userName, @password, @notes)";
           SqlCommand exsql = new SqlCommand(sql, cn);
           exsql.Parameters.AddWithValue("@name", employeeName);
           exsql.Parameters.AddWithValue("@salary", employeeSalary);
           exsql.Parameters.AddWithValue("@userName", employeeUserName);
           exsql.Parameters.AddWithValue("@password", employeePassword);
           exsql.Parameters.AddWithValue("@notes", emplpyeeNotes);
           exsql.ExecuteNonQuery();

           MessageBox.Show("employee added Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
           this.employeeTableAdapter.Fill(this.studentManagementDBDataSet.Employee);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try using parameterized query like this instead:

string employeeName = textBoxName.Text;
string employeeUserName = textBoxUserName.Text;
string emplpyeeNotes = richTextBoxNotes.Text;
string employeePassword = textBoxPassword.Text;

SqlConnection cn = new SqlConnection(global::StudentManagementProject.Properties.Settings.Default.StudentManagementDBConnectionString);

    try
    {
        string sql = "INSERT INTO Employee (Name,Salary,UserName,Password,Notes) VALUES(@name, @salary, @userName, @password, @notes)";
        SqlCommand exsql = new SqlCommand(sql, cn);
        cn.Open();
        exsql.Parameters.AddWithValue("@name", employeeName);
        exsql.Parameters.AddWithValue("@salary", 2000);
        exsql.Parameters.AddWithValue("@userName", employeeUserName);
        exsql.Parameters.AddWithValue("@password", employeePassword);
        exsql.Parameters.AddWithValue("@notes", emplpyeeNotes);
        int result=exsql.ExecuteNonQuery();
        if (result > 0 )
        {
        MessageBox.Show("employee added Successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }else
        { 
          MessageBox.Show("employee insertion failed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        cn.Close();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...