I have a function Like FetchData(query) in class Name InsertAndFetch:
public static FetchData(query)
{
da = new SqlDataAdapter();
dt = new DataTable();
dt.Clear();
Connectivity.openconnection();
cmd = new SqlCommand(query, Connectivity.cn);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
try
{
da.Fill(dt);
}
catch (SqlException e2)
{
MessageBox.Show(e2.Message);
}
}
Call for the function from 'SaleOrder' Class.
InsertAndFetch.FetchData("Select CustName from Customer where CId='Cus_1'");
txtcustname.Text=InsertAndFetch.dt.Rows[0][0].ToString();
Here Suppose By Mistake In query, the column was Typed CId instead of Actual column 'CustId', In definition of FetchData, SQLException will be thrown that Column 'CId' is not a valid column.
Now I want that the control should stop execution after this exception(shouldn't exit the app) and not move back to the Call of the Function in SaleOrder Class as It will cause an error ' No row at Index 0' while assigning value to txtcustname.