I am trying to create a simple gridview that displays one table from my Oracle DB and allows me to delete a row. I am currently getting the error 'SQL Command Not Properly Ended' and after looking around on here it seems that Oracle doesn't allow joins on update statements. But I am not joining or doing an update statement. Can anyone see where the issue is coming from? Thanks in advance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.OracleClient;
namespace DatabaseTest
{
public partial class AvatarView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string avatarID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string deleteSql = "DELETE FROM Avatar WHERE AvatarID = :AvatarID; ";
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionString))
using (var cmd = new OracleCommand(deleteSql, con))
{
cmd.Parameters.Add(":AvatarID", OracleType.VarChar).Value = avatarID;
con.Open();
int deleted = cmd.ExecuteNonQuery();
}
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
}
Source code -
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="AvatarView.aspx.cs" Inherits="DatabaseTest.AvatarView" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="Gridview1_RowDeleting" AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="AVATARID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None" AllowPaging="True" Height="422px" Width="1020px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField="AVATARID" HeaderText="AVATARID" SortExpression="AVATARID" ReadOnly="True" />
<asp:BoundField DataField="AVATARNAME" HeaderText="AVATARNAME" SortExpression="AVATARNAME" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="STRENGTH_CURR" HeaderText="STRENGTH_CURR" SortExpression="STRENGTH_CURR" />
<asp:BoundField DataField="GENDER" HeaderText="GENDER" SortExpression="GENDER" />
<asp:BoundField DataField="HOARD" HeaderText="HOARD" SortExpression="HOARD" />
<asp:BoundField DataField="SPECIESID" HeaderText="SPECIESID" SortExpression="SPECIESID" />
<asp:BoundField DataField="USERID" HeaderText="USERID" SortExpression="USERID" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>" ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand="SELECT * FROM "AVATAR""></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Remove the semicolon from the SQL statement on this line:
string deleteSql = "DELETE FROM Avatar WHERE AvatarID = :AvatarID; ";
Related
I am unable to see my gridview in the page, no matter what I change. I have used Datasource and Databind, Visible is True, I am thinking at least I should see only the table but nothing. Also I am really sorry that I did not translate this, some things are in Spanish but I think you will understand the context despite the names are in spanish. If this is a problem I can edit and translate.
Thanks.
aspx file:
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Masterpage.Master" CodeBehind="ListarRemateCerrado.aspx.cs"
Inherits="Interfaz.ListarRemateCerrado" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<p>CIERRE DE REMATE</p>
<asp:ListBox ID="lstRemates" runat="server"></asp:ListBox>
<asp:GridView ID="grvRemates" runat="server" AutoGenerateColumns="False" AllowSorting="True" OnPageIndexChanging="grvRemates_PageIndexChanging"
OnSorting="grvRemates_Sorting" AllowPaging="True" PageSize="5" AutoGenerateSelectButton="True"
ShowHeaderWhenEmpty="True" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ID Remate" SortExpression="Remate">
<ItemTemplate>
<asp:Label ID="lblRemate" runat="server" Text='<%# Eval("Remate") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comision Rematador" SortExpression="Comision">
<ItemTemplate>
<asp:Label ID="lblComision" runat="server" Text='<%#Eval("Comision")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView><br />
<asp:GridView ID="grvRemateSeleccionado" runat="server" AllowSorting="True" PageSize="5" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:Button ID="btnRefrescar" runat="server" Text="Refrescar" OnClick="btnRefrescar_Click" />
<asp:Button ID="btnVolver" runat="server" Text="Volver" OnClick="btnVolver_Click" CausesValidation="false" /> <br />
<asp:Label ID="lblError" runat="server" ForeColor="Red"></asp:Label>
<asp:Label ID="lblCorrecto" runat="server"></asp:Label>
</asp:Content>
aspx.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dominio;
namespace Interfaz
{
public partial class ListarRemateCerrado : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lstRemates.DataSource = Agencia.Instancia().devolverRemates();
lstRemates.DataTextField = "Datos";
lstRemates.DataValueField = "numero";
lstRemates.DataBind();
if (!IsPostBack)
{
List<Remate> ordenada = Agencia.Instancia().devolverRemates();
ordenada.Sort(new Remate.OrdenarXRemate());
Session["sortedView"] = ordenada;
BindData(ordenada);
Session["filtro"] = "numero";
Session["direccion"] = SortDirection.Ascending;
}
}
protected void btnRefrescar_Click(object sender, EventArgs e)
{
List<Remate> ordenada = Agencia.Instancia().devolverRemates();
ordenada.Sort(new Remate.OrdenarXRemate());
Session["sortedView"] = ordenada;
BindData(ordenada);
Session["filtro"] = "numero";
Session["direccion"] = SortDirection.Ascending;
}
protected void btnVolver_Click(object sender, EventArgs e)
{
Response.Redirect("MenuPrincipalAdmin.aspx");
}
protected void BindData(List<Remate> r)
{
lstRemates.DataSource = r;
lstRemates.DataBind();
}
protected void grvRemates_Sorting(object sender, GridViewSortEventArgs e)
{
List<Remate> ordenada = (List<Remate>)Session["sortedView"];
if (e.SortExpression == "Remate") ordenada.Sort(new Remate.OrdenarXRemate());
if (e.SortExpression == "Comision") ordenada.Sort(new Remate.OrdenarXComision());
if (e.SortExpression == Session["filtro"].ToString().Trim())
{
SortDirection aux = (SortDirection)Session["direccion"];
if (SortDirection.Ascending == aux)
{
Session["direccion"] = SortDirection.Descending;
ordenada.Reverse();
}
else
{
Session["direccion"] = SortDirection.Ascending;
}
}
Session["filtro"] = e.SortExpression;
Session["sortedView"] = ordenada;
BindData(ordenada);
}
protected void grvRemates_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvRemates.PageIndex = e.NewPageIndex;
BindData((List<Remate>)Session["sortedView"]);
}
protected void grvRemates_SelectedIndexChanged(object sender, EventArgs e)
{
CargarPaquete();
}
private void CargarPaquete()
{
GridViewRow row = grvRemates.SelectedRow;
Label lbl = row.FindControl("lblRemate") as Label;
int codigo = int.Parse(lbl.Text);
grvRemateSeleccionado.DataSource = Agencia.Instancia().devolverLotesRemate(codigo);
grvRemateSeleccionado.DataBind();
}
}
}
You have nowhere set the call databind method for the grid,
grvRemates.DataSource= Agencia.Instancia().devolverRemates();
grvRemates.DataBind();
I'm trying to create a simple gridview for a table in my database. I am trying to enable the delete feature of the gridview but i'm getting a strange error when I hit delete on one of my rows. Does anyone know why i'm getting this issue? I am connected to my Oracle database through the server explorer and the connection string is correct. Here is a screenshot of the error I get when I run the page and click on a delete link. Thanks in advance!
Web.config -
<configuration>
<connectionStrings>
<add name="ConnectionString3" connectionString="Data Source=obiwan;User ID=ac9555f;Password=*******"
providerName="System.Data.OracleClient" />
AvatarView Source Code -
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="AvatarView.aspx.cs" Inherits="DatabaseTest.AvatarView" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="Gridview1_RowDeleting" AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" DataKeyNames="AVATARID" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="None" AllowPaging="True" Height="422px" Width="1020px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField="AVATARID" HeaderText="AVATARID" SortExpression="AVATARID" ReadOnly="True" />
<asp:BoundField DataField="AVATARNAME" HeaderText="AVATARNAME" SortExpression="AVATARNAME" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="STRENGTH_CURR" HeaderText="STRENGTH_CURR" SortExpression="STRENGTH_CURR" />
<asp:BoundField DataField="GENDER" HeaderText="GENDER" SortExpression="GENDER" />
<asp:BoundField DataField="HOARD" HeaderText="HOARD" SortExpression="HOARD" />
<asp:BoundField DataField="SPECIESID" HeaderText="SPECIESID" SortExpression="SPECIESID" />
<asp:BoundField DataField="USERID" HeaderText="USERID" SortExpression="USERID" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<SortedAscendingCellStyle BackColor="#FAFAE7" />
<SortedAscendingHeaderStyle BackColor="#DAC09E" />
<SortedDescendingCellStyle BackColor="#E1DB9C" />
<SortedDescendingHeaderStyle BackColor="#C2A47B" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>" ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>" SelectCommand="SELECT * FROM "AVATAR""></asp:SqlDataSource>
</div>
</form>
</body>
</html>
AvatarView 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.Configuration;
using System.Data.OracleClient;
namespace DatabaseTest
{
public partial class AvatarView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string avatarID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string deleteSql = "DELETE FROM Avatar WHERE AvatarID = :AvatarID; ";
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionString))
using (var cmd = new OracleCommand(deleteSql, con))
{
cmd.Parameters.Add(":AvatarID", OracleType.VarChar).Value = avatarID;
con.Open();
int deleted = cmd.ExecuteNonQuery();
}
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
}
You are using SQL Server specific classes to connect to an Oracle server. Use the Oracle ones OracleConnection or a more generic approach IDbConnection
Example using Oracle clases:
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString3"].ConnectionString))
I am trying to define the tfoot section and table rows for during the pre-render. Is there a way to define text input using DataTables Column Filter Add-on but i can not generate the tfoot table rows. What am i missing ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Home
{
public partial class Glossary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.PreRender += new EventHandler(GridView1_PreRender);
}
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count > 0)
{
//forces grid to render thead/th elements
GridView1.UseAccessibleHeader = true;
GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
This is the code for the form currently. I am wondering if there is anything wrong.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Glossary.aspx.cs" Inherits="Home.Glossary" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title spellcheck="true">Glossary</title>
<style type="text/css" media="all">
#import "DataTables-1.9.4/DataTables-1.9.4/media/css/jquery.dataTables_themeroller.css";
#form1 {
width: 100%;
}
</style>
<script src="JQuery-DataTables-ColumnFilter/media/js/jquery.dataTables.columnFilter.js"></script>
<script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.js"></script>
<script src="DataTables-1.9.4/DataTables-1.9.4/media/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function () {
$('#<%=GridView1.ClientID%>').dataTable()
.columnFilter();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="background: #A0A0A0">
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4" BorderStyle="None" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" Height="111px" Width="213px" style="margin-left: 0px; margin-right: 4px;">
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<TextBoxStyle Font-Size="0.8em" />
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
</asp:Login>
</AnonymousTemplate>
</asp:LoginView>
</div>
<asp:SqlDataSource ID="TedGlossary" runat="server" ConnectionString="<%$ ConnectionStrings:Glsry_Taylor %>" SelectCommand="SELECT * FROM [Glossary] ORDER BY [TermText]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="TermText,DefNbr,DefVerNbr" DataSourceID="TedGlossary"
EnableSortingAndPagingCallbacks="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
AutoGenerateColumns="False" AutoGenerateEditButton="False" style="margin-right: 0px" Width="100%" Height="332px">
<Columns>
<asp:BoundField DataField="TermText" HeaderText="Term" ReadOnly="True" SortExpression="Term" />
<asp:BoundField DataField="DefNbr" HeaderText="Number" ReadOnly="True" SortExpression="DefinitionNumber" />
<asp:BoundField DataField="DefVerNbr" HeaderText="Version" ReadOnly="True" SortExpression="DefinitinonVersionNumber" />
<asp:BoundField DataField="DefText" HeaderText="Definition" SortExpression="Definition" />
<asp:BoundField DataField="AmplifyingExplanationText" HeaderText="Amplifying Explanation" SortExpression="AmplifyingExplanationText" />
<asp:BoundField DataField="SeeAlsoText" HeaderText="See Also" SortExpression="See Also" />
<asp:BoundField DataField="AuthoritativeSrcText" HeaderText="Authoritative Source" SortExpression="AuthoritativeSrcText" />
<asp:BoundField DataField="ScopeName" HeaderText="Scope" SortExpression="Scope" />
<asp:BoundField DataField="DomnName" HeaderText="Domain" SortExpression="Domain" />
<asp:BoundField DataField="GovernanceStateName" HeaderText="Governance State" SortExpression="GovernanceStateName" />
<asp:BoundField DataField="LastUpdtTimestamp" HeaderText="Last Updated" SortExpression="LastUpdtTimestamp" />
</Columns>
<FooterStyle BackColor="Silver" />
</asp:GridView>
</form>
</body>
</html>
You need to set Gridview's attribute "ShowFooter" to True. It is set to False by default
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" />
I'm trying to build a dynamic nested collapsable gridview for a project in college.
I read this article which helped me a bit, but I'm still stuck.
the two relevant tables in my sql server are: member which contains info including id (primary key), and history table that is used to store history details about calls made with foreign key id that points to the member table. (each history row gets an int by identity-history_num).
I'd like that by a given id the grid will list all the history records which will be expendable to show the content field of the history table.
I did some tests and managed to get it to work, but not dynamic. I guess I need to create the gridview dynamically as well.
here is my aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script type="text/javascript">
function showNestedGridView(obj) {
var nestedGridView = document.getElementById(obj);
var imageID = document.getElementById('image' + obj);
if (nestedGridView.style.display == "none") {
nestedGridView.style.display = "inline";
imageID.src = "minus.png";
} else {
nestedGridView.style.display = "none";
imageID.src = "plus.png";
}
}
</script>
<body>
<form id="form1" runat="server">
<div id='div1' runat="server">
<asp:GridView ID="gridViewMaster" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" DataKeyNames="id"
ForeColor="Black" GridLines="None"
onrowdatabound="gridViewMaster_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:showNestedGridView('customerID-<%# Eval("id") %>');">
<img id="imagecustomerID-<%# Eval("history_num") %>" alt="Click to show/hide orders" border="0" src="plus.png" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="history_num" HeaderText="history_id" ReadOnly="True"
SortExpression="Member-ID" />
<asp:BoundField DataField="h_date" HeaderText="History-Date"
SortExpression="Member-Name" />
<asp:BoundField DataField="topic" HeaderText="History-topic"
SortExpression="Member-Name" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="customerID-<%# Eval("history_num") %>" style="display:none;position:relative;left:25px;" >
<asp:GridView ID="nestedGridView" runat="server" AutoGenerateColumns="False"
DataKeyNames="id">
<RowStyle VerticalAlign="Top" BackColor="White" ForeColor="#330099" />
<Columns>
<asp:BoundField DataField="content" HeaderText="content"
SortExpression="OrderDate" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<%-- <asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:igroup20_test2ConnectionString %>"
SelectCommand="select id, h_date, topic from history where id='038191904'"></asp:sqldatasource>--%>
</div>
</form>
</body>
</html>
and this is the code behind for now:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource sqlDS = new SqlDataSource();
sqlDS.ID = "sqlDS";
sqlDS.ConnectionString = "igroup20_test2ConnectionString";
sqlDS.SelectCommand = "select history_num, h_date, topic from history where id='038191904'";
div1.Controls.Add(sqlDS);
gridViewMaster.DataSource = sqlDS;
gridViewMaster.DataBind();
}
protected void gridViewMaster_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string his_num = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "history_num"));
GridView gridViewNested = (GridView)e.Row.FindControl("nestedGridView");
SqlDataSource sqlDataSourceNestedGrid = new SqlDataSource();
sqlDataSourceNestedGrid.ConnectionString = ConfigurationManager.ConnectionStrings["igroup20_test2ConnectionString"].ConnectionString;
sqlDataSourceNestedGrid.SelectCommand = "SELECT content from history where history_num='" + his_num + "'";
gridViewNested.DataSource = sqlDataSourceNestedGrid;
gridViewNested.DataBind();
}
}
}
history table fields are:
history_num
id
h_date
topic
content
I'm using a gridview with editing and updating enabled and using the UpdateCommand in the SqlDataSource to update the data. But whenever I try to update a record I get the exception
Function or procedure has too many arguments specified
Code:
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Manager_UnApprovedActivites : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string Appproval = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
SqlDataSource1.UpdateParameters["id"].DefaultValue = id.ToString();
SqlDataSource1.UpdateParameters["approval"].DefaultValue = Appproval.ToString();
SqlDataSource1.Update();
}
}
ASP.net:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="UnApprovedActivites.aspx.cs" Inherits="Manager_UnApprovedActivites" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="AcitivityId" DataSourceID="SqlDataSource1"
Height="273px" Width="976px" AutoGenerateEditButton ="True">
<Columns>
<asp:BoundField DataField="AcitivityId" HeaderText="AcitivityId" InsertVisible="False" ReadOnly="True" SortExpression="AcitivityId" />
<asp:BoundField DataField="ActiviityName" HeaderText="ActiviityName" SortExpression="ActiviityName" />
<asp:BoundField DataField="ActivityLocation" HeaderText="ActivityLocation" SortExpression="ActivityLocation" />
<asp:BoundField DataField="ActivityStartDate" HeaderText="ActivityStartDate" SortExpression="ActivityStartDate" />
<asp:BoundField DataField="ActivityDueDate" HeaderText="ActivityDueDate" SortExpression="ActivityDueDate" />
<asp:BoundField DataField="ActivityDescription" HeaderText="ActivityDescription" SortExpression="ActivityDescription" />
<asp:BoundField DataField="ActvityTypeId" HeaderText="ActvityTypeId" SortExpression="ActvityTypeId" />
<asp:BoundField DataField="BarchId" HeaderText="BarchId" SortExpression="BarchId" />
<asp:CheckBoxField DataField="Approval(Authority)" HeaderText="Approval(Authority)" SortExpression="Approval(Authority)" />
<asp:CheckBoxField DataField="ApprovalManager" HeaderText="ApprovalManager" SortExpression="ApprovalManager" />
<asp:BoundField DataField="DateAdded" HeaderText="DateAdded" SortExpression="DateAdded" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WCA DatabaseConnectionString2 %>"
SelectCommand="SELECT * FROM [Activities]"
UpdateCommand="ApproveActivity"
UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="id" Type="Int64" />
<asp:Parameter Name="approval" Type="Boolean" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
in following Line :
string Appproval = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
that return CheckBox Text Not checkBox value , instead use this
CheckBox chb= ((CheckBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]);
if (chb != null)
{
SqlDataSource1.UpdateParameters["approval"].DefaultValue =chb.Checked;
}