Visible properties Essay

Custom Student Mr. Teacher ENG 1001-04 5 June 2017

Visible properties

In the properties of Label2, Label3, TextBox1, TextBox2, DropDownList1, DataGrid1, Button3 and Button5 we will change the Visible field to be false; later on we will change it back to true in the code. The idea behind this is that whenever the user presses a specific button, only the relevant fields will be visible. For instance, when the user presses the “add user” button, TextBox1, TextBox2 and Button3 appear while the rest of the objects remain invisible. In this web form we need to connect to the database and therefore we need to add data adapter as we did in webform1.

Follow the steps mentioned earlier and add a data adapter and a data set to this web form. It is recommended to use a new data set although it is ok to use the existing one. At the beginning of each function we will change the Visible properties of all the objects that will be used in this function to be visible and all the rest to be invisible. For example the start of the “add user“ function will be: TextBox1. Visible = true; TextBox2. Visible = true; Label2. Visible = true; Label3. Visible = true; Button3. Visible = true; DropDownList1. Visible = false; Button5. Visible = false;

DataGrid1. Visible = false; Those commands will reveal the text boxes, their labels and the add button, the rest of the objects will remain hidden. You can find the beginning of other functions in the added code. In order to be able to use the SQL functions we need to add this row to the top of the code page: using System. Data. SqlClient; We will start with the “add” button function. First let us define 3 strings: user_name, password and val. In the user_name variable we will save the user name typed by the user and in the password variable we will save the password typed by the user.

This we will do using these commands: user_name = TextBox1. Text. ToString(); password = TextBox2. Text. ToString(); Now before we continue let me remind you the SQL insert command syntax: INSERT INTO [table] (column1, column2,…) VALUES (‘value1’,…) To val string we will insert the strings of values using this line: val= “‘”+user_name +”‘, ‘”+ password +”‘”; Now we will define a new object called SqlCommand and we will initialize it with the SQL command that we wish to perform and the connection we are using as in the next command: SqlCommand insCommand = new SqlCommand(“INSERT INTO [user].

(userName, pass) VALUES (” + val + “)”,sqlConnection1); The next step is to open the connection, perform the operation and eventually close the connection as follows: insCommand. Connection. Open(); insCommand. ExecuteNonQuery(); insCommand. Connection. Close(); To finish the function we will clear the text boxes using those commands: TextBox1. Text=””; TextBox2. Text=””; Lets continue with the “remove user” button in this function we need to initialize the DropDownList values we will do that by selecting the just the user names from the database and then we will bind the data with the DropDownList.

Before we start with the code we need to go to the web form design area and select DropDownList1 object and update its properties in the following fields in that order: DataSource = dataSet21 DataMember = user DataTextField = userName DataTextField = userName Now lets get bake to the code, first we will clear the data set and the DropDownList: DropDownList1. Items. Clear(); dataSet21. Clear(); Then we need to select just the names form the database: sqlDataAdapter1. SelectCommand. CommandText = “SELECT userName FROM [user]”; sqlDataAdapter1. Fill(dataSet21);

Now we will bind the data set and the DropDownList: DropDownList1. DataSource = dataSet21; DropDownList1. DataBind(); DropDownList1. Visible = true; To finish this function we need to reset the data set: sqlDataAdapter1. SelectCommand = new SqlCommand( “SELECT * FROM [user]”,sqlConnection1); sqlDataAdapter1. Fill(dataSet21,”user”); The next function is the “remove” button function in this function we will delete the selected user name from the database. Again we will start with defining a string: user_name that will hold the user name that we wish to delete from the database.

This name will be received by the command: user_name = DropDownList1. SelectedItem. Text. ToString(); Now before we continue let me remind you the SQL delete command syntax: DELETE FROM [table] WHERE column1 = ‘value1’ Now we will define a new SqlCommand object and initialize it using this command: SqlCommand DelCommand = new SqlCommand(“DELETE FROM [user] WHERE userName = ‘”+ user_name + “‘”,sqlConnection1); And to perform the operation: DelCommand. Connection. Open(); DelCommand. ExecuteNonQuery(); DelCommand. Connection. Close();

