C# 未插入数据

C# 未插入数据,c#,asp.net,events,C#,Asp.net,Events,当我试图在按钮上将表单数据保存到sql数据库时,单击“数据”按钮不会保存到表中。这是我的web.config文件和代码隐藏文件 web.config: <connectionStrings> <add name="conn" connectionString="Data Source=admin-pc\SQLEXPRESS;database=abc;Integrated Security=True;User Instance=True" providerName="Sy

当我试图在按钮上将表单数据保存到
sql
数据库时,单击“数据”按钮不会保存到表中。这是我的
web.config
文件和代码隐藏文件

web.config:

<connectionStrings>
    <add name="conn" connectionString="Data Source=admin-pc\SQLEXPRESS;database=abc;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

在代码隐藏文件aspx.cs中,尝试以下代码:

public partial class registration : System.Web.UI.Page
{
    string con = ConfigurationManager.ConnectionStrings["conn"].ToString()
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtFistname.Text = "";
        txtlastname.Text = "";
        TextBox1.Text = "";
        txtEmail.Text = "";

        txtcontac.Text = "";
        txtPassword.Text = "";
        txtconPassword.Text = "";
        SqlCommand cmd = new SqlCommand("insert into reg(FName,LName,Gender,email,contact,password,conpasswd) values('" + txtFistname.Text + "','" + txtlastname.Text + "','" + TextBox1.Text + "','" + txtEmail.Text + "','" + txtcontac.Text + "','" + txtPassword.Text + "','" + txtconPassword.Text + "')", con);

        cmd.CommandType = CommandType.Text;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("Data inserted successfully");
        }

        catch (Exception ex)
        {
            Response.Write("Somethings goes wrong" + ex.Message);
        }
    }
}
public partial class registration : System.Web.UI.Page
{
    string con = ConfigurationManager.ConnectionStrings["conn"].ToString()
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        txtFistname.Text = "";
        txtlastname.Text = "";
        TextBox1.Text = "";
        txtEmail.Text = "";

        txtcontac.Text = "";
        txtPassword.Text = "";
        txtconPassword.Text = "";
        SqlCommand cmd = new SqlCommand("insert into reg(FName,LName,Gender,email,contact,password,conpasswd) values('" + txtFistname.Text + "','" + txtlastname.Text + "','" + TextBox1.Text + "','" + txtEmail.Text + "','" + txtcontac.Text + "','" + txtPassword.Text + "','" + txtconPassword.Text + "')", con);

        cmd.CommandType = CommandType.Text;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("Data inserted successfully");
        }

        catch (Exception ex)
        {
            Response.Write("Somethings goes wrong" + ex.Message);
        }
    }
}