Click here to Skip to main content
14,550,550 members
Rate this:
Please Sign up or sign in to vote.
i"m to retrieve data from a mysql database to a read only textbox, i"m using a stored procedure  with input parameter i"m also using dapper as my mapper, and  class library as model for my data, any ideas pleas<pre lang="text">

What I have tried:

Below is from my books library class
lass library
public list<BooksModel>MyQuantity{get; set;}
Public string QuantiyNew{get {return MyQuantity.ToString();set{;}}}

Below is from my sqlconnector class, I'm using two data storage, Mysql and textile so I created an enum I'm also using an interface IDataconnection
public class Sqlconnector: iDataconnection
public List<BooksModel> GetStockQuantity(BooksModel modell)
          List<Booksmodel> output;
            using (IDbConnection connection = new MySqlConnection(GlobalConfig.CnnString(mydb)))
                output = (BooksModel)connection.Query<BooksModel>("Book_GetAll_By_BookTitle", new { book_titlesp = bookname }, commandType: CommandType.StoredProcedure);
modell.MyQuantity= output;
            //var invoice = connection.Get<Invoice>(1);
            return output;

Now this is the code behind my UI design
Public partial class books item: Form
List<BooksModel> stocktotal =new List<BooksModel>();
Private void Stock now()
BooksModel model= new BooksModel(txtbt1.text,null,null,null,null,null);
stocktotal= GlobalConfig.Connection.GetStockQuantity(model);

Private void WireUpStockTotal()
stocktotaltxt.Data bindings.Clear();
stock total.Add(new BooksModel(){QuantityAgain= "C#"});
stocktotaltxt.DataBindings.Add("Text", stock total[0],"QuantityAgain");
Private void txtbt1_Leave(object sender, EventArgs e)
Stock now();

the storage procedure is simply a "SELECT * FROM booktable WHERE book name= booksp;"

I'm trying to get the result of my query into the stocktotaltxt textbox, but whenever I run this code it goes to my sqlconnector class and throws an exception of 'object reference not set to an instance of an object
BooksLibrary.Model.Transaction.MyQuantity.get returned null' at the public string QuantityAgain{get {return MyQuantity.ToString();} set{;}}
Updated 24-May-20 2:11am
Patrice T 22-May-20 21:56pm
Show your code !
Richard MacCutchan 23-May-20 3:50am
You forgot to tell us what the problem is.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

object reference not set to an instance of an object

That means that you are trying to use a variable reference that has not been initialised. Either because you forgot to do it, or because you expected it to be returned from some method that failed. The only way to find out which one it is and where the failure occurred is to use the debugger to step through the code until it hits the exception. You can then examine all the variables to see what is happening or not happening.

However looking at your code it is most likely the following:
output = (BooksModel)connection.Query<BooksModel>("Book_GetAll_By_BookTitle", new { book_titlesp = bookname }, commandType: CommandType.StoredProcedure);
modell.MyQuantity= output;

You are assuming that output receives some value, but you forgot to check whether it does or not. You should never assume that method calls do what you expect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100