I cannot seem to get the combobox text value to display the output of a function. Its behaving extremely strangely.
All of these work:
item.Text = "test";
string testStr = "test";
item.Text = testStr;
string testStr = "test" function();
item.Text = testStr;
string testStr = " " function();
item.Text = testStr;
These do not work
item.Text = function();
testStr = "" function();
item.Text = testStr;
I'm about to tear my hair out. Please help me.
I listed above what I tried. Its worth noting that the function is working fine in every other situation, and it outputs a normal string without issue. I just need this to work:
item.Text = function();
The function is:
public static string getStringFromDB(string sqlStr)
{
string result = null;
SqlConnection conn;
conn = openCon();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
try
{
result = (string)cmd.ExecuteScalar();
}
catch (Exception ex)
{
result = getIntFromDB(sqlStr).ToString();
}
closeConn(conn);
return result;
}
Edit: I have now discovered that the following messagebox displays properly, but the Combobox does not display any data.
item.Text = getStringFromDB("SELECT " dbStrings[2] " FROM " dbStrings[0] " WHERE " dbStrings[1] " = " getIntFromDB("SELECT " item.Name " FROM MASTER WHERE MASTER_ID = " recordID ";"));
MessageBox.Show(item.Text);
Edit: This morning, the code was working fine after no changes were made. I have no idea what is going on.
CodePudding user response:
I am closing this, as the code is working this morning. I did not make any changes to the code since posting this.
CodePudding user response:
It seems like the issue is with the function getStringFromDB(string sqlStr). It appears that the function is working fine in all other situations, but it's not working when you assign its return value to the Text property of the combobox item.
Here are a few things you can try:
Verify the return value of the function: Make sure that the function is returning a string value as expected. You can check this by adding a temporary line of code to print the return value of the function to the console before assigning it to the combobox item's Text property.
Check for null values: Make sure that the function is not returning a null value. If it is, you'll need to handle that case in your code.
Check the combobox item's Text property: Make sure that the combobox item's Text property is accessible and can be set.
Check for any exceptions: Make sure that there are no exceptions being thrown when you assign the return value of the function to the combobox item's Text property. If there are, make sure to handle them appropriately.
If the function is working fine in other situations but not working here, you may try calling the function and storing the result in a variable, and then assign the variable to the combobox's Text property.