Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnSave_Click(object sender, EventArgs e)
        {
           
            try
            {
                if (txtFirstName.Text == "")
                {
                    MessageBox.Show("Please enter First Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtFirstName.Focus();
                    return;
                }
                if (txtLastName.Text == "")
                {
                    MessageBox.Show("Please enter Last Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtLastName.Focus();
                    return;
                }
                //if (txtMobileNo.Text == "")
                //{
                  //  MessageBox.Show("Please enter mobile number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   // txtMobileNo.Focus();
                    //return;
               // }
                cc.con = new SqlConnection(cs.DBCon);
                cc.con.Open();
                string ct = "Select * from StaffMain where StaffCode=@d1";
                cc.cmd = new SqlCommand(ct);
                cc.cmd.Connection = cc.con;
                cc.cmd.Parameters.AddWithValue("@d1", txtStaffCode.Text);
                cc.rdr = cc.cmd.ExecuteReader();
                if (cc.rdr.Read()) 
                {
                    MessageBox.Show("Staff Code already exit", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtStaffCode.Text = "";
                    txtStaffCode.Focus();
                    if (cc.rdr != null) 
                    {
                        cc.con.Close();
                    }
                         return;
                }
                cc.con = new SqlConnection(cs.DBCon);
                cc.con.Open();
                string cb = ("insert into StaffMain(StaffCode,FirstName,LastName,FatherFname,FatherLname,CitizenNo,District,BankAcc,BankName,PermanentAddress,TempAddress,PhoneI,PhoneII,MobileNo,Email,Department,Designation,Groups,Status,Remarks)VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@12,@d13,@d14,@d15,@d16,@d17,@d18,@d19,@d20)");
                cc.cmd = new SqlCommand(cb);
                cc.cmd.Connection = cc.con;
                cc.cmd.Parameters.AddWithValue("@d1", txtStaffCode.Text);
                cc.cmd.Parameters.AddWithValue("@d2", txtFirstName.Text);
                cc.cmd.Parameters.AddWithValue("@d3", txtLastName.Text);
                cc.cmd.Parameters.AddWithValue("@d4", txtFatherFname.Text);
                cc.cmd.Parameters.AddWithValue("@d5", txtFatherLname.Text);
                cc.cmd.Parameters.AddWithValue("@d6", txtCitizen.Text);
                cc.cmd.Parameters.AddWithValue("@d7", cmbDistrict.Text);
                cc.cmd.Parameters.AddWithValue("@d8", txtBankAcc.Text);
                cc.cmd.Parameters.AddWithValue("@d9", cmbBankName.Text);
                cc.cmd.Parameters.AddWithValue("@d10", txtPermanentAddress.Text);
                cc.cmd.Parameters.AddWithValue("@d11", txtTemporaryAddress.Text);
                cc.cmd.Parameters.AddWithValue("@d12", txtPhoneI.Text);
                cc.cmd.Parameters.AddWithValue("@d13", txtPhoneII.Text);
                cc.cmd.Parameters.AddWithValue("@d14", txtMobileNo.Text);
                cc.cmd.Parameters.AddWithValue("@d15", txtEmailID.Text);
                cc.cmd.Parameters.AddWithValue("@d16", txtDepartment.Text);
                cc.cmd.Parameters.AddWithValue("@d17", txtDesiganation.Text);
                cc.cmd.Parameters.AddWithValue("@d18", txtGroup.Text);
                cc.cmd.Parameters.AddWithValue("@d19", cmbStatus.Text);
                cc.cmd.Parameters.AddWithValue("@d20", txtRemarks.Text);
                cc.cmd.ExecuteReader();
                cc.con.Close();
                st1 = lblUser.Text;
                st2 = "Added the new Staff having StaffCode'" + txtStaffCode.Text + "'";
                MessageBox.Show("Data Added Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            
        }

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

must declare the scalar variable
Posted
Updated 23-Jul-16 5:18am
v4

1 solution

Look at your code:
C#
string cb = ("insert into StaffMain(StaffCode,...,Remarks)VALUES(@d1,...,@d11,@12,@d13,...,@d20)");
...
cc.cmd.Parameters.AddWithValue("@d12", txtPhoneI.Text);
The names need to match:@12 and @d12 are not the same.
It's a better idea to name your parameters for the value:
Instead of "@d1", use "@SC" for "StaffCode".
Instead of "@d12", use "PHI" for "Phone I".
And so on - it's a lot more obvious when you read it.
 
Share this answer
 
Comments
Member 12649932 23-Jul-16 11:30am    
Thanks
OriginalGriff 23-Jul-16 11:40am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900