How to catch when a return value is null in C#

  • Thread starter Thread starter IndigoMontoya
  • Start date Start date
I

IndigoMontoya

Guest
I am passing a parameter to sql server and returning an output param. There is a few instances where the output param is null, which throws an error in my code.


How do I check for a null return value?


This is the line of code that throws the error when null is returned

string rn = (string)sqlComm.Parameters["@rn"].Value;

private void Form2_Load(object sender, EventArgs e)
{
dtSN = dtAS.DefaultView.ToTable(true, "locale");

foreach (DataRow row in dtSN.Rows)
{
var locale = row["locale"] as string;
dataGridView1.DataSource = dtAS;
dataGridView1.AutoResizeColumns();
((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "locale = '" + locale + "'";
var csvString = dataGridView1.ForEachCell(wrapper => EscapeSpecialChars(wrapper, '&', '/')).ChangeRows(RemoveExtraData).ToCsvString();

csvString = dataGridView1.ToCsvString();

using (SqlConnection conn3 = new SqlConnection(StartupForm.ServerAddressToUse))
{
conn3.Open();
SqlCommand sqlComm = new SqlCommand("_GetInfo", conn3);
sqlComm.Parameters.AddWithValue("@sn", Convert.ToString(locale));

sqlComm.CommandType = CommandType.StoredProcedure;
SqlParameter retval = sqlComm.Parameters.Add("@rn", SqlDbType.VarChar);

retval.Direction = ParameterDirection.Output;

sqlComm.ExecuteNonQuery();
string rn = (string)sqlComm.Parameters["@rn"].Value;
conn3.Close();
conn3.Dispose();
}

this.Close();
}
}

Continue reading...
 
Back
Top