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?
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++)
{
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>");
Planned_Hrs[count] = 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]);
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);
}
}
}
}