Home > Software engineering >  How do I pass textbox values for this method argument?
How do I pass textbox values for this method argument?

Time:04-15

I have an Entity Model class named BookMetaData.cs. The class has three properties: id, Name, and Price. I also have a file BookRepository.cs with another class which has an Add() method to save a book to myModel.edmx.

Here's my `Add method:

private CRUDTestEntities db = new CRUDTestEntities();
       
public bool Add(CRUDTest.BOOK_TBL entity,bool autoSave = true)
{
    try
    {
        db.BOOK_TBL.Add(entity);
        if (autoSave)
        {
            return Convert.ToBoolean(db.SaveChanges());
        }
        else
            return false;
    }
    catch 
    {
        return false;
    }
}

I have 3 textboxes in my form: id, Name, and Price. How can I pass the values from these textboxes to My Add() method?

I'm trying to call the method like this, but I'm not sure how to finish this code:

var blBook = new Repositories.BookRepository();
blBook.Add(/* There are two arguments in this method. How do I pass in textboxes?*/);

CodePudding user response:

You have to use the values from your textboxes to construct a new object matching the argument type for the method, which seems to be CRUDTest.BOOK_TBL. It'll look something like this:

var blBook = new Repositories.BookRepository();
var book = new CRUDTest.BOOK_TBL() {
    id = txtID.Text,
    Name = txtName.Text,
    Price = Convert.ToDecimal(txtPrice.Text)
};

blBook.Add(book);
  • Related