I'm still learning the particulars of C# so please forgive me if this rudimentary. I have been looking for an answer to this but haven't found one.
I have declared the local variable (in my function):
string myVar;
I keep getting the error, "Use of unassigned local variable" when I try to return it:
return (myVar);
What am I doing wrong here?
public string GetSomethingFromDB()
{
string connectionString;
string sql;
string myVar;
connectionString = "Data Source=MyServer; Port=MyPort; User ID=someuser; Password=mypassword; Database=myDatabase";
sql = "select something from something";
using (AseConnection conn = new AseConnection(connectionString))
{
using (AseCommand cmd = new AseCommand(sql, conn))
{
try
{
conn.Open();
using (AseDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
myVar= reader.GetString("column1").Trim();
}
reader.Close();
reader.Dispose();
}
conn.Close();
conn.Dispose();
cmd.Dispose();
return (myVar);
}
catch (AseException ex)
{
//do some stuff
}
finally
{
//do some stuff
}
}
}
}