Hi All
I am new to C# and used to do procedural programming so now I am doing my app in OOP.
I want to bind my combobox key and value from a database but I have numerous comboboxes to do so I am doing it as a method. I will first post the code I have done and then the exception that I am getting
and my error is
If I can be pointed in the right direction so that I can fix my code or understand where I am going wrong.
Thank you
I am new to C# and used to do procedural programming so now I am doing my app in OOP.
I want to bind my combobox key and value from a database but I have numerous comboboxes to do so I am doing it as a method. I will first post the code I have done and then the exception that I am getting
C#:
public static class ComboBoxBinding
{
public static void BindKeyValueComboBox(ComboBox comboBoxName, string tableName, string comboBoxKey, string comboBoxValue)
{
string queryString = "Select " + comboBoxKey + ", " + comboBoxKey + " FROM " + tableName;
SqlConnection comboBoxDatabaseConnection = DatabaseUtilities.GetDatabaseConnectionDetails();
SqlDataAdapter comboBoxDataAdapter = new SqlDataAdapter(queryString, comboBoxDatabaseConnection);
DataSet comboBoxDataSet = new DataSet();
comboBoxDataAdapter.Fill(comboBoxDataSet, tableName);
comboBoxName.ItemsSource = comboBoxDataSet.Tables[0].DefaultView;
comboBoxName.DisplayMemberPath = comboBoxDataSet.Tables[0].Columns[comboBoxValue].ToString();
comboBoxName.SelectedValuePath = comboBoxDataSet.Tables[0].Columns[comboBoxKey].ToString();
}
}
and my error is
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=StudentSynergy
StackTrace:
at StudentSynergy.ComboBoxBinding.BindKeyValueComboBox(ComboBox comboBoxName, String tableName, String comboBoxKey, String comboBoxValue) in C:\Users\Lional\Documents\C#\C# Projects\StudentSynergy\ComboBoxBinding.cs:line 22
If I can be pointed in the right direction so that I can fix my code or understand where I am going wrong.
Thank you