I cannot see where I'm doing wrong. No data is populated into the grid view.
HERE is my aspx gridview code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="TeamMemberId"
Width="772px" CellPadding="4"
ForeColor="#333333" GridLines="Both" Height="16px"
onselectedindexchanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="TeamMemberLanId" HeaderText="LANID"
InsertVisible="False" ReadOnly="True" SortExpression="TeamMemberLanId" />
<asp:BoundField DataField="First Name" HeaderText="FirstName"
SortExpression="Name" />
<asp:BoundField DataField="Last Name" HeaderText="LastName"
SortExpression="Name" />
<asp:BoundField DataField="IsActive" HeaderText="Status" />
<asp:TemplateField HeaderText="Select">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
and here is my code behind.
public void BindData()
{
try
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mystring"].ToString());
cmd.CommandText = "Select TeamMemberLanId, FirstName, LastName,IsActive from dbo.LoanOwnerStamp ORDER BY 2,3";
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Open();
cmd.ExecuteNonQuery();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
else
{
//Bind Empty grdview with Columns names in Header and "No Team Member record found" message if no records are found in the database
BindEmptyGridWithHeader(GridView1, ds);
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + ex.Message.ToString() + "');", true);
}
finally
{
con.Close();
}
}
Hope another pair of eyes can detect error in my gridview or code behind. BTW , da,ds, cmd, con are defined in the class.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
Query returns several rows,so there are no issues there.
Related
I have datagrid where I display my database with movies.
I made textbox and I would like to filter this data grid when im writing
This is my event code
protected void wyszukaj_txt_TextChanged(object sender, EventArgs e)
{
string qry = "select tytul, rok_produkcji, gatunek, cena from filmy where tytul like'" + szukaj.Text + "'";
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(qry, con);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
and this is a code of datagrid
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Vertical" Height="356px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" RowHeaderColumn="cena" style="z-index: 1; left: 154px; top: 519px; position: absolute; height: 356px; width: 1120px; margin-right: 0px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="id_filmu" HeaderText="ID" ReadOnly="True" SortExpression="id_filmu" Visible="False" />
<asp:BoundField DataField="tytul" HeaderText="Tytuł" SortExpression="tytul" />
<asp:BoundField DataField="gatunek" HeaderText="Gatunek" SortExpression="gatunek" />
<asp:BoundField DataField="rok_produkcji" HeaderText="Rok produkcji" SortExpression="rok_produkcji" />
<asp:BoundField DataField="cena" HeaderText="Cena" SortExpression="cena" />
<asp:TemplateField HeaderText="Kup">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Kup" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" Text="Kup" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
When Im trying to press enter I have an exception. Additional information: Elements DataSource and DataSourceID are both defined in 'GridView1'.
I understand that I have duplication of this datasource but I have no idea how can I change it. I tried to put 'DataSourceID' where now is only (.cs file) DataSource but then the I have error about the type (dataset)
Anybody help please?
Thanks a lot!
In your query
string qry = "select tytul, rok_produkcji, gatunek, cena from filmy where tytul like'" + szukaj.Text + "'";
The column id_filmu doesn't exist.
I have the following nested Girdview. The issue is when the parentgrid is loaded, it is showing 2 childgrids, one by the expand img, one below each row when I click on the expand img. The one by the expand img is unexpected and unwanted. Can someone help me fix this issue?
ASPX
<asp:GridView ID="gvLotDetails" runat="server" Width="600px" BackColor="White" AutoGenerateColumns="False" OnRowDataBound="gvLotDetails_DataBound" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="169px" DataKeyNames="ItemNumber">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="Images/plus.png" />
<asp:Panel ID="pnlLotDetailsExtend" runat="server">
<asp:GridView ID="gvLotDetailsExpand" runat="server" AutoGenerateColumns="false" DataKeyNames="Slab">
<Columns>
<asp:BoundField DataField="Slab" SortExpression="Slab" ReadOnly="True" HeaderText="Slab" />
<asp:BoundField DataField="Size" SortExpression="Size" ReadOnly="True" HeaderText="Size" />
<asp:BoundField DataField="Sqft" SortExpression="Sqft" ReadOnly="True" HeaderText="Sqft" />
<asp:BoundField DataField="Block" SortExpression="Block" ReadOnly="True" HeaderText="Block" />
<asp:BoundField DataField="Totalweight" SortExpression="Totalweight" ReadOnly="True" HeaderText="Totalweight" />
<asp:BoundField DataField="country" SortExpression="country" ReadOnly="True" HeaderText="Country" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Right" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000" />
<AlternatingRowStyle BackColor="White" HorizontalAlign="Left" Font-Underline="true" />
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemNumber" SortExpression="ItemNumber"
ReadOnly="True" HeaderText="ItemNumber" />
<asp:BoundField DataField="whse" SortExpression="whse" ReadOnly="True"
HeaderText="Warehouse" />
<asp:BoundField DataField="Bundle" SortExpression="Bundle" ReadOnly="True"
HeaderText="Bundle" />
</Columns>
<PagerSettings Position="TopAndBottom" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Right" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000" />
<AlternatingRowStyle BackColor="White" HorizontalAlign="Left" Font-Underline="true" />
</asp:GridView>
JavaScript
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
$(this).attr("src", "Images/minus.png");
});
$("[src*=minus]").live("click", function () {
$(this).attr("src", "Images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
Code Behind
A button event triggers GetDeatils()
private void GetDetails()
{
query = "SELECT ItemNumber, WHSE, Bundle FROM labels_slab_details WHERE lot= '" + this.txtLot.Text.Trim() + "' and containernumber='" + this.ddlCont.SelectedValue +
"' GROUP BY ItemNumber, WHSE, Bundle ORDER BY Bundle, ItemNumber";
cn = new SqlConnection(cnBCStr);
SqlDataAdapter da = new SqlDataAdapter(query, cn);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
this.gvLotDetails.DataSource = ds.Tables[0];
this.gvLotDetails.DataBind();
this.gvLotDetails.Visible = true;
}
else
{
this.gvLotDetails.Visible = false;
this.errLabelDetail.Text = "* Create Bundle Data First! ";
return;
}
}
protected void gvLotDetails_DataBound(object Sender, GridViewRowEventArgs Ea)
{
if (Ea.Row.RowType == DataControlRowType.DataRow)
{
string ItemNumber = gvLotDetails.DataKeys[Ea.Row.RowIndex]["ItemNumber"].ToString();
GridView gvLotDetailsExpand = Ea.Row.FindControl("gvLotDetailsExpand") as GridView;
gvLotDetailsExpand.DataSource = GetData(string.Format("SELECT slab, size, sqft, block, totalweight, country FROM labels_slab_details WHERE LOT='" + this.txtLot.Text + "' AND ItemNumber='" + ItemNumber + "'"));
gvLotDetailsExpand.DataBind();
}
}
private static DataTable GetData(string query)
{
string cnBCStr = System.Configuration.ConfigurationManager.ConnectionStrings["conBarcodes_SQLWeb"].ConnectionString;
using (SqlConnection cn = new SqlConnection(cnBCStr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = cn;
da.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
}
}
I updated the code to remove some changes I made for testing the issue.
I found the answer. The extra childGrid can be hidden by updating the panel parameter.
<asp:Panel ID="pnlLotDetailsExtend" runat="server" Style="display: none">
I have a form where I upload documents to Gridview and then save to the database.
When I want to load the form, I can't get the document info loaded to my GridView. I have put even an AlertMessage to know if it reads the files and it does. So I get the Message "Table is not empty" when loading the file. I don't know what else, I have to do in order to load the info to the grid.
Please, help me with it. Here is my code:
<asp:GridView ID="gvUploadDocumentList" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" EmptyDataText="No files uploaded"
ForeColor="Black" GridLines="Horizontal" style="text-align: center;
margin:0px auto;" Width="800px">
<Columns>
<asp:BoundField DataField="Filename" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" OnClick="DeleteFile" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if(!page.IsPostBack)
{
LoadRequestInfo();
LoadDocuments();
}
}
public void LoadDocuments()
{
CommonCollection c = new CommonCollection();
var CTS = c.COMPTRACKERCONNECTIONSTRING();
var CTS_conn = new SqlConnection(CTS.ConnectionString);
try
{
SqlCommand cmd = new SqlCommand("CTS_GetDocuments", CTS_conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#RequestId", "1F0C0732-34CD-48DC-9730-268FE17BAF03"));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
CTS_conn.Open();
da.Fill(ds);
CTS_conn.Close();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
gvUploadDocumentList.DataSource = ds;
gvUploadDocumentList.DataBind();
ShowAlertMessage("Table is not empty");
}
else
ShowAlertMessage("No Data");
}
catch (Exception ex)
{
ShowAlertMessage(ex.Message.ToString());
}
}
Just based on what you are listing, I don't see any code that calls the LoadDocuments() method. If this isn't called in the PageLoad() method, and there is no button or other even that calls it, you will never get the data into your gridview.
An easy way to verify if this is happening is to put a break into your code at the beginning of the method and if VS doesn't start stepping through your code, that's your issue.
I have problem in display gridview in my project asp.net .
This is my Gridview.
<asp:GridView ID="GridView1" runat="server" Width="100%" ViewStateMode="Enabled" AutoGenerateColumns="true" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateSelectButton="true" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
This is my code in page load.
protected void Page_Load(object sender, EventArgs e)
{
try
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = connectionstring;
conn.Open();
string sql = "select * from merchant";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error : " + ex.ToString());
Label1.Text = ex.ToString();
}
}
why my Gridview can't display?. Someone please fix my problem.
Thanks
Change Your ASP.NET CODE
Exp:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="ID" DataField="IDUser" ItemStyle-Width="50"/>
<asp:BoundField HeaderText="Name" DataField="Name" ItemStyle-Width="200"/>
<asp:BoundField HeaderText="Username" DataField="UserName" ItemStyle-Width="200"/>
</Columns>
</asp:GridView>
I am trying to display a gridview from a sql database using a query on a dropdownlist selected index changed for the dropdownlist.
Here is the code I currently have for this that I thought should work:
I am not getting any errors, the gridview just won't show up when i select a package from the dropdownlist.
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Clear();
DropDownList1.Items.Add(new ListItem("Select Package", "Select Package"));
DropDownList1.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct Package_Name from dbo.PackageDetail";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Package_Name";
DropDownList1.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["datarepConnectionString"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT [ID], [PartNumber], [Qty], [PartDescription], [PR1], [PR2], [PR3], [EXTPR2], [EXTPR1], [EXTPR3] FROM [PackageDetail] where [Package_Name] = '" + DropDownList1.Text + "' ";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
ASP.NET:
<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" EnableViewState="true" >
</asp:DropDownList>
</center>
<p> </p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" GridLines="Vertical" AllowSorting="True" Height="160px" Width="374px" EnableViewState="true" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="PartNumber" HeaderText="PartNumber" SortExpression="PartNumber" />
<asp:BoundField DataField="PartDescription" HeaderText="PartDescription" SortExpression="PartDescription" />
<asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
<asp:BoundField DataField="PR1" HeaderText="PR1" SortExpression="PR1" />
<asp:BoundField DataField="PR2" HeaderText="PR2" SortExpression="PR2" />
<asp:BoundField DataField="PR3" HeaderText="PR3" SortExpression="PR3" />
<asp:BoundField DataField="EXTPR2" HeaderText="EXTPR2" SortExpression="EXTPR2" />
<asp:BoundField DataField="EXTPR1" HeaderText="EXTPR1" SortExpression="EXTPR1" />
<asp:BoundField DataField="EXTPR3" HeaderText="EXTPR3" SortExpression="EXTPR3" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Help is very appreciated thank you.