Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is code, developed, I dont if I am mising anything, or the syntax gone wrong anywhere, dont understand where its going wrong.
I wish to read the data from same database (from two tables) and then it will get filled into different gridviews.

Can experts help me?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace Final_PlannedVsActuals
{
    public partial class PlannedVsActuals : System.Web.UI.Page
    {
        SqlCommand cmd = new SqlCommand();
        SqlConnection con; 
       
        SqlDataAdapter da = new SqlDataAdapter();
        int[] Planned_Hrs = new int[50];
        int[] Actual_Hrs = new int[50];
        int[] delta = new int[50];
        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
             con=  new SqlConnection(@"Data Source=IN1W7D-300386;Initial Catalog=MISIN;Integrated Security=True");
                con.Open();
                Response.Write("<script LANGUAGE='JavaScript' >alert('Con open')</script>");
                if (!IsPostBack)
                {
                    Cal_WkDelta();
                    Show_Country();
                }
            }
            catch(Exception ex)
            { Response.Write(""+ex); }
        }
          private void Cal_WkDelta()
        {
           try
                {
                    GridView4.Visible = true;
					string wk,grd;
					for (int w =1;w <= 5;w++)//to itrate weeks
            {
                    wk=string.Concat("Week_",w);
                            for(int count= 0;count<gridview4.rows.count;count++)>
                        {
                                grd=Convert.ToString(GridView4.Rows[count]);
                                string s1 = "DELETE FROM Daily_log_week";
								string s2 = "create view Daily_log_week  as (SELECT [country],[Monthc],[yearc],[sector],[TIME_SPENT],[datec]," +
                                             "('Week'+'_'+(convert(nvarchar(10),((((day(datec)-1)/7)+1))))) as Weekn" +
                                             "FROM [MISIN].[dbo].[tbl_daily_log] where sector = 'SEA' or sector = 'MEA' or sector = 'NEA'" +
                                             "GROUP BY country, Monthc, yearc,sector, TIME_SPENT,DATEc);";
                                string s3 = "DELETE FROM Daily_log_week";
								string s4 = "create view sector_week_actual_hours  as SELECT [country],[Monthc],[yearc],[sector]," +
                                            "Sum([TIME_SPENT]) as Actual_Hrs,[weekn] FROM [MISIN].[dbo].[Daily_log_week]" +
                                            "group by country, Monthc, yearc,sector,weekn;";

								SqlCommand cmd = new SqlCommand(s1, con);
                                cmd.ExecuteNonQuery();
								SqlCommand sqlcmd = new SqlCommand(s2, con);
                                sqlcmd.ExecuteNonQuery();

                                SqlCommand scmd = new SqlCommand(s3, con);
                                scmd.ExecuteNonQuery();
                                SqlCommand dlogcmd = new SqlCommand(s4, con);
                                dlogcmd.ExecuteNonQuery();


                                string s5 = "select Plnned_Hrs as p from PlanHrs where Week_Number=wk and Country =grd";
                                SqlCommand spcmd = new SqlCommand(s5, con);
                                SqlDataAdapter da = new SqlDataAdapter(spcmd);
                                DataSet ds = new DataSet();

                                da.Fill(ds, "plan");
                                DataRow drow = ds.Tables[0].Rows[count];
                                int pp = Convert.ToInt32(drow[1]);
                                Response.Write("<script LANGUAGE='JavaScript' >alert('1')</script>");

                                //int pp=cmd.ExecuteNonQuery();
                                Planned_Hrs[count] = pp;
                                // g1.Rows[count].Cells["Column1"].Control[03]= pp;

                                string s2 = "select TIME_SPENT from sector_week_actual_hours where Weekn=wk and country =grd";
                                cmd = new SqlCommand(s2, con);
                                da.Fill(ds, "plan");
                                 drow = ds.Tables[0].Rows[count];
                                int at = Convert.ToInt32(drow[1]);
                               //int at=cmd.ExecuteNonQuery();
                                 Actual_Hrs[count] = at;
                                 Response.Write("<script LANGUAGE='JavaScript' >alert('8')</script>");

                                delta[count] = Planned_Hrs[count] - Actual_Hrs[count];
                                Response.Write("week"+w);
                                
                                dt.Rows[count][0] = Planned_Hrs[count];
                                dt.Rows[count][1] = Actual_Hrs[count];
                                dt.Rows[count][2] = delta[count];

                                count++;
                            }

                            if (w == 1)
                            {
                                g1.Visible = true;
                                g1.DataSource = dt;
                                g1.DataBind();
                                Response.Write("<script LANGUAGE='JavaScript' >alert('2')</script>");
                            }
                            else
                                if (w == 2)
                                {
                                    g1.DataSource = dt;
                                    g1.DataBind();
                                    Response.Write("<script LANGUAGE='JavaScript' >alert('3')</script>");
                                }
                                else
                                    if (w == 3)
                                    {
                                        g3.DataSource = dt;
                                        g3.DataBind();
                                        Response.Write("<script LANGUAGE='JavaScript' >alert('4')</script>");
									}
                                    else
                                        if (w == 4)
                                        {
                                            g4.DataSource = dt;
                                            g4.DataBind();
                                            Response.Write("<script LANGUAGE='JavaScript' >alert('5')</script>");
                                        }
                                        else
                                        {
                                            g5.DataSource = dt;
                                            g5.DataBind();
                                            Response.Write("<script LANGUAGE='JavaScript' >alert('6')</script>");
                                        }
							}
						}
                catch (Exception ex)
					{
						Response.Write(""+ex);
					}
				}
			private void Show_Country()
			{
              try
              {
                  string sql1 = "select DISTINCT Country from PlanHrs ";
                  cmd = new SqlCommand(sql1,con);
                  da = new SqlDataAdapter(cmd);
                  DataSet ds1 = new DataSet();
                  da.Fill(ds1,"Country");
                  GridView4.DataSource = ds1.Tables[0];
                  GridView4.DataBind();
              }
              catch (Exception ex)
				{
                  Response.Write("" + ex);
				}
          }
        }
	}
