0
public DataSet ds = new DataSet();
public SqlDataAdapter adapter = new SqlDataAdapter();

public int AuthenticatedUserAge(String User_name)
{
    string sql = "SELECT UserName, Age FROM tblDataProg WHERE (UserName ='" +   User_name  + "')";
    ds = GetDataSet(sql);
    int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
    return help;    
}

public DataSet GetDataSet(string sql)
{
    SqlConnection connection = new SqlConnection(ConnStr());
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = sql;
    cmd.Connection = connection;
    adapter = new SqlDataAdapter(cmd);
    connection.Open();
    adapter.Fill(ds);
    connection.Close();
    return ds;
}    

public string ConnStr()
{
    return @"Data Source=.\SQLEXPRESS;AttachDbFilename='G:\עבודת גמר תכנות\v5\חדש תיקיה 1\v4\App_Data\SiteDB.mdf';Integrated Security=True;User Instance=True";
}

I need to check that the value that is returned from AuthenticatedUserAge is a number less than 17 that it will write hello teenager else write hello adult.

The problem I think is this line doesn't return a value:
int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
it throws this error:

Input string was not in a correct format

JumpingJezza
  • 5,122
  • 10
  • 62
  • 98
user3649869
  • 1
  • 1
  • 2

5 Answers5

0

This is not really an answer, but it will not be clear in a comment.

Break this line up:

int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());

Into:

//set a breakpoint on the next line
string temp = ds.Tables[0].Rows[0]["Age"].ToString(); 
//take one step (F10) over the break and hover over temp
//or use a watch.  What is the value of temp?
int help = int.Parse(temp);
Phil Sandler
  • 26,117
  • 19
  • 77
  • 138
0

The value in the database for that user's age must be NULL.
You need to check for NULL, and to be safe you should also check for no results returned from the database.

public int AuthenticatedUserAge(String User_name)
{
    string sql = "SELECT UserName, Age FROM tblDataProg WHERE (UserName ='" +   User_name  + "')";
    ds = GetDataSet(sql);
    //check for no results
    if(ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
    {
       return 0;
    }
    //check for null
    if(ds.Tables[0].Rows[0]["Age"] == DBNull.Value)
    {
       return 0;
    }
    int help = int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
    return help;    
}

furthermore you REALLY should not be using dynamic SQL - please use parameters instead. Read this.

Community
  • 1
  • 1
JumpingJezza
  • 5,122
  • 10
  • 62
  • 98
0

Use the TryParse method to prevent an error:

public int AuthenticatedUserAge(String User_name)
{
    string sql = "SELECT UserName, Age FROM tblDataProg WHERE (UserName ='" +   User_name  + "')";
    ds = GetDataSet(sql);
    int help = 0;
    int.TryParse(ds.Tables[0].Rows[0]["Age"].ToString(), out help);
    return help;    
}

With the above code, if an error occurs, the value of help will remain at its initialization value (in this case, 0).

johnnyRose
  • 6,243
  • 16
  • 40
  • 58
Novin.Kh
  • 91
  • 2
  • 6
0

Try this. It will help you.

int help = Convert.ToInt32(ds.Tables[0].Rows[0]["Age"]);
Ankita Shah
  • 1,818
  • 11
  • 30
Ajsatis
  • 31
  • 2
0

Probably age field is Null you may use this to identify and set a default value.

int Help = ds.Tables[0].Rows[0]["Age"] != DBNull.Value ? Convert.ToInt16(ds.Tables[0].Rows[0]["Age"]) : 0;