Click here to Skip to main content
15,889,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Dears!
i Have a Problem system use of unassigned local variable "Fee";
if comboBox SelectedValueChanged and the value form the comboBox is not available in database then Show a message that you select a Class which have no fee saved in dataBase

My Code

this line in ComboBox_SelectedValueChanged
C#
FeeLabel.Text = GetFee().ToString();


and the GetFee() method is

C#
private int GetFee()
        {
            int fee;

            using (DbSQLServer db = new DbSQLServer(AppSetting.ConnectionString()))
            {
                fee = (int)db.GetScalarValue("GetFeePaymentByClass", new DbParameter { Parameter = "Class", Value = FindStudentComboBox.Text });
            }
            return fee;
        }


This is my code to call scalar value from database
C#
public object GetScalarValue(string storedProceName, DbParameter parameter)
       {
           object value = null;

           using (SqlCommand cmd = new SqlCommand(storedProceName, conn))
           {
               cmd.CommandType = System.Data.CommandType.StoredProcedure;

               if (conn.State != ConnectionState.Open)
               {
                   conn.Open();
               }

               cmd.Parameters.AddWithValue(parameter.Parameter, parameter.Value);

               value = cmd.ExecuteScalar();
           }

           return value;
       }


How I solve this?

What I have tried:

I have searched in the google but not solve my issue so please do more clear because I have a student of software engineer not a developer or professional
Posted
Updated 13-Jan-19 2:42am
v2

Your connection using DbSQLServer looks very strange, maybe you should use a more standard way of connecting, see: https://www.guru99.com/c-sharp-access-database.html[^]
And also: Obtaining a Single Value from a Database | Microsoft Docs[^]

If your code works, you could change it like this:
int? fee;

     using (DbSQLServer db = new DbSQLServer(AppSetting.ConnectionString()))
      {
          fee = (int)db.GetScalarValue("GetFeePaymentByClass", new DbParameter { Parameter = "Class", Value = FindStudentComboBox.Text });
      }

      If (fee.HasValue)
         return fee;
      else
         return 0;

Notice the int? see explanation here: Null-conditional Operators - C# Reference | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
Fahim ullah 13-Jan-19 7:56am    
This is my library and i was pass ExecuteScalar() value ervry thing is correct but only not pass null value
Fahim ullah 13-Jan-19 8:00am    
Please Check again I Post My scalar method on which I call a scalar value from the database
Fahim ullah 13-Jan-19 9:01am    
Not solve
Couldnt you write the code something like:
C#
var fee = null; 
fee = GetFee();
if (string.IsNullOrEmpty(fee))
{
    otherClass.OtherRoutine();
    FeeLabel.Text = "<T.B.D.>"
}
else
{
    Feelabel.Text = fee.ToString();
}

Where T.B.D stands for "To be determined" or whatever else would make sense in the event of no-data.
 
Share this answer
 
v5
Comments
Fahim ullah 13-Jan-19 8:51am    
Please define otherClass.OtherRoutine(); and also there is an error cannot assign null to an implicitly-typed variable.
my Method is integer
Member 11830457 13-Jan-19 11:49am    
Well as to which other Class, you said:
>>then Show a message that you select a Class which have no fee saved in dataBase
If you didnt mean you should then select an "alternative" class that handles "no data" just leave that line out and asssign a message that seems appropriat i.e FeeLabel.Text = "Whatever you like";
As regards the implicitly-typed variable - my bad forgot about that. As Wendelius mentions then return it to a nullable int.

int? fee = GetFee();
if (string.IsNullOrEmpty(fee))
{
FeeLabel.Text = "<t.b.d.>";
}
else
{
Feelabel.Text = fee.ToString();
}
From what I gather you have two potential problems:
- The record is found but the return value from the database is NULL
- The record isn't found.

In the latter case a c# null value is returned but in the first case a DBNull.Value Field (System) | Microsoft Docs[^] would be returned.

If in both cases you want to return null, you could modify the method GetScalarValue to return the following
C#
return value == DBNull.Value ? null : value ;


Then what comes to handling the null value in method GetFee, if the fee would be NULL I would imagine you want to show an empty string. After all a fee of 0 is different from the situation when a fee is not found.

If the assumption is correct you could use nullable int. In other words
C#
private int? GetFee()
        {
            int? fee;
...
fee = (int?)db.GetScalarValue...
...
            return fee;
        }

Also you would need to change the calling code to the following to avoid null reference exception
C#
FeeLabel.Text = GetFee();


A little off-topic but it seems that you're storing the connection in some variable, perhaps even in open state. I would recommend not to do this. Instead, keep the connection open as short time as possible and let connection pooling to handle repeated connection opens, for more information, see SQL Server Connection Pooling (ADO.NET) | Microsoft Docs[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900