C# Get String From Data set Exception

129 Views Asked by At

I searched already & it isn't the issue to read Dataset , The issue is i done same but it is not working

I Am trying to get complete name from Datasset, when i try following

String Receiver_name = rec_Ds.Tables[0].Rows[0][First_Name].ToString() + " " + rec_Ds.Tables[0].Rows[0][Last_Name].ToString();

it gives ArgumentNullException was unHandeled With sub Message of 'name' argument cannot be null.

so I searched over internet & changed Code to

String Receiver_name = rec_Ds.Tables[0].Rows[0][“First_Nameˮ].ToString() + “ ˮ + rec_Ds.Tables[0].Rows[0][“Last_Nameˮ].ToString();

now it gives “First_Nameˮ Doesn't exist in content

I tried get it working by following , It doesn't seems any difference ( in above and below code) to me in any kind but it works perfectly

My question is Why this work while above doesn't worked, And why above code doesn't work

String Receiver_name = rec_Ds.Tables[0].Rows[0]["First_Name"].ToString();
Receiver_Name+= " " + rec_Ds.Tables[0].Rows[0]["Last_Name"].ToString();
2

There are 2 best solutions below

0
On BEST ANSWER

Well, “First_Nameˮ is treated as a variable name and not as a quoted string, as the quotes are not "normal" double quotes, but typographical left/right quotes, so they are threated as unicode signs.

Change them to normal double quotes.

String Receiver_name = rec_Ds.Tables[0].Rows[0]["First_Name"].ToString() + " " + rec_Ds.Tables[0].Rows[0]["Last_Name"].ToString();

So: Be careful when copy&pasting from the internet - make sure the quotes are actually recognised as quotes and check the syntax highlighting.

0
On

Your Code is 100% Correct Except you doesn't use Double Quotes might be due to you copied from web

actually you are using Left Double quotes & Right Double quotes , Their Hexadecimal values doesn't match with simple quotes

Character name             Symbol  Decimal-Val   Hax-Val

left double quotation mark   “      8220         201C
right double quotation mark  ”      8221         201D
double quotes                "      34           22

In Compiler left double quotation mark & Right double quotation mark Considered to be part of name , Not considered as quotation Mark, That's why you getting error

Following will work fine for you

 Receiver_name = rec_Ds.Tables[0].Rows[0]["First_Name"].ToString() + " " + rec_Ds.Tables[0].Rows[0]["Last_Name"].ToString();