Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have taken parameters for a command and assigning gridview rows as values.When the grid view row is null,it shows error 'Object Reference not set to an instance'.I know how to assign values using variable in try block.But i want to give like this.Is there any method to convert that values to empty strings in C#.

What I have tried:

C#
cmdTemp.Parameters.AddWithValue("@comment_NAME30", row.Cells[0].Value.ToString());
cmdTemp.Parameters.AddWithValue("PRODUCT_NO", row.Cells[1].Value.ToString());
cmdTemp.Parameters.AddWithValue("@PRODUCT_NAME", row.Cells[2].Value.ToString());
cmdTemp.Parameters.AddWithValue("@QTY", row.Cells[3].Value.ToString());
cmdTemp.Parameters.AddWithValue("@UOM", row.Cells[4].Value.ToString());
cmdTemp.Parameters.AddWithValue("@PRICE", row.Cells[5].Value.ToString());
cmdTemp.Parameters.AddWithValue("@ITEM1_NO", row.Cells[6].Value.ToString());
cmdTemp.Parameters.AddWithValue("@DISCOUNT_PC", row.Cells[7].Value.ToString());
cmdTemp.Parameters.AddWithValue("@AMOUNT", row.Cells[8].Value.ToString());
cmdTemp.Parameters.AddWithValue("@SC_NO", row.Cells[10].Value.ToString());
cmdTemp.Parameters.AddWithValue("@GL_CODE", row.Cells[11].Value.ToString());
cmdTemp.Parameters.AddWithValue("@PROJ_NO", row.Cells[12].Value.ToString());
cmdTemp.Parameters.AddWithValue("@Gp_NO", row.Cells[13].Value.ToString());
cmdTemp.Parameters.AddWithValue("@WH_NO", row.Cells[14].Value.ToString());
cmdTemp.Parameters.AddWithValue("@Sales_exempt", row.Cells[15].Value.ToString());
cmdTemp.Parameters.AddWithValue("@FACTOR", row.Cells[16].Value.ToString());
cmdTemp.Parameters.AddWithValue("@Category", row.Cells[18].Value.ToString());
cmdTemp.Parameters.AddWithValue("@IMPORTED_FROM_NO", row.Cells[19].Value.ToString());
cmdTemp.Parameters.AddWithValue("@IMPORTED_FROM", row.Cells[20].Value.ToString());
cmdTemp.Parameters.AddWithValue("@GST_Amt", row.Cells[23].Value.ToString());
cmdTemp.Parameters.AddWithValue("@DISCOUNT_PC_2", row.Cells[24].Value.ToString());
cmdTemp.Parameters.AddWithValue("@Ref_No", row.Cells[25].Value.ToString());
cmdTemp.Parameters.AddWithValue("@GL_DESC", row.Cells[27].Value.ToString());
cmdTemp.Parameters.AddWithValue("@PRODUCT_NAME_LONG", cfs.singlequotconver(strdesc));
cmdTemp.Parameters.AddWithValue("@CREATED_BY", uid.ToString());
cmdTemp.Parameters.AddWithValue("@DATE_CREATED", cfs.sqldateconverion());
cmdTemp.Parameters.AddWithValue("@Deleted", "False");
cmdTemp.Parameters.AddWithValue("@INV_NO", ddl_Inv.Text);
cmdTemp.Parameters.AddWithValue("@Transfer", "U");
cmdTemp.Parameters.AddWithValue("@TICK", "C");
cmdTemp.Parameters.AddWithValue("@ID", strInvId);
cmdTemp.Parameters.AddWithValue("@AC_NO", cfs.singlequotconver(txt_Custcode.Text));
cmdTemp.Parameters.AddWithValue("@AC_NAME", cfs.singlequotconver(txt_Cus_Name.Text));
cmdTemp.Parameters.AddWithValue("@ITEM10", cfs.singlequotconver(LineNo));
cmdTemp.Parameters.AddWithValue("@invNo_user", ddl_Inv.Text + uid.ToString());
cmdTemp.Parameters.AddWithValue("@AVERAGE_COST", cfs.get_data("select distinct AVERAGE_COST from PRODUCT where PRODUCT_NO='" + strpcode + "'"));
cmdTemp.Parameters.AddWithValue("@LP_COST", cfs.get_data("select distinct LP_COST from PRODUCT where PRODUCT_NO='" + strpcode + "'"));
cmdTemp.Parameters.AddWithValue("@STD_COST", cfs.get_data("select distinct STD_COST from PRODUCT where PRODUCT_NO='" + strpcode + "'"));
cmdTemp.Parameters.AddWithValue("@QTY_UNFILLED", strqtyfilled);
cmdTemp.Parameters.AddWithValue("@DATETIME", cfs.sqldateconverion(txt_trans_Date.Value));
cmdTemp.Parameters.AddWithValue("@Batch_No", batchno);
cmdTemp.Parameters.AddWithValue("@Expiry_Date", Expirydate);
cmdTemp.Parameters.AddWithValue("@PRINT_ITEM", printitem);
cmdTemp.Parameters.AddWithValue("@QTY_BILLED", Prodqty);
cmdTemp.Parameters.AddWithValue("@ITEM6_NO", Prodqty);
cmdTemp.Parameters.AddWithValue("@ITEM7_NO", pri);
cmdTemp.Parameters.AddWithValue("@ITEM8_NO", net);
Posted
Updated 21-Nov-16 2:39am
v2

The new ?? operator is available for this type of operation. If the value on the left is null, it will return the value on the right. There is also a similar ?. operator, which does not call the property on the right if the value on the left is null, but returns null instead.

So, if you are sure that the input variable is a string, you can just pass
C#
string result = value ?? "";

If it is an unknown type object
C#
string result = value?.ToString() ?? "";
 
Share this answer
 
Use the null-coalescing operator:
C#
private string AlwaysAString(object o)
    {
    return o.ToString() ?? "";
    }

[edit]Bah! No, that'll throw a null reference exception...
C#
private string AlwaysAString(object o)
    {
    if (o != null) return o.ToString();
    return "";
    }

[/edit]
Then just call it:
C#
cmdTemp.Parameters.AddWithValue("@comment_NAME30", AlwaysAString(row.Cells[0]));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900