con.Open();
//stringquery=("insert into Tbl_EmployeeDetails values='"+txtName.Text+"','"+txtContact.Text+"','"+txtAddress.Text+"','"+txtEmployeeID.Text+"','"+txtJobLocation.Text+"','"+txtDateOfBirth.Text+"','"+Rdllist.selectedItem.Text+"'");
//sql command cmd=new sqlcommand(query,con);
//cmd.ExecuteNonquery();
SqlCommand cmd= new SqlCommand("Insert into Tbl_EmployeeDetails(Name,Address,Contact,Emp_ID,JobLocation,DateOfBirth,Gender)values(@Name,@Address,@Contact,@Emp_ID,@JobLocation,@DateOfBirth,@Gender)",con);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@Contact", txtContact.Text);
cmd.Parameters.AddWithValue("@Employee_ID", txtEmp_ID.Text);
cmd.Parameters.AddWithValue("@JobLocation", txtJobLocation.Text);
cmd.Parameters.AddWithValue("@DateOfBirth", txtDateOfBirth.Text);
cmd.Parameters.AddWithValue("@Gender", Rdllist.SelectedItem.Text);
cmd.ExecuteNonQuery();
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"alertMessage","alert('Record Inserted Successfully')",true);
txtName.Text= String.Empty;
txtAddress.Text= String.Empty;
txtContact.Text= String.Empty;
txtEmp_ID.Text= String.Empty;
txtJobLocation.Text= String.Empty;
txtDateOfBirth.Text= String.Empty;
// txtGender.Text= String.Empty;
con.Close();
}
}
答案 0 :(得分:0)
@Employee_ID应与@Emp_ID相同,与查询相同。
例如:
cmd.Parameters.AddWithValue("@Emp_ID", txtEmp_ID.Text);
因此请尝试:
con.Open();
SqlCommand cmd= new SqlCommand("Insert into Tbl_EmployeeDetails(Name,Address,Contact,Emp_ID,JobLocation,DateOfBirth,Gender)values(@Name,@Address,@Contact,@Emp_ID,@JobLocation,@DateOfBirth,@Gender)",con);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@Contact", txtContact.Text);
cmd.Parameters.AddWithValue("@Emp_ID", txtEmp_ID.Text);
cmd.Parameters.AddWithValue("@JobLocation", txtJobLocation.Text);
cmd.Parameters.AddWithValue("@DateOfBirth", txtDateOfBirth.Text);
cmd.Parameters.AddWithValue("@Gender", Rdllist.SelectedItem.Text);
cmd.ExecuteNonQuery();
ScriptManager.RegisterClientScriptBlock(this,this.GetType(),"alertMessage","alert('Record Inserted Successfully')",true);
txtName.Text= String.Empty;
txtAddress.Text= String.Empty;
txtContact.Text= String.Empty;
txtEmp_ID.Text= String.Empty;
txtJobLocation.Text= String.Empty;
txtDateOfBirth.Text= String.Empty;
// txtGender.Text= String.Empty;
con.Close();
}
}
答案 1 :(得分:0)
或者您可以将您的值更改为:
values(@Name,@Address,@Contact,@Employee_ID,@JobLocation,@DateOfBirth,@Gender)
因为你这样称呼它:
cmd.Parameters.AddWithValue("@Employee_ID", txtEmp_ID.Text);
或
values(@Name,@Address,@Contact,@Emp_ID,@JobLocation,@DateOfBirth,@Gender)
cmd.Parameters.AddWithValue("@Emp_ID", txtEmp_ID.Text);
将在cmd.parameters.addwithvalue中使用值中的名称。
谨防AddWithValue的陷阱:http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/