Posted
Updated 23-Nov-14 8:57am
v2
Comments
Afzaal Ahmad Zeeshan 23-Nov-14 14:58pm    
Where actually is the error, I am not going to create a new Console application just to test and look for the error.
Ashish Kurne 23-Nov-14 15:22pm    
Hi Thanks for reply...

On Run, the code jumps directly to the grid ("alert 2") skipping the ("alert 1") and ("alert 8"). Its not giving any error. Build is successful, connections is okay. But on run, am not able to view the output. Had cross checked the xml also, but data is not visible in the grids (g1, g2, g3, g4). Getting confused, where it is going wrong?

Thanks and regards
MJ2014 23-Nov-14 15:31pm    
can pls tell me how many rows available in gridview4?
Ashish Kurne 23-Nov-14 15:36pm    
There are 27 rows in gridview4
Ashish Kurne 23-Nov-14 17:16pm    
Hi, this may be additional information. There are more than thousands of rows, but upon using "Distinct" there are only 27 rows available to populate the data. I have recreated entire solution again, including database table, still no errors, no results, the screen is yet blank... Please help..?

1 solution

This code is pretty awfull, if it is code developed, consider throwing the code behind away and starting from scratch only keeping the SQL.

With the structure of the code, i doubt the problem is with the SQL (although i honestly didn't investigate), just throw it all away and follow this simple example of how to do things right.
http://www.aspsnippets.com/Articles/ASPNet-Nested-GridViews-GridView-inside-GridView-with-Expand-and-Collapse-feature.aspx[^]

And get that SQL out of your code behind ;) must be server side or at least in datamanager classes or you're (always) in for a world of pain (if not now, then later) most of us abandoned that approach in ASP3 because over time, with maintenance and such it costed more than the dynamic and rapidity gave
 
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