Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Data binding to textbox

Posted on 2006-04-27
8
Medium Priority
?
548 Views
Last Modified: 2007-12-19
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
Comment
Question by:jung1975
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 16556106
Change this..

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

return result;
}
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16556175
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
 

Author Comment

by:jung1975
ID: 16556464
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16556485
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
 

Author Comment

by:jung1975
ID: 16556715
if I change to ExecuteScalar();

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



0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 500 total points
ID: 16556789
You will have to do conversion..

result = (decimal)dbCommand.ExecuteScalar();
0
 

Author Comment

by:jung1975
ID: 16556947
I changed it.. but still getting -1  

decimal result = 0;

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

        }


        finally
        {
            dbConnection.Close();
        }


        return result;
0
 

Author Comment

by:jung1975
ID: 16557007
Nevermind I 've fixed the problem. Thank you!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Screencast - Getting to Know the Pipeline
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question