Azm Amn

Azm Amn

  • NA
  • 108
  • 17.7k

GridView not showing in browser

Jul 29 2015 1:17 AM
Design
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h1 style="text-align: center">Update FIR</h1>
<form id="form1" runat="server">
<div >
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="true" DataKeyNames="FIR_ID,Police_ID" Visible="true" EnableViewState="true" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="FIR_ID" HeaderText="FIR_ID" ReadOnly="True" SortExpression="FIR_ID" />
<asp:BoundField DataField="Police_ID" HeaderText="Police_ID" ReadOnly="true" SortExpression="Police_ID" />
<asp:BoundField DataField="Victim_FullName" HeaderText="Victim_FullName" SortExpression="Victim_FullName" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="District" HeaderText="District" SortExpression="District" />
<asp:BoundField DataField="PoliceStation" HeaderText="PoliceStation" SortExpression="PoliceStation" />
<asp:BoundField DataField="Act" HeaderText="Act" SortExpression="Act" />
<asp:BoundField DataField="Section" HeaderText="Section" SortExpression="Section" />
<asp:BoundField DataField="Occurence_Address" HeaderText="Occurence_Address" SortExpression="Occurence_Address" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:PoliceConnectionString %>" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [FIR]" UpdateCommand="UPDATE [FIR] SET [Victim_FullName] = @Victim_FullName, [Date] = @Date, [District] = @District, [PoliceStation] = @PoliceStation, [Act] = @Act, [Section] = @Section, [Occurence_Address] = @Occurence_Address WHERE [FIR_ID] = @original_FIR_ID AND [Police_ID] = @original_Police_ID AND [Victim_FullName] = @original_Victim_FullName AND [Date] = @original_Date AND [District] = @original_District AND [PoliceStation] = @original_PoliceStation AND [Act] = @original_Act AND [Section] = @original_Section AND (([Occurence_Address] = @original_Occurence_Address) OR ([Occurence_Address] IS NULL AND @original_Occurence_Address IS NULL))">
<UpdateParameters>
<asp:Parameter Name="Police_ID" Type="String" />
<asp:Parameter Name="Victim_FullName" Type="String" />
<asp:Parameter Name="Date" Type="String" />
<asp:Parameter Name="District" Type="String" />
<asp:Parameter Name="PoliceStation" Type="String" />
<asp:Parameter Name="Act" Type="String" />
<asp:Parameter Name="Section" Type="String" />
<asp:Parameter Name="Occurence_Address" Type="String" />
<asp:Parameter Name="original_FIR_ID" Type="String" />
<asp:Parameter Name="original_Police_ID" Type="String" />
<asp:Parameter Name="original_Victim_FullName" Type="String" />
<asp:Parameter Name="original_Date" Type="String" />
<asp:Parameter Name="original_District" Type="String" />
<asp:Parameter Name="original_PoliceStation" Type="String" />
<asp:Parameter Name="original_Act" Type="String" />
<asp:Parameter Name="original_Section" Type="String" />
<asp:Parameter Name="original_Occurence_Address" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
 
 
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace Final_Project
{
public partial class Update_FIR : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=AHZAM-PC\\AHZAM;Initial Catalog=Police;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) // If you not place this check then you will get the old values because GridView in Bind on every postback
{
DataBind();
}
}
private void BindGrid() // function for binding gridview
{
DataTable dt = new DataTable();
dt.Columns.Add("FIR_ID");
dt.Columns.Add("Police_ID");
dt.Columns.Add("Victim_FullName");
dt.Columns.Add("Date");
dt.Columns.Add("District");
dt.Columns.Add("PoliceStation");
dt.Columns.Add("Act");
dt.Columns.Add("Section");
dt.Columns.Add("Occurence Address");
DataRow r = dt.NewRow();
r[0] = "FIR_ID 1";
r[1] = "Police_ID 1";
r[2] = "Victim_FullName 1";
r[3] = "Date 1";
r[4] = "District 1";
r[5] = "PoliceStation 1";
r[6] = "Act 1";
r[7] = "Sections 1";
r[8] = "Occurence Address 1";
DataRow r1 = dt.NewRow();
r1[0] = "FIR_ID 2";
r1[1] = "Police_ID 2";
r1[2] = "Victim_FullName 2";
r1[3] = "Date 2";
r1[4] = "District 2";
r1[5] = "PoliceStation 2";
r1[6] = "Act 2";
r1[7] = "Sections 2";
r1[8] = "Occurence Address 2";
dt.Rows.Add(r);
dt.Rows.Add(r1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; // setting new index
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string newvalue = ((TextBox)row.Cells[0].Controls[0]).Text;
GridView1.EditIndex = -1; // Again reset
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; // reseting grid view
BindGrid();
}
}
}
After Execution the browser only display the heading and no Grid view at all. help please. 

Answers (13)