Hi all
I am currently learning asp .net and i'm struggling with it.
I am trying to create a admin.aspx form where administrators can go in and add new users to the system. bellow is my code
Admin.aspx.cs (code behind Add button)
private void addUserBt(object sender, System.EventArgs e)
{
//create an object to access dbconn.cs
dbConn connectTodb = new dbConn();
string cmd = "INSERT INTO users (username, password, name, permission, active) VALUES (usernameTxt, passwordTxt, nameTxt,,1)";
connectTodb.getConnected(cmd);
Console.WriteLine("Record Inserted");
}
now i have another class called dbConn where the connection happens
public void getConnected(string cmd)
{
string sqlString = @"Data Source = (local);initial catalog=Bugs; User Id =sa; Password = saadmin;";
SqlConnection sqlConn = new SqlConnection(sqlString);
sqlConn.Open();
SqlCommand sqlCmd = new SqlCommand(cmd);
SqlDataReader reader = sqlCmd.ExecuteReader();
}
everytime i run my app, nothing is inserted in the db
here are the columns in my db
users{user_id (auto, prmary key), username, password, name, permission, active}
cna anyone tell me where i'm going wrong
thanks in advance
Blocked AccountPosted Sep 5, 2008, 6:24 AM
Hi Yasmin,
Basically SqlDataReader is used to read the data from the database. Use ExecuteNonQuery() instead of ExecuteReader() , as Khan told u.
ArshadPosted Sep 5, 2008, 6:14 AM
i will get you one example
SqlSqlConnection con=new SqlConnection("keep your connctionString here");
SqlCommand cmd=new SqlCommand();
try
{
cmd.CommandText="Insert into emp(Id, Name) values("+strId+","+strName+")";
cmd.Connection=con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch(ex As Exception)
{
throw new Exception(ex.Message);
}
finally
{
if(con.open())
{
con.Close();
}
}
This is one way to insert the data or else you can use the DataAdapter,thats all
Thanks