C# Lab: We were assigned the lab to connect database with Windows form applicati
ID: 3691835 • Letter: C
Question
C# Lab:
We were assigned the lab to connect database with Windows form application and create some methods to perform certain tasks. Below is my code( but its not working , when i run the code , an error occurs in first line of LowestScore(() method(I have written it in bold ).The error that occurs is :An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll.
the purpose of the method is to compare all the three scores and display the lowest one in the new text box .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentTestScores_DetailView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void testsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.testsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.studentScoresDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'studentScoresDataSet.Tests' table. You can move, or remove it, as needed.
this.testsTableAdapter.Fill(this.studentScoresDataSet.Tests);
LowestScore();
}
private void LowestScore()
{
double lowestScore = Convert.ToDouble(textBox1.Text);
// textBox1.Text = lowestScore.ToString();
double average = Convert.ToDouble(textBox2.Text);
if (Convert.ToDouble(test1TextBox) < Convert.ToDouble(test2TextBox) && Convert.ToDouble(test1TextBox) < Convert.ToDouble(test3TextBox))
{
lowestScore = Convert.ToDouble(test1TextBox);
}
else if (Convert.ToDouble(test2TextBox) < Convert.ToDouble(test3TextBox) && Convert.ToDouble(test2TextBox) < Convert.ToDouble(test1TextBox))
{
lowestScore = Convert.ToDouble(test2TextBox);
}
else if (Convert.ToDouble(test3TextBox) < Convert.ToDouble(test1TextBox) && Convert.ToDouble(test3TextBox) < Convert.ToDouble(test2TextBox))
{
lowestScore = Convert.ToDouble(test3TextBox);
}
}
private void bindingNavigatorPositionItem_Click(object sender, EventArgs e)
{
LowestScore();
}
}
}
I cant figure out what am i doing wrong and need help with that.
Explanation / Answer
You are doing mistake in converting string value to double. Here instead of passing text you are passing object.
If the text boxes names are test1TextBox, test2TextBox,test3TextBox, then replace them in conversion method as
test1TextBox.Text, test2TextBox.Text,test3TextBox.Text
If the text boxes names are textBox1, textBox2,textBox3, then replace them in conversion method as
textBox1.Text, textBox2.Text,textBox3.Text ( I provided code with this assumption)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentTestScores_DetailView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void testsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.testsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.studentScoresDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'studentScoresDataSet.Tests' table. You can move, or remove it, as needed.
this.testsTableAdapter.Fill(this.studentScoresDataSet.Tests);
LowestScore();
}
private void LowestScore()
{
double lowestScore = Convert.ToDouble(textBox1.Text);
// textBox1.Text = lowestScore.ToString();
double average = Convert.ToDouble(textBox2.Text);
if (Convert.ToDouble(textBox1.Text) < Convert.ToDouble(textBox2.Text) && Convert.ToDouble(textBox1.Text) < Convert.ToDouble(textBox3.Text))
{
lowestScore = Convert.ToDouble(textBox1.Text);
}
else if (Convert.ToDouble(textBox2.Text) < Convert.ToDouble(textBox3.Text) && Convert.ToDouble(textBox2.Text) < Convert.ToDouble(textBox1.Text))
{
lowestScore = Convert.ToDouble(textBox2.Text);
}
else if (Convert.ToDouble(textBox3.Text) < Convert.ToDouble(textBox1.Text) && Convert.ToDouble(textBox3.Text) < Convert.ToDouble(textBox2.Text))
{
lowestScore = Convert.ToDouble(textBox3.Text);
}
}
private void bindingNavigatorPositionItem_Click(object sender, EventArgs e)
{
LowestScore();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.