0

I am creating a datatable and adding rows dynamically and am binding datatable to gridview when there are no records am displaying as no records found using emptydatatext but this is not working.Here is my code

  protected void show_fence_report(object sender, EventArgs e)
  {

    int drp_fence_id              = common.make_int(common.get_request("drp_fence"));
    string fence_start_date_time1 = common.get_request("fence_start_date_time");
    string fence_end_date_time1   = common.get_request("fence_end_date_time");

    //Delete data from datatable and gridview
    dt_fence.Clear();

    if (gridview_fence.DataSource != null)
    {
        ((DataView)gridview_fence.DataSource).Table.Clear();
    }
    gridview_fence.DataBind();
    gridview_fence.EmptyDataText = "Records Not Found";

    hid_fence_id.Value = drp_fence_id.ToString();
    hid_fence_start_datetime.Value = fence_start_date_time1;
    hid_fence_end_datetime.Value = fence_end_date_time1;

    display_fence_report();

    gridview_fence.EmptyDataText = "Records Not Found";

}

public void display_fence_report()
{

    string fence_id1 = "", fence_name1, fence_type1 = "", fence_status;
    float default_size = 100000, landmark_latitude1 = 0, landmark_longitude1 = 0, fence_latitude1 = 0, fence_longitude1 = 0, fence_size1 = 0, longitude_x = 0, latitude_y = 0;
    int points_polygon = 0;      


    ArrayList vertices_x = new ArrayList();
    ArrayList vertices_y = new ArrayList();

    query = "select latitude,longitude,added_date_time,speed,location,(select object_value from tracking_master_objects where object_id = a.object_id) as object from tracking_data_domain a where object_id in(select object_id from tracking_assign_fence a where fence_id = '" + hid_fence_id.Value + "' and is_active = '1') and (added_date_time between convert(datetime, '" + hid_fence_start_datetime.Value + "', 105) and convert(datetime, '" + hid_fence_end_datetime.Value + "', 105)) order by gps_id asc";

    dr = common.run_query(query, db, cm, dr, 0);
    if (dr.HasRows)
    {
        //To Build Fence Latitudes and Longitudes
        query = "select fence_id,fence_name,fence_type,landmark_latitude,landmark_longitude,fence_latitude,fence_longitude,fence_size from tracking_master_fences where domain_id = '" + domain_id1 + "' and fence_id = '" + hid_fence_id.Value + "' and is_active = '1'";

        dr2 = common.get_row(query, db2, cm2, dr2, 0);
        if (dr2.HasRows)
        {

            fence_id1 = dr2["fence_id"].ToString();
            fence_name1 = dr2["fence_name"].ToString();
            fence_type1 = dr2["fence_type"].ToString();
            landmark_latitude1 = common.make_float(dr2["landmark_latitude"].ToString());//fs_lat
            landmark_longitude1 = common.make_float(dr2["landmark_longitude"].ToString());//fs_long
            fence_latitude1 = common.make_float(dr2["fence_latitude"].ToString());//sec_lat
            fence_longitude1 = common.make_float(dr2["fence_longitude"].ToString());//sec_long
            fence_size1 = common.make_float(dr2["fence_size"].ToString());

        }
        dr2.Close();

        //Build POlygon Vertices
        if (fence_type1 == "4")
        {


            query = "select polygon_latitude,polygon_longitude from tracking_master_fence_polygons where fence_id = '" + hid_fence_id.Value + "' and domain_id = '" + domain_id1 + "'";
            dr1 = common.run_query(query, db1, cm1, dr1, 0);
            if (dr1.HasRows)
            {
                while (dr1.Read())
                {
                    vertices_x.Add(dr1["polygon_latitude"]);
                    vertices_y.Add(dr1["polygon_longitude"]);
                }
            }
            dr1.Close();
            points_polygon = vertices_x.Count;

        }

        //Create a Datatable of 14 rows
        dt_fence.Columns.Add("fence_id", typeof(string));
        dt_fence.Columns.Add("Date", typeof(string));
        dt_fence.Columns.Add("Speed", typeof(string));
        dt_fence.Columns.Add("Location", typeof(string));
        dt_fence.Columns.Add("landmark_latitude", typeof(string));
        dt_fence.Columns.Add("landmark_longitude", typeof(string));
        dt_fence.Columns.Add("fence_latitude", typeof(string));
        dt_fence.Columns.Add("fence_longitude", typeof(string));
        dt_fence.Columns.Add("latitude", typeof(string));
        dt_fence.Columns.Add("longitude", typeof(string));
        dt_fence.Columns.Add("fence_size", typeof(string));
        dt_fence.Columns.Add("fence_type", typeof(string));
        dt_fence.Columns.Add("fence_status", typeof(string));
        dt_fence.Columns.Add("object", typeof(string));


        while (dr.Read())
        {
            fence_status = "";
            float latitude = common.make_float(dr["latitude"].ToString());
            float longitude = common.make_float(dr["longitude"].ToString());

            if (fence_type1 == "1" || fence_type1 == "2")
            {

                if (((landmark_latitude1 * 100000) < (latitude * 100000) && (fence_latitude1 * 100000) > (latitude * 100000)) && ((landmark_longitude1 * 100000) < (longitude * 100000) && (fence_longitude1 * 100000) > (longitude * 100000)))
                {
                    fence_status = "Inside";
                }
                else
                {
                    fence_status = "Outside";
                }

            }
            else if (fence_type1 == "3")
            {
                float ft = ((latitude * 100000) - (landmark_latitude1 * 100000));
                float st = ((longitude * 100000) - (landmark_longitude1 * 100000));

                float sqrt = common.make_float(Math.Sqrt((ft * ft) + (st * st)).ToString());

                if (fence_size1 < sqrt)
                {
                    fence_status = "Out Side";
                }
                else
                {
                    fence_status = "In Side";
                }
            }
            else if (fence_type1 == "4")
            {
                longitude_x = common.make_float(dr["latitude"].ToString());
                latitude_y = common.make_float(dr["longitude"].ToString());

                int i = 0, j = 0, c = 0;
                for (i = 0, j = points_polygon - 1; i < points_polygon; j = i++)
                {
                    float vertices_y_i_val = common.make_float(vertices_y[i].ToString());
                    float vertices_y_j_val = common.make_float(vertices_y[j].ToString());
                    float vertices_x_i_val = common.make_float(vertices_x[i].ToString());
                    float vertices_x_j_val = common.make_float(vertices_x[j].ToString());

                    if (((vertices_y_i_val > latitude_y != (vertices_y_j_val > latitude_y)) &&
(longitude_x < (vertices_x_j_val - vertices_x_i_val) * (latitude_y - vertices_y_i_val) / (vertices_y_j_val - vertices_y_i_val) + vertices_x_i_val)))
                    {
                        c = 1;
                    }

                }

                if (c == 1)
                {
                    fence_status = "In Side";
                }
                else
                {
                    fence_status = "Out Side";
                }
            }

            dt_fence.Rows.Add(fence_id1, dr["added_date_time"], dr["speed"], dr["location"], landmark_latitude1, landmark_longitude1, fence_latitude1, fence_longitude1, latitude, longitude, fence_size1, fence_type1, fence_status, dr["object"]);

        }//End of while Loop

        //hid_ds_fence.Value = dt_fence.ToString();
        gridview_fence.DataSource = dt_fence;
        gridview_fence.DataBind();


    }
    dr.Close();

    fence_modalpopup.Show();

}
Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
user2257113
  • 21
  • 2
  • 4

