rwheeler23
asked on
Proper placement VS C# 2008 Empty DataSet
I am working on this code and being new to VS C# I apparently do not understand proper error checking. If you search for --- in this code you will see where the problem is. The second SQL statement may very well be empty so I thought if I checked for that the Try Catch would not fire. I was wrong. It appears the error actually occurs with this statement: DataDataAdapter.Fill(DataD ataSet);. How do I adjust this code so the error message in the Try Catch will not fire.
namespace PPActivityDetail
{
public partial class frmPPBreakdown : DexUIForm
{
// Create a reference to the Cash Receipts Entry window
static RmCashReceiptsForm CashReceiptsForm = Dynamics.Forms.RmCashReceipts;
static RmCashReceiptsForm.RmCashReceiptsWindow RmCashReceiptsWindow = CashReceiptsForm.RmCashReceipts;
// Define connection string
string ConnectionString = "";
// Setup SQL Connection Handlers
private System.Data.SqlClient.SqlConnection DataConnection;
private System.Data.DataSet DataDataSet;
private System.Data.SqlClient.SqlCommand DataCommand;
private System.Data.SqlClient.SqlDataAdapter DataDataAdapter;
// Define variables to hold batch number and batch source
string BatchSource;
string BatchNumber;
public frmPPBreakdown()
{
InitializeComponent();
/* Assign values to text box fields */
txtBatchNumber.Text = RmCashReceiptsWindow.BatchNumber;
txtDocumentNumber.Text = RmCashReceiptsWindow.DocumentNumber;
txtCustomerNumber.Text = RmCashReceiptsWindow.CustomerNumber;
txtDocumentDate.Text = RmCashReceiptsWindow.DocumentDate.Value.ToString("MM/dd/yy");
/* Assign value for Batch Source and Batch Name */
BatchSource = RmCashReceiptsWindow.BatchSource;
BatchNumber = RmCashReceiptsWindow.BatchNumber;
try
{
SqlConnection DataConnection = GetConnection();
DataDataSet = new System.Data.DataSet();
DataDataSet.CaseSensitive = false;
DataCommand = new System.Data.SqlClient.SqlCommand();
DataCommand.Connection = DataConnection;
DataCommand.CommandText = "SELECT GLPOSTDT FROM SY00500 WHERE BCHSOURC = '" + BatchSource + "' AND BACHNUMB = '" + BatchNumber + "'";
DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
DataDataAdapter.SelectCommand = DataCommand;
DataDataAdapter.TableMappings.Add("Table", "POSTINGDATE");
DataDataAdapter.Fill(DataDataSet);
txtPostDate.Text = DataDataSet.Tables["POSTINGDATE"].Rows[0]["GLPOSTDT"].ToString();
if (txtPostDate.Text != "")
{
txtPostDate.Text = Convert.ToDateTime(txtPostDate.Text).ToString("MM/dd/yy");
}
DataCommand.CommandText = "SELECT JOBNUMBR,INVCNMBR,RCPTRCVD,RCPTDISC,PPREQSTD,PPPAID,PPRECOUP,SALSALES,SALFRGHT,SALGRPAID FROM rbsPPACTIVITY WHERE DOCNUMBR = '" + RmCashReceiptsWindow.DocumentNumber + "'";
DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
DataDataAdapter.SelectCommand = DataCommand;
DataDataAdapter.TableMappings.Add("Table", "JOBDATA");
DataDataAdapter.Fill(DataDataSet);
--- I THOUGHT THIS CHECK WOULD TAKE CARE OF THIS ---
if (DataDataSet.Tables.Count > 0)
{
txtJobNumber.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["JOBNUMBR"].ToString();
txtInvoiceNumber.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["INVCNMBR"].ToString();
txtReceiptsReceived.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["RCPTRCVD"].ToString();
txtReceiptsDiscounts.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["RCPTDISC"].ToString();
txtPPRequested.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["PPREQSTD"].ToString();
txtPPPaid.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["PPPAID"].ToString();
txtPPRecoup.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["PPRECOUP"].ToString();
txtSales.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["SALSALES"].ToString();
txtSalFreight.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["SALFRGHT"].ToString();
txtSalGRPaid.Text = DataDataSet.Tables["JOBDATA"].Rows[0]["SALGRPAID"].ToString();
txtReceiptsReceived.Text = Convert.ToDecimal(txtReceiptsReceived.Text).ToString("C");
txtReceiptsDiscounts.Text = Convert.ToDecimal(txtReceiptsDiscounts.Text).ToString("C");
txtPPRequested.Text = Convert.ToDecimal(txtPPRequested.Text).ToString("C");
txtPPPaid.Text = Convert.ToDecimal(txtPPPaid.Text).ToString("C");
txtPPRecoup.Text = Convert.ToDecimal(txtPPRecoup.Text).ToString("C");
txtSales.Text = Convert.ToDecimal(txtSales.Text).ToString("C");
txtSalFreight.Text = Convert.ToDecimal(txtSalFreight.Text).ToString("C");
txtSalGRPaid.Text = Convert.ToDecimal(txtSalGRPaid.Text).ToString("C");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
}
private SqlConnection GetConnection()
{
// Create reader and open file containing connection string
TextReader tr = new StreamReader(@"C:\Program Files\Microsoft Dynamics\GP\Addins\GSE.INI");
// Read line containing the connection string
ConnectionString = tr.ReadLine();
// Close the stream
tr.Close();
DataConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
DataConnection.Open();
return DataConnection;
}
private void frmPPBreakdown_Load(object sender, EventArgs e)
{
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window