Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi, I am fetching the record from database and want to check the Whether record for perticular row is present or not? I am trying some code at below ,but error is that cannot convert bool to string...
So how can i overcome this problem

C#
bool chek = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Contains();
if(Chek>0)
                            {
                                txtMobileNo.Text = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Substring(3);
                            }
Posted
Updated 27-Jun-18 7:24am

Use the Boolean.Parse[^] or Boolean.TryParse[^] methods.
 
Share this answer
 
bool check = string.IsNullOrEmpty( Convert.ToString( ds.Tables["table"].Rows[0]["column"]));
C#

 
Share this answer
 
Comments
SVT02 20-Feb-14 1:40am    
But How can i write the 'chek' for testing condition
prakashdotnet 20-Feb-14 1:42am    
what exactly you want in check condition
SVT02 20-Feb-14 1:49am    
if(chek)
{
......
}
prakashdotnet 20-Feb-14 2:59am    
yes using
bool check = string.IsNullOrEmpty( Convert.ToString( ds.Tables["table"].Rows[0]["column"]));

you can add check condition in if
if (check)
{
// ur rest of logic
}
 
Share this answer
 
try this

C#
var chek = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Contains();
if(Int.Parse(Chek)>0)
  {
   txtMobileNo.Text = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Substring(3);
  }


your trying to compare string to int that's why you get the error I guess. . .
 
Share this answer
 
v3
Comments
SVT02 20-Feb-14 1:49am    
I have tried this but error is that input string is not in correct format
Here you don't need to do any Type conversion.Please try is as below.

C#
if (DS.Tables["table"].Count()>0 && DS.Tables["table"].Rows[0]["MB_MobileNo"].Count()>0)

      {
       txtMobileNo.Text = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Substring(3);
      }
 
Share this answer
 
v3
Comments
SVT02 20-Feb-14 1:55am    
It shows error like Operator '>' cannot be applied on operands of type 'method group' and 'int'
Sampath Lokuge 20-Feb-14 2:10am    
I have updated the solution.Please check that.
solved just tried
C#
if(DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString()!="")
                            {
                                txtMobileNo.Text = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Substring(3);
                            }
 
Share this answer
 
Simple you can write like below...

C#
if (!string.IsNullOrEmpty(DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString()))
               {
                   txtMobileNo.Text = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Substring(3);
               }
 
Share this answer
 
You are trying to assign a string value to bool.which never possible.
You can try ternary to check:

C#
string vString = DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString().Substring(3);
string abc = vString !="" ? vString : "";
 
Share this answer
 
if (DS.Tables["table"].Rows.Count > 0 && !string.IsNullOrEmpty(DS.Tables["table"].Rows[0]["MB_MobileNo"].ToString()))
{
//do your logic
}

// Have to include the Rows.Count check to avoid possible IndexOutOfRangeException
 
Share this answer
 
Comments
Richard Deeming 27-Jun-18 13:32pm    
As already mentioned in solution 5 - FOUR YEARS AGO.
Member 13889705 4-Nov-21 13:53pm    
Solution 5 is slightly different and throws an exception. This does not.
Richard Deeming 5-Nov-21 5:20am    
It took you three years to respond to this comment, and your defence is to claim that the virtually identical code you copied from throws "an exception"? You couldn't even be bothered to specify which exception you think it throws, nor explain why you think your solution wouldn't throw it.

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