Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I pass textbox values for this method argument?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?*/);

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading