hello,

Can u tell me how can i use try...catch in following coding.

This code is for save the data.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace savita
{
    public partial class Form5 : Form
    {
        DataTable dt;
        DataRow dr;

        public Form5()
        {
            InitializeComponent();
        }


dt = hosDataSet.Tables["inpatient"];
            dr = dt.NewRow();
            dr["ipid"] = textBox1.Text;
            dr["nm"] = textBox2.Text;
            dr["addr"] = textBox3.Text;
            dr["phno"] = textBox4.Text;
            dr["mobno"] = textBox5.Text;
            dr["age"] = textBox6.Text;
            dr["bld"] = comboBox1.Text;
            dr["adm"] = comboBox2.Text;
            dr["bed"] = comboBox3.Text;
            // dr["dt"] = dateTimePicker1.Text;
            DialogResult result = MessageBox.Show("You Want To Save Data", "Save", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                MessageBox.Show("The Data Is Saved", "save");
                dt.Rows.Add(dr);
                inpatientTableAdapter.Update(hosDataSet);
                this.inpatientTableAdapter.Fill(this.hosDataSet.inpatient);
            }
            if (result == DialogResult.No)
            {
                MessageBox.Show("The Data Is Not Saved", "Save");
            }

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            comboBox1.Text = "";
            comboBox2.Text = "";
            comboBox3.Text = "";

           }
}

Virusisfound

try this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace savita
{
public partial class Form5 : Form
{
DataTable dt;
DataRow dr;

public Form5()
{
InitializeComponent();
}

try
{
dt = hosDataSet.Tables["inpatient"];
dr = dt.NewRow();
dr["ipid"] = textBox1.Text;
dr["nm"] = textBox2.Text;
dr["addr"] = textBox3.Text;
dr["phno"] = textBox4.Text;
dr["mobno"] = textBox5.Text;
dr["age"] = textBox6.Text;
dr["bld"] = comboBox1.Text;
dr["adm"] = comboBox2.Text;
dr["bed"] = comboBox3.Text;
// dr["dt"] = dateTimePicker1.Text;
DialogResult result = MessageBox.Show("You Want To Save Data", "Save", MessageBoxButtons.YesNo);

if (result == DialogResult.Yes)
{
MessageBox.Show("The Data Is Saved", "save");
dt.Rows.Add(dr);
inpatientTableAdapter.Update(hosDataSet);
this.inpatientTableAdapter.Fill(this.hosDataSet.inpatient);
}
if (result == DialogResult.No)
{
MessageBox.Show("The Data Is Not Saved", "Save");
}
}
Catch(Exception e)
{
MessageBox.Show (E.Message)
}
Finally
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
}
}
}

The idea of TRY/CATCH/FINALLY is to use it when you know it's likely an exception will be thrown.

file read/writes
network connections
data type conversions or parsing
memory allocations
whatever else you think might throw an exception

Good coding habits include trying to reasonably account for as many runtime errors as you can think of.

Don't leave CATCH empty. Use it to at least provide a bit of debugging logic and have it display the error thrown.

Use FINALLY for any code that absolutely has to run, even if an error is thrown.

Thank You So much for showing me use of try and catch. now after this I will always use try and catch in my program. and keep practicing it its really very helpful


Once again thank both of u

mark thread solved if you expecting you get

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.