当我尝试将表单数据保存到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
{
SqlConnection con = new SqlConnection("Data Source=admin-pc\SQLEXPRESS ; Database=abc; Integrated Security=true");
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();
}
catch (Exception ex)
{
Response.Write("Data inserted successfully");
}
}
}
答案 0 :(得分:0)
在你的代码隐藏文件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);
}
}
}