• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Data binding to textbox

I am trying to display the result of SP below to a textbox. How can I do this (C#)?
I've tried TextBox12.Text = BadDeptDB.PushBDPAllSites(year, month);
But, it says Cannot implicitly convert 'void' to 'string'...

public static void BDPALLSITES(int month, int year)
{

string connectionString = ConfigurationManager.ConnectionStrings["Finance_AppsConnectionString"].ConnectionString;
System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);

System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = "usp_BDPALLSITES";
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.Connection = dbConnection;


System.Data.IDataParameter dbParam_year = new System.Data.SqlClient.SqlParameter();
dbParam_year.ParameterName = "@year";
dbParam_year.Value = year;
dbParam_year.DbType = System.Data.DbType.Int32;
dbCommand.Parameters.Add(dbParam_year);



System.Data.IDataParameter dbParam_month = new System.Data.SqlClient.SqlParameter();
dbParam_month.ParameterName = "@month";
dbParam_month.Value = month;
dbParam_month.DbType = System.Data.DbType.Int32;
dbCommand.Parameters.Add(dbParam_month);

int result = 0;

try
{
dbConnection.Open();
result = dbCommand.ExecuteNonQuery();
}
catch
{

}


finally
{
dbConnection.Close();
}



}


--SP
Create proc usp_BDPALLSITES ( @month int , @year int)
as
select distinct(f.bad_dept_percent) from Revenue_aggregate_assertions f where ((f.[month] = @month) AND (f.[year] = @year) )
0
jung1975
Asked:
jung1975
  • 4
  • 2
  • 2
1 Solution
 
naveenkohliCommented:
Change this..

public static int BDPALLSITES(int month, int year)
{
.... All you implementation....

return result;
}
0
 
Carl TawnSystems and Integration DeveloperCommented:
Its complaining because the return type of your method is void, i.e. you're not returning anything.

You probably want to change your method header to either:

   public static string BDPAllSites(int month, int year)

Or:

   public static int BDPAllSites(int month, int year)


Then you would either return "result" as a string with:      return result.toString();

Or, return as an int and assign to the textbox with:          

         TextBox12.Text =  BadDeptDB.BDPAllSites(year, month);           // should be ok because C# can implicitly cast int to string
0
 
jung1975Author Commented:
ok, I changed the code like below. But, it returns -1 .. It should be return 2.20. I've checked the SP and it returns 2.20.
I am not sure what I am doing wrong
 public static decimal BDPALLSITES(int month, int year)
    {
       
-----
decimal result = 0;

        try
        {
            dbConnection.Open();
            result = dbCommand.ExecuteNonQuery();
        }
        catch
        {

        }


        finally
        {
            dbConnection.Close();
        }


       return result;
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Carl TawnSystems and Integration DeveloperCommented:
Thats because ExecuteNonQuery() is not the method you need. The -1 that it returns is the number of records affected, which in this case is irrelevant so it returns -1.

You need to be calling the ExecuteScalar() method instead.
0
 
jung1975Author Commented:
if I change to ExecuteScalar();

I get Cannot implictly convert type 'object' to decimal. An explicit conversion exists ( are you missing a cast)?



0
 
naveenkohliCommented:
You will have to do conversion..

result = (decimal)dbCommand.ExecuteScalar();
0
 
jung1975Author Commented:
I changed it.. but still getting -1  

decimal result = 0;

        try
        {
            dbConnection.Open();
            result = (decimal)dbCommand.ExecuteNonQuery();
        }
        catch
        {

        }


        finally
        {
            dbConnection.Close();
        }


        return result;
0
 
jung1975Author Commented:
Nevermind I 've fixed the problem. Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now