2017-03-25 5 views
-1

Wenn ich versuche, meine Formulardaten in sql Datenbank auf Knopfdruck zu speichern, ist Daten nicht in der Tabelle speichern. Hier ist meine web.config Datei und Code hinter der Datei.Daten werden nicht eingefügt

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> 

Hier mein Code hinter Datei ist 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"); 
     } 
    } 
} 

Antwort

0

Sie

Response.Write("Data inserted successfully"); 

in catch-Block schreiben .please es entfernen .

0

In Ihrem Code hinter Datei aspx.cs Datei versuchen, diesen Code:

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); 
     } 
    } 
} 
Verwandte Themen