I want to return a last record stored in database and want to instantiate that returned value to the class ReligionCaste's member pID, But i have the title error.
My database function is
public int LastEnteredRecord()
{
int lastId=0;
dbConnection = new SqlConnection(connectionString);
try //If error in connection opening, throws it.
{
dbConnection.Open();
command.Connection = dbConnection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.LastRecordEntered";
try //if error in query execution, throws it.
{
lastId= Convert.ToInt32 ( command.ExecuteScalar());
// MessageBox.Show("Record Entered with ID:"+lastId .ToString ());
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
dbConnection.Close();
dbConnection.Dispose();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
return lastId;
}
I want to assign that return value to a class instant the code is
class ReligionCaste
{
//public int religion_ID, caste_ID;
public String religion, sect, caste, subCaste;
public int pID;
DatabaseHandler.DBCntrlr dbObj = new DatabaseHandler.DBCntrlr();
pID = dbObj.LastEnteredRecord();
}
but it giving the above mentioned error.
You need a constructor to assign a value to pId, at the moment your syntax is invalid. Example:
class ReligionCaste
{
public ReligionCaste()
{
pID = dbObj.LastEnteredRecord();
}
//public int religion_ID, caste_ID;
public String religion, sect, caste, subCaste;
public int pID;
DatabaseHandler.DBCntrlr dbObj = new DatabaseHandler.DBCntrlr();
}
class ReligionCaste
{
//public int religion_ID, caste_ID;
public String religion, sect, caste, subCaste;
public int pID;
private DatabaseHandler.DBCntrlr dbObj;
public ReligionCaste()
{
dbObj = new DatabaseHandler.DBCntrlr();
pID = dbObj.LastEnteredRecord();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.