I am relatively new to Dapper and also C#. I am trying to get Vendor Names from the SQL database into a combobox when the form opens up.
Here is the code:
The Database tables is like this:
CREATE table [dbo].[PURCH_VENDOR]
([VENDOR_ID] varchar(20),
,[VEND_NAME] varchar(20),
,[VEND_ADDRESS] varchar(20),
,[VEND_PHONE] varchar(20),
,[VEND_WEBSITE] varchar(20),
,[NOTES] varchar(20))
Vendor Class:
public class Vendor:IVendor
{
//properties
public int ID;
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public string Notes { get; set; }
public static string ErrorMessage { get; set; }
//Constructors
public Vendor(string name,string address,string phone,string website,string notes)
{
Name = name;
Address = address;
Phone = phone;
Website = website;
Notes = notes;
}
public Vendor(string name)
{
Name = name;
}
public Vendor() { }
public List<Vendor> GetVendors()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("SAMPLEDB")))
{
var output = connection.Query<Vendor>("select vend_name from dbo.purch_vendor").ToList();
return output;
}
}
The code in the Form is like this:
public partial class AddVendor : Form
{
//List<Vendor> vendor = new List<Vendor>();
public AddVendor()
{
InitializeComponent();
Vendor db = new Vendor();
var outp = db.GetVendors();
foreach (Vendor vd in outp)
{
cb_vendorName.Items.Add(vd.Name);//This is where the error kicks in
}
//IVendor vendorRep = new Vendor();
//List<Vendor> vendor = vendorRep.GetAll();
//foreach(var vend in vendor)
//{
// //Console.WriteLine(vend.Name);
// cb_vendorName.Items.Add(vend.Name);
//}
}
}
The error i get is this:
An unhandled exception of type 'System.ArgumentNullException' occurred in System.Windows.Forms.dll
but I dont have any NULL values in the database for vendor names.
As I debugged, it seemed to recognize that there are two entries in the database but does not populate any of the members properties. Any help would be appreciated.
Thanks!
You seem to have two issues there. First, there's a design issue that is not causing the exception you experience but it's something you should fix either way. Vendor
is a model class and should only contain the properties you listed (plus the constructors). Getting a list of Vendor
objects within the Vendor class itself is not good design. You should do it in another class or even in the main class where you're populating the ComboBox.
Now, to the error you're experiencing. By using the SQL query select vend_name from dbo.purch_vendor
, you're only getting a single field that can't be mapped to any of the properties in your Vendor
class.
Since VEND_NAME
is the name of the target column in your DB table purch_vendor
, you should have that also as a property name in your model:
public string VEND_NAME { get; set; } // instead of Name
Otherwise, the mapping engine doesn't know which field to populate.
If you take a look at this simple example, you can see that the column names are 1:1 to the property names in the model class. That way the properties will be filled just fine when you make the query.