good day,
Here is the whole code from the page, I hope its clear on your page.
public partial class EditUsers : System.Web.UI.Page
{
int intUserID = 0;
string strUserName = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
strUserName = "" + Session["name"].ToString();
//string strUserName = "";
}
else
{
strUserName = "";
}
if (!IsPostBack)
{
loadListbox();
}
}
private void loadListbox()
{
try
{
lboUsers.Items.Clear(); // .Items.Clear();
//string sql = "SELECT CompanyName from Company";
con.Open();
// String con = "Data Source=Webserver\\Webserver;Initial Catalog=IntelligentThinkingKeyGenerator;User ID=sa;Password=PH-Factor;";
//SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
//ConfigurationManager.ConnectionStrings["con"].ConnectionString);
//DataSet customers = new DataSet();
//'adapter.Fill(customers, "Customers");
SqlCommand com = new SqlCommand("sproc_UserSelectAll", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataReader dtr = com.ExecuteReader();
while (dtr.Read())
{
string name = (string)dtr["Username"];
int coid = (int)dtr["UserID"];
ListItem item = new ListItem(name);
item.Value = Convert.ToString(coid);
item.Text = name;
lboUsers.Items.Add(item);
lboUsers.DataBind();
//cboCompanyName.Items.Add("name");
//cboCompanyName.app
}
con.Close();
dtr.Close();
}
catch (Exception ex)
{
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
// Create the DataSet object
conn.Open();
//conn.Open();
SqlCommand cmdInsert = new SqlCommand("sproc_UserUpdate", conn);
cmdInsert.CommandType = CommandType.StoredProcedure;
//cmdInsert.Parameters.Add(new SqlParameter("@OrderId", SqlDbType.Int));
//cmdInsert.Parameters["@OrderId"].Direction = ParameterDirection.Output;
//cmdInsert.Parameters["@OrderId"].SourceColumn = "OrderId";
intUserID = Convert.ToInt32(lboUsers.SelectedItem.Value);
cmdInsert.Parameters.AddWithValue("@UserID", intUserID);
cmdInsert.Parameters.AddWithValue("@Username", tboUsername.Text);
cmdInsert.Parameters.AddWithValue("@Password", tboPassword.Text);
cmdInsert.Parameters.AddWithValue("@Firstname", tboFirstname1.Text);
cmdInsert.Parameters.AddWithValue("@Lastname", tboLastname.Text);
cmdInsert.Parameters.AddWithValue("@Address1", tboAddress1.Text);
cmdInsert.Parameters.AddWithValue("@Address2", tboAddress2.Text);
cmdInsert.Parameters.AddWithValue("@Address3", tboAddress3.Text);
cmdInsert.Parameters.AddWithValue("@Address4", "");
cmdInsert.Parameters.AddWithValue("@Postcode", tboPostcode.Text);
cmdInsert.Parameters.AddWithValue("@Telephone", tboTele.Text);
cmdInsert.Parameters.AddWithValue("@Mobile", tboMobile.Text);
cmdInsert.Parameters.AddWithValue("@Fax", tboFax.Text);
cmdInsert.Parameters.AddWithValue("@Email", tboEmail.Text);
// cmdInsert.Parameters.AddWithValue("@Isadmin",true);
cmdInsert.Parameters.AddWithValue("@DateLastUpdated", DateTime.Now);
cmdInsert.Parameters.AddWithValue("@Updatedby", strUserName);
cmdInsert.Parameters.AddWithValue("@Active", true);
cmdInsert.ExecuteNonQuery();
// Label5. = true;
lblInfo.Text = "User Updated Succesfully";
cmdInsert.Dispose();
conn.Close();
}
catch(Exception ex)
{
}
}
protected void btnClose_Click(object sender, EventArgs e)
{
this.tboAddress1.Text = "";
this.tboAddress2.Text = "";
this.tboAddress3.Text = "";
this.tboEmail.Text = "";
this.tboFax.Text = "";
this.tboFirstname1.Text = "";
this.tboLastname.Text = "";
this.tboMobile.Text = "";
this.tboPassword.Text = "";
this.tboPostcode.Text = "";
this.tboTele.Text = "";
this.tboUsername.Text = "";
}
protected void lboUsers_SelectedIndexChanged(object sender, EventArgs e)
{
intUserID = Convert.ToInt32(lboUsers.SelectedItem.Value);
try
{
this.tboAddress1.Text = "";
this.tboAddress2.Text = "";
this.tboAddress3.Text = "";
this.tboEmail.Text = "";
this.tboFax.Text = "";
this.tboFirstname1.Text = "";
this.tboLastname.Text = "";
this.tboMobile.Text = "";
this.tboPassword.Text = "";
this.tboPostcode.Text = "";
this.tboTele.Text = "";
this.tboUsername.Text = "";
lblInfo.Text = "";
//this.tboUsername.Text = Convert.ToString(chkActive.Checked);
if (intUserID != 0)
{
con.Open();
SqlCommand com = new SqlCommand("sproc_UserSelectForListbox", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@UserID", intUserID);
SqlDataReader dtr = com.ExecuteReader();
while (dtr.Read())
{ this.tboUsername.Text = "" + (string)dtr["Username"];
this.tboAddress1.Text = "" + (string)dtr["Address1"];
this.tboAddress2.Text = "" + (string)dtr["Address2"];
this.tboAddress3.Text = "" + (string)dtr["Address3"];
this.tboEmail.Text = "" + (string)dtr["email"];
this.tboFax.Text = "" + (string)dtr["fax"];
this.tboFirstname1.Text = "" + (string)dtr["Firstname"];
this.tboLastname.Text = "" +(string)dtr["LastName"];
this.tboMobile.Text = "" + (string)dtr["Mobile"];
this.tboPassword.Text = "" + (string)dtr["Password"];
this.tboPostcode.Text = "" + (string)dtr["PostCode"];
this.tboTele.Text = "" + (string)dtr["Telephone"];
this.tboUsername.Text = "" + (string)dtr["Username"];
//this.CheckBox1.Checked = (bool)dtr["checked"];
}
con.Close();
//if (tboLicence1.Text == "")
//{
// lblInformation1.Text = "No Key for Company";
//}
dtr.Dispose();
con.Close();
com.Dispose();
}
}
catch (Exception ex)
{
//
}
}
protected void chkActive1_CheckedChanged(object sender, EventArgs e)
{
}
}