2 Answers2

1

In your code you're assigning EmptyDataText value after DataBind. It should be before DataBind. If "Message" is not going to change, you should rather define in HTML source as suggested.

Nilesh Thakkar
  • 2,877
  • 1
  • 24
  • 43
0

You can use something like this

  <Columns>

    <Columns>
<EmptyDataTemplate>
        <asp:Label ID="lblEmptydata" runat="server" Text="No Data here"></asp:Label>
      </EmptyDataTemplate>
                        </asp:GridView>

Or can try this

in my remark gridview's row_databoundevent i wrote

if (e.Row.Cells[0].Text == "&nbsp;") // to display text when there is no data

{
e.Row.Cells[0].Text = "No Data Found";

}

Or can try this too

dt.Rows.Add(dt.NewRow());
                    grid1.DataSource = dt;
                    grid1.DataBind();
                    int totalcolums = grid1.Rows[0].Cells.Count;
                    grid1.Rows[0].Cells.Clear();
                    grid1.Rows[0].Cells.Add(new TableCell());
                    grid1.Rows[0].Cells[0].ColumnSpan = totalcolums;
                    grid1.Rows[0].Cells[0].Text = "No Data Found";
Darshan
  • 535
  • 1
  • 3
  • 16
  • i am genrating a fence report in vehicle tracking system in popup the above code is working fine.But when i open a poup also its displaying the text.Empty text should be displayed only when i hit a submit button – user2257113 Apr 17 '13 at 11:01
  • ok try to bind empty data text before data bind like this gridview_fence.EmptyDataText = "Records Not Found";gridview_fence.DataBind(); – Darshan Apr 17 '13 at 11:08
  • 1. If i give before databind it has problem as only when data is present for the first time when i hit a submit button the records will be displayed after that i select vehicle that has no data that time empty data text is displaying but if select vehicle which has no records for first time it is not displaying – user2257113 Apr 17 '13 at 11:24
  • its not displaying empty data text at all if i select the vehicle that has no records.But if i select the vehicles that has records and after that i select vehicle that has no records at that time empty data text is displayed – user2257113 Apr 17 '13 at 11:32
  • I have edited my answer try the section after OR..hope it will work – Darshan Apr 17 '13 at 11:37