public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=JIMMY-PC;initial Catalog=jimmy;
Integrated Security=true");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Insert into Employee ('First Name','Middle Name',
'Last Name','D O B','Sex','Address')values (@FName,@MName,@LName,
@DOB,@Sex,@Address)", con);
{
cmd.Parameters.AddWithValue("@Fname",TextBox1.Text);
cmd.Parameters.AddWithValue("@Mname",TextBox2.Text);
cmd.Parameters.AddWithValue("@Lname",TextBox3.Text);
cmd.Parameters.AddWithValue("@DOB",TextBox4.Text);
cmd.Parameters.AddWithValue("@Sex", RadioButton1.SelectedItem);
cmd.Parameters.AddWithValue("@Address",TextBox5.Text);
con.Open();
//Response.Write("<script>alert('Registered successfully......!')</script>");
}
con.Close();
}
我是asp.net的新手,请帮帮我。 我尝试使用cmd.executenonquery,但有一些错误......我的连接字符串有什么问题吗?
答案 0 :(得分:0)
我们必须传递ListItem的selectedItem Text / Value。如果以下代码不适合您,请提供错误详细信息,并提及您调用此执行方法的位置。
cmd.Parameters.AddWithValue("@Sex", RadioButton1.SelectedItem.Text)
//OR
cmd.Parameters.AddWithValue("@Sex", RadioButton1.SelectedItem.Value)
答案 1 :(得分:0)
最好像这样更改你的SqlCommand:
SqlCommand cmd = new SqlCommand("Insert into Employee ([First Name],[Middle Name],[Last Name],[D O B],[Sex],[Address])
values (@FName,@MName,@LName,@DOB,@Sex,@Address)", con);
答案 2 :(得分:0)
con.Open();
string Title = ddl_title.SelectedValue.ToString();
string FirstName = txtname.Text;
string MiddleName = txtmiddlename.Text;
string LastName = txtlastname.Text;
string Gender = ddl_gender.SelectedValue.ToString();
string DateOfBirth = txtdateofbirth.Text;
string Age = txtage.Text;
string Cast = txtcast.Text;
string Address = txtaddress.Text;
string City = txtcity.Text;
string Area = txtarea.Text;
string PinCode = txtpincode.Text;
SqlCommand cmd = new SqlCommand("Insert Into AddNewMemberWomenExecutiveCommittee Values('"+Title+"','"+FirstName+"','"+MiddleName+"','"+LastName+"','"+Gender+"','"+DateOfBirth+"','"+Age+"','"+Cast+"','"+Address+"','"+City+"','"+Area+"','"+PinCode+"')", con);
int i = cmd.ExecuteNonQuery();
if (i == 1)
Response.Write("Insert Successfully");
con.Close();
GetData();
(1)con.open();//----->open the connection
(2)SqlCommand cmd= new SqlCommand("Query,con");//--->command object
(3)cmd.ExecuteNonQuery();----->function for insert, update, delete (4)con.close();-------->close the connnection, where con is connection object