This blog explains -
- How to use classes and inheritance.
- CRUD operations with listbox using databse.
- How to use split function.
- How to validate the textboxes using javascript.
It also covers the the following System Test question,
- Create a class for the math ( "+","-") operations -- name the class "MathOp".
- Extend the class using inheritance to include ("*","/") name the class "MathOp2".
- Create the following class/function when select a line from the list box, the user should be able to modify the values/operations and save it to the listbox/database Let’s say the user clicked on “1+3=4” , the values 1,3 should be displayed in the input text boxes.
Step 1: At DataBase.
I use the following table to demonstrate the above concepts.
- Create table MathRresults(
- ID int primary key identity(1,1),
- Result varchar(20)
- )
Application
Step 2: Creating the project.
Now create the project using the following.Go to Start, then All Programs and click Microsoft Visual Studio 2010.
Go to File, then click New, Project..., Visual C# , Web. Then select ASP.NET Empty Web Application.
Provide the project a name and specify the location.

Create the connection string in the Web.Config file as in the following code snippet:
- <connectionStrings>
- <add name="conStr"
- connectionString="Password= 1234; User ID=sa; Database=DB_Jai; Data Source=."
- providerName="System.Data.SqlClient"/>
- </connectionStrings>
Next: Right-click on Solution Explorer and add a web form to your project.

Step 4 : JavaScript Validations.
Use the following code to validate the textboxes.
- < script type = "text/javascript" > function Valid()
- {
- if (document.getElementById('<%=txtFirstNumber.ClientID%>').value.trim() == "") {
- var msg = document.getElementById('<%=lblMsg.ClientID %>');
- msg.innerHTML = "Please enter First Number";
- msg.style.color = "red";
- document.getElementById('<%=txtFirstNumber.ClientID%>').focus();
- return false;
- }
- if (document.getElementById('<%=txtSecondNumber.ClientID%>').value.trim() == "") {
- var msg = document.getElementById('<%=lblMsg.ClientID %>');
- msg.innerHTML = "Please enter Second Number";
- msg.style.color = "red";
- document.getElementById('<%=txtSecondNumber.ClientID%>').focus();
- return false;
- }
- }
- < /script>
CodeBehind
Add the following namespaces:
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
Invoke the ConnectionString from Web.Config as in the following:
- SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
- private Decimal Num1;
- private Decimal Num2;
- string constr = ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString;
MathOp class Contains two methods that are Add, Subtract.
MathOp2 class Contains two methods that are Mul, Div.
- public class MathOp
- {
- public Decimal Add(Decimal value1, Decimal value2)
- {
- return (value1 + value2);
- }
- public Decimal Subtract(Decimal value1, Decimal value2)
- {
- return (value1 - value2);
- }
- }
- public class MathOp2: MathOp
- {
- public Decimal Mul(Decimal value1, Decimal value2)
- {
- return (value1 * value2);
- }
- public Decimal Div(Decimal value1, Decimal value2)
- {
- return (value1 / value2);
- }
- }
- //Clear () method is used to clear the textbox box values.
- private void Clear()
- {
- txtFirstNumber.Text = string.Empty;
- txtSecondNumber.Text = string.Empty;
- lblResult.Text = string.Empty;
- lblMsg.Text = string.Empty;
- }
- //saveResult() method is used to save the result sets in database.
- private void saveResult()
- {
- SqlConnection con = new SqlConnection(constr);
- SqlCommand cmd = new SqlCommand("insert into MathRresults values('" + lblResult.Text + "')", con);
- SqlDataReader dbr;
- try
- {
- con.Open();
- dbr = cmd.ExecuteReader();
- lblMsg.Text = "Values saved Successfully";
- lblMsg.ForeColor = Color.Green;
- dbr.Read();
-
- }
- catch (Exception ex)
- {
- lblMsg.Text = "Failed to Save because :" + ex;
- lblMsg.ForeColor = Color.Red;
- }
- con.Close();
- BindResultsToListview();
- }
- // UpdateResult() method is used to update the result sets with existing records in database.
- private void UpdateResult()
- {
- int ? id = null;
- foreach(ListItem li in ListBox1.Items)
- {
- if (li.Selected == true)
- {
- id = Convert.ToInt32(ListBox1.SelectedValue);
- }
- }
- SqlConnection con = new SqlConnection(constr);
- SqlCommand cmd = new SqlCommand("Update MathRresults set Result = '" + lblResult.Text + "' Where ID = '" + id + "'", con);
- SqlDataReader dbr;
- try
- {
- con.Open();
- dbr = cmd.ExecuteReader();
- lblMsg.Text = "Values Updated Successfully";
- lblMsg.ForeColor = Color.Green;
- dbr.Read();
- //while (dbr.Read())
- //{
- //}
- }
- catch (Exception ex)
- {
- lblMsg.Text = "Failed to update because:" + ex;
- lblMsg.ForeColor = Color.Red;
- }
- finally
- {
- con.Close();
- BindResultsToListview();
- }
- }
- //BindResultsToListview Method is used to bind the operations to ListBox.
- private void BindResultsToListview()
- {
- Clear();
- SqlConnection con = new SqlConnection(constr);
- string cmd = "Select Id, Result from MathRresults";
- SqlDataAdapter adpt = new SqlDataAdapter(cmd, con);
- DataSet ds = new DataSet();
- adpt.Fill(ds, "MathRresults");
- DataTable myDataTable = ds.Tables[0];
- if (ds.Tables.Count > 0 && ds != null && ds.Tables[0].Rows.Count > 0) {
- ListBox1.DataSource = ds;
- ListBox1.DataValueField = "ID";
- ListBox1.DataTextField = "Result";
- ListBox1.DataBind();
- }
- }
- //DeleteFromListView is used to delete the selected item in ListBox.
- private int DeleteFromListView(int id)
- {
- //Here using statement is automatically close the connections
- using(SqlConnection con = new SqlConnection(constr))
- {
- SqlCommand cmd = new SqlCommand("delete from MathRresults where id = " + id + "", con);
- con.Open();
- cmd.ExecuteNonQuery();
- return 0;
- }
- }

Join the conversation! Your thoughts help the community grow.