Now we need to update the DropDownList so we will clear the data set and do the exact thing that we did in the “remove user button”: dataSet21. Clear(); sqlDataAdapter1. SelectCommand. CommandText = “SELECT userName FROM [user]”; sqlDataAdapter1. Fill(dataSet21); DropDownList1. Items. Clear(); DropDownList1. DataSource = dataSet21; DropDownList1. DataBind(); DropDownList1. Visible = true; sqlDataAdapter1. SelectCommand = new SqlCommand( “SELECT * FROM [user]”,sqlConnection1); sqlDataAdapter1. Fill(dataSet21,”user”); The last function is the “show all users” function.

In this function we will bind the data set that hold the entire table (users and passwords) with the DataGrid object. Again before we start with the code we need to go to the web form design area and select DropDownList1 object and update its properties in the following fields in that order: DataSource = dataSet21 DataMember = user DataKeyField = userName In the code the lines we need to add are: sqlDataAdapter1. SelectCommand = new SqlCommand( “SELECT * FROM [user]”,sqlConnection1); sqlDataAdapter1. Fill(dataSet21,”user”); DataGrid1. DataSource = dataSet21;

DataGrid1. DataBind(); DataGrid1. Visible=true; By doing all of the above we finished writing our small web application. In the following pages you can see the entire code as it should be. private void Button1_Click(object sender, System. EventArgs e) { // add user TextBox1. Visible = true; TextBox2. Visible = true; Label2. Visible = true; Label3. Visible = true; Button3. Visible = true; DropDownList1. Visible = false; Button5. Visible = false; DataGrid1. Visible = false; } private void Button2_Click(object sender, System. EventArgs e) { // remove user TextBox1.

Visible = false; TextBox2. Visible = false; Label2. Visible = false; Label3. Visible = false; Button3. Visible = false; DropDownList1. Visible = true; Button5. Visible = true; DataGrid1. Visible = false; DropDownList1. Items. Clear(); dataSet21. Clear(); sqlDataAdapter1. SelectCommand. CommandText = “SELECT userName FROM [user]”; sqlDataAdapter1. Fill(dataSet21); DropDownList1. DataSource = dataSet21; DropDownList1. DataBind(); DropDownList1. Visible = true; sqlDataAdapter1. SelectCommand = new SqlCommand( “SELECT * FROM [user]”,sqlConnection1); sqlDataAdapter1.

Fill(dataSet21,”user”); } private void Button4_Click(object sender, System. EventArgs e) { // show all users TextBox1. Visible = false; TextBox2. Visible = false; Label2. Visible = false; Label3. Visible = false; Button3. Visible = false; DropDownList1. Visible = false; Button5. Visible = false; sqlDataAdapter1. SelectCommand = new SqlCommand( “SELECT * FROM [user]”,sqlConnection1); sqlDataAdapter1. Fill(dataSet21,”user”); DataGrid1. DataSource = dataSet21; DataGrid1. DataBind(); DataGrid1. Visible=true; } private void Button3_Click(object sender, System. EventArgs e)

{//add button String user_name,password,val; user_name = TextBox1. Text. ToString(); password = TextBox2. Text. ToString(); val= “‘”+user_name +”‘, ‘”+ password +”‘”; SqlCommand insCommand = new SqlCommand( “INSERT INTO [user] (userName, pass) VALUES (” + val + “)”,sqlConnection1); insCommand. Connection. Open(); insCommand. ExecuteNonQuery(); insCommand. Connection. Close(); TextBox1. Text=””; TextBox2. Text=””; } private void Button5_Click(object sender, System. EventArgs e) { //remove button String user_name; user_name = DropDownList1. SelectedItem. Text. ToString();

SqlCommand DelCommand = new SqlCommand(“DELETE FROM [user] WHERE userName = ‘”+ user_name + “‘”,sqlConnection1); DelCommand. Connection. Open(); DelCommand. ExecuteNonQuery(); DelCommand. Connection. Close(); dataSet21. Clear(); sqlDataAdapter1. SelectCommand. CommandText = “SELECT userName FROM [user]”; sqlDataAdapter1. Fill(dataSet21); DropDownList1. Items. Clear(); DropDownList1. DataSource = dataSet21; DropDownList1. DataBind(); DropDownList1. Visible = true; sqlDataAdapter1. SelectCommand = new SqlCommand( “SELECT * FROM [user]”,sqlConnection1); sqlDataAdapter1. Fill(dataSet21,”user”); }.

Free Visible properties Essay Sample

A+

  • Subject:

  • University/College: University of California

  • Type of paper: Thesis/Dissertation Chapter

  • Date: 5 June 2017

  • Words:

  • Pages:

We will write a custom essay sample on Visible properties

for only $16.38 $13.9/page

your testimonials