Receiving data from variable from another form. [* Resovled *]
Hello,
I have 2 forms. One is called librarySettings, and another one is called loanBook. When the user wants to loan a book, l want to be able to receive 2 variables from this form, which is DaysBorrowed, and OverdueFine. This way if the settings are changed then the loanbook form will have the updated variables.
This is what l have done, and l have done this before, but can't seen to get it to work. Don't know what is going on here.
LibrarySettings form.
Code:
This load the variables data from a database, so that the settings can be updated to a database.
private void frmLibrarySettings_Load(object sender, System.EventArgs e)
{
try
{
cnn.Open();
OleDbCommand cmd = cnn.CreateCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM LibrarySettings";
da.SelectCommand = cmd;
da.Fill(dt);
upDDays.Value = Convert.ToInt16(dt.Rows[0]["Days"]); //load into a numericUpDown control
upDFine.Value = Convert.ToInt16(dt.Rows[0]["Fine"]);
}
catch ( OleDbException ex )
{
MessageBox.Show(ex.Message);
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message);
}
}
//Set up 2 public procedures
Code:
public int fineAmount
{
get
{
return Convert.ToInt16(upDFine.Value);
}
}
public int DaysToBorrow
{
get
{
return Convert.ToInt16(upDDays.Value);
}
}
That is all for the library settings form
for the loanbook form.
I have declared this in the form at the top of the class
Code:
frmLibrarySettings getLibraryDetails = null; //Finds the day and fine amount that has been set in the library settings form
Find out how long the book can be loaned for.
Code:
int loanDays = 0;
loanDays = getLibraryDetails.DaysToBorrow; //Get the days the book can be loaned for - error occurs here
The error message l get when this code is executed is " Object reference not set to an instance of an object."
Not sure what l am doing wrong. Hope someone can help
Thanks in advance,
Steve
Re: Receiving data from variable from another form.
Code:
frmLibrarySettings getLibraryDetails = null; //Finds the day and fine amount that has been set in the library settings form
Why are you setting it to null?
Re: Receiving data from variable from another form.
It did not work when it was NULL, so l set it to null. I thought that maybe that was the problem.
Do you have any other ideas
Thanks for you reply,
Steve
Re: Receiving data from variable from another form.
Code:
frmLibrarySettings getLibraryDetails = new frmLibrarySettings();
Re: Receiving data from variable from another form.
Thanks for you help, it worked