Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 03, 2012 09:29 AM by scriptkid
Member
30 Points
21 Posts
Mar 03, 2012 08:13 AM|LINK
Hi people.
This is my code:
public string district { get { String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; String strQuery = "SELECT district FROM bankscombined WHERE ifsccode= '" + ifsccode + "'"; MySqlConnection con = new MySqlConnection(strConnString); MySqlCommand cmd = new MySqlCommand(); cmd.CommandType = CommandType.Text; cmd.Connection = con; con.Open(); cmd.CommandText = strQuery; MySqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); String dist = sdr["district"].ToString(); con.Close(); return dist; }//end of get block } //end of district
Now, What I am trying to do is, I passed a ifsccode which is not listed on the database.
I am not understanding the way to implement the try & catch block. I tried to do this, but I failed.
I am getting this error message currently without implementing the try & catch block:
"Invalid attempt to access a field before calling Read()" pointing to the following line of the code:
String dist = sdr["district"].ToString();
Can anyone please provide me the soultion to this ?
Thank you
All-Star
31374 Points
5420 Posts
Mar 03, 2012 08:26 AM|LINK
hi, before reading check whether data is there or not using below code
if(dataReader.HasRows){
sdr.Read();
String dist = sdr["district"].ToString(); }
Participant
896 Points
238 Posts
Mar 03, 2012 08:30 AM|LINK
check sdr["distinct"] is null or not
if not null then only fillon the string ..i.e.
Mar 03, 2012 08:41 AM|LINK
Thank you for your valuable replies.
If i did that, I am unable to return the value to the variable district. The string variable dist goes out of scope.
Check the screenshot.
Mar 03, 2012 08:49 AM|LINK
hi, check my updated code
String dist=""; if(dataReader.HasRows){
dist = sdr["district"].ToString(); } return dist;
Mar 03, 2012 09:29 AM|LINK
Genius :D
thanks :)
scriptkid
Member
30 Points
21 Posts
Implementing Try Catch Block
Mar 03, 2012 08:13 AM|LINK
Hi people.
This is my code:
public string district
{
get
{
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
String strQuery = "SELECT district FROM bankscombined WHERE ifsccode= '" + ifsccode + "'";
MySqlConnection con = new MySqlConnection(strConnString);
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.CommandText = strQuery;
MySqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
String dist = sdr["district"].ToString();
con.Close();
return dist;
}//end of get block
} //end of district
Now, What I am trying to do is, I passed a ifsccode which is not listed on the database.
I am not understanding the way to implement the try & catch block. I tried to do this, but I failed.
I am getting this error message currently without implementing the try & catch block:
"Invalid attempt to access a field before calling Read()" pointing to the following line of the code:
String dist = sdr["district"].ToString();
Can anyone please provide me the soultion to this ?
Thank you
karthicks
All-Star
31374 Points
5420 Posts
Re: Implementing Try Catch Block
Mar 03, 2012 08:26 AM|LINK
hi, before reading check whether data is there or not using below code
if(dataReader.HasRows){
sdr.Read();
String dist = sdr["district"].ToString();
}
Karthick S
Mahesh Darku...
Participant
896 Points
238 Posts
Re: Implementing Try Catch Block
Mar 03, 2012 08:30 AM|LINK
check sdr["distinct"] is null or not
if not null then only fillon the string ..i.e.
String dist = sdr["district"].ToString();
scriptkid
Member
30 Points
21 Posts
Re: Implementing Try Catch Block
Mar 03, 2012 08:41 AM|LINK
Thank you for your valuable replies.
If i did that, I am unable to return the value to the variable district. The string variable dist goes out of scope.
Check the screenshot.
karthicks
All-Star
31374 Points
5420 Posts
Re: Implementing Try Catch Block
Mar 03, 2012 08:49 AM|LINK
hi, check my updated code
String dist="";
if(dataReader.HasRows){
sdr.Read();
dist = sdr["district"].ToString();
}
return dist;
Karthick S
scriptkid
Member
30 Points
21 Posts
Re: Implementing Try Catch Block
Mar 03, 2012 09:29 AM|LINK
Genius :D
thanks :)