Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to display the sum up certain columns from my SQL Server database into Winforms textboxes.
My Database results: [^]

And this is how my winForms looks like: [^]

My C# code:

C#
if (combobox1.SelectedText != null)
{
    string CS = (@"Data )

    SqlConnection con = new SqlConnection(CS);

    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.Connection = con;
    sqlCmd.CommandText = ("[dbo].[spSalesAnalyse]");
    sqlCmd.CommandType = CommandType.StoredProcedure;

    cbAnalyse.SelectedValue.ToString());
    SqlDataReader myReader;

    try
    {
         con.Open();
         SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
       // Save the results in the DT. Call the adapter statement and fill it in the DT
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                //Fill textBoxes with data in DT
                textBox1.Text = dt.Rows[0].Field<string>("North");
                textBox2.Text = dt.Rows[1].Field<string>("East");
                textBox3.Text = dt.Rows[2].Field<string>("West");
                ....
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        con.Close();
    }
}
else
    MessageBox.Show("Error");



The Problem: If I debugg till this line adapter.Fill(dt), I do see the records BUT they are not getting to my textBoxes. They are empty when debugg reach textBoxes level

My Expectation:[^]
Posted
Comments
Karthik_Mahalingam 30-Apr-15 5:21am    
make sure that
textBox2.Text = dt.Rows[1].Field<string>("East");
textBox3.Text = dt.Rows[2].Field<string>("West");
these lines are pointing to Row 1
Maciej Los 30-Apr-15 5:37am    
Miky, this piece of code and images are useless. Please, be more specific and provide more details about your issue.
What have you tried? What's wrong with your code? Have you debug it?
mikybrain1 30-Apr-15 6:16am    
@ Maciej Los. Yes I've debug it and everything seems to be okay at this level adapter.Fill(dt);[The records are being fired into dt] BUT not getting into my textBoxes.

New screenshot: http://x53i.imgup.net/167327.PNG
Maciej Los 30-Apr-15 6:22am    
OK, try this:
dt.Rows[0]["North"] instead of dt.Rows[0].Field<string>("North")
mikybrain1 30-Apr-15 6:44am    
unfortunately no. It says identifier expected. So i put string infront but no

Please try below code and let me know for any issues:

Looks like you are trying to get one textbox value at a time. You could use the GROUP BY clause in SQL to fetch your data.

SQL
SELECT
  Country,
  Region,
  SUM([Sales1]) AS [First Sales],
  SUM([Sales2]) AS [Sec Sales],
  SUM([Sales3]) AS [Third Sales]
FROM 
  [dbo].[tblSales]
GROUP BY Country, Region

A single query will return all your results. You can take this results in a DataSet and then set the values of the corresponding textboxes by fetching values from the DataSet.

C#
//Use query string as show above
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);

Now that you have records inside your DataSet, you can fill your textboxes.

C#
DataRow dataRow = dataset.Tables[0].Select("Country = 'Italy' AND Region = 'North'").FirstOrDefault();
if (dataRow != null) {
    textbox1.Text = dataRow["First Sales"].ToString();
}
 
Share this answer
 
v2
Comments
mikybrain1 30-Apr-15 6:18am    
I've tried this approach and it's not working
Miky, you mixed everything!

Why to force doors wide open? Use proper tools, such as ReportViewer[^]. It enables grouping, sorting, etc.[^].
 
Share this answer
 
Comments
mikybrain1 30-Apr-15 9:41am    
Thnx anyway for the tips. I think I'm getting it now :). The foreach loop is working pretty good. I just need to advance & improve it. That's self help effort hahahaha.

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