3
Answers

Insert data into sql sever

Yasmine Amin

Yasmine Amin

12y
13.9k
1
Hi all i have been struggling with this for a while now.. I'm trying to add a new user to a system but for some reason nothing is being inserted. Below is my code:
My ASP form

The form is a simple admin form with 2 textboxes and a button, the user must fill their username and a password then click on the button to insert their username and password into the database... the field IDs are usernameTxt and passwordTxt
 Form.aspx.cs
public class WebForm1 : System.Web.UI.Page
{


 protected System.Web.UI.WebControls.Button testBt;
 protected System.Web.UI.WebControls.Table addUserTb;
 protected System.Web.UI.WebControls.TextBox usernameTxt;
 protected System.Web.UI.WebControls.TextBox passwordTxt;
 private void addUserBt(object sender, System.EventArgs e)
 {
 //to define strings to hold values entered inside a form
 string the_username;
 string the_name;
 string the_pass;
 the_username = usernameTxt.Text;
 the_pass = passwordTxt.Text;
 //create an object to access dbconn.cs
 dbConn connectTodb = new dbConn();
 string cmd = "INSERT INTO users (username, password, permission, active) VALUES('" + the_username + "', '" + the_pass + "', 1, 1);";

 connectTodb.getConnected(cmd);
 Console.WriteLine("Record Inserted");
}//end of function


 My connection class
using System.Data.SqlTypes;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace firstForm
{

 public class dbConn  {
 public dbConn(){}
 public void getConnected(string cmds)
 {

 SqlConnection sqlConn = new SqlConnection("Data Source = (local);initial catalog=bugsDb; User Id =sa; Password = saadmin;");



 //Insert Command
 SqlCommand sqlCmd = new SqlCommand(cmds,sqlConn);
 try{
 sqlCmd.CommandText = cmds;
 sqlConn.Open();
 sqlCmd.ExecuteNonQuery();
 sqlConn.Close();
 }
 finally{
 sqlConn.Close();
 }
}//end function
}
}
 
Can anyone tell me why nothing is being inserted. I'm using a sql server 2005 express version..
thanks in advance
 
Answers (3)