deleting row from gridview - c#

I have 2 tables MachineGroups and Machines. MachineGroups has columns:
MachinegroupID
MachineGroupName
MachineGroupDesc
And Machines has columns:
MachineGroupID (FK)
MachineID
MachineName
Machinedesc
Now I want to delete a machinegroup but not the ones that have machines in it.
DELETE FROM MachineGroups
WHERE MachineGroupID NOT IN (SELECT DISTINCT MachineGroupID FROM Machines)
This will delete all the rows in the gridview which have no. of machines= 0
What i want is to delete only the row whose delete link is clicked...
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="1" CellSpacing="2"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
Width="100%" ondatabound="GridView1_DataBound1"
onrowdatabound="GridView1_RowDataBound1">
<RowStyle BackColor="#D0D8E8" ForeColor="#333333" Height="35px" />
<Columns>
<asp:BoundField DataField="MachineGroupID" HeaderText="MachineGroupID"
InsertVisible="False" ReadOnly="True" SortExpression="MachineGroupID"
Visible="False" />
<asp:BoundField DataField="MachineGroupName" HeaderText="MachineGroupName"
SortExpression="MachineGroupName" />
<asp:BoundField DataField="MachineGroupDesc" HeaderText="MachineGroupDesc"
SortExpression="MachineGroupDesc" />
<asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded"
SortExpression="TimeAdded" />
<asp:TemplateField HeaderText="CanBeDeleted" SortExpression="CanBeDeleted"
Visible="False">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("CanBeDeleted") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Bind("CanBeDeleted") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="No. of PCs" HeaderText="No. of PCs" ReadOnly="True"
SortExpression="No. of PCs" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>"
SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted"
DeleteCommand="DELETE FROM MachineGroups
WHERE MachineGroupID NOT IN (SELECT DISTINCT MachineGroupID FROM Machines)">
<DeleteParameters>
<asp:Parameter Name="original_MachineGroupID" />
<asp:Parameter Name="original_MachineGroupName" />
<asp:Parameter Name="original_MachineGroupDesc" />
<asp:Parameter Name="original_CanBeDeleted" />
<asp:Parameter Name="original_TimeAdded" />
</DeleteParameters>
</asp:SqlDataSource>

Not tested, but the direction should be right:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>"
SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted"
DeleteCommand="DELETE FROM MachineGroups WHERE MachineGroupID = #MachineGroupID">
<DeleteParameters>
<asp:Parameter Name="MachineGroupID" />
</DeleteParameters>
</asp:SqlDataSource>

Related

How to take ID of a row in gridview when you press a button in a row

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="IDProject" DataSourceID="SqlDataSource1" GridLines="Vertical" style="margin-top:2rem" Width="1056px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField SortExpression="ProjectLeader" HeaderText="Project Leader">
<ItemTemplate>
<asp:Label Text='<%# Bind("ProjectLeader") %>' ID="lvProjectLeader" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" SortExpression="ProjectName" />
<asp:TemplateField HeaderText="Start date:" SortExpression="StartDate">
<ItemTemplate>
<asp:Label Text='<%# Bind("StartDate") %>' ID="lbStartDate" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client Name" SortExpression="Client name">
<ItemTemplate>
<asp:Label Text='<%# Bind("Client name") %>' ID="lbClientName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employees">
<ItemTemplate>
<asp:Button Text="Open" ID="EmployeesOpen" OnClick="EmployeesOpen_Click" runat="server" CssClass="btn btn-outline-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="Insert into Project values(#ProjectName, #ClientID,#ProjectLeader,#StartDate)"
SelectCommand="select IDProject, ProjectLeader, ProjectName, StartDate, ClientID, Client.Name from Project inner join Client on IDClient = ClientID"
UpdateCommand="Update Project set ProjectName = #ProjectName where IDProject = #IDProject">
<InsertParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="ClientID" />
<asp:Parameter Name="ProjectLeader" />
<asp:Parameter Name="StartDate" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="IDProject" />
</UpdateParameters>
</asp:SqlDataSource>
I made a gridview table for Projects which has values ProjectName, ClientID, ProjectLeader, StartDate and all the employees that work on that Project. Since i cant fit all the employees that work on a single project inside a small table i made a button inside that column that redirects you to another page where you can see table with all the employees. The employees are in another table in sql and i made third table which connects Projects and employees and it only contains ProjectID and EmployeeID. I have a button inside every row but i cant find a way how to make in code behind so that it takes the id of that project and prints on another page only employees that are working on that project.
public partial class Project : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EmployeesOpen_Click(object sender, EventArgs e)
{
Response.Redirect("Employees_On_Project.aspx");
}
}
You will find what you are looking for here : https://forums.asp.net/t/1957409.aspx?Gridview+get+SelectedRow+in+button+click+event

How make many controlParameter to filter GridView in ASP.NET?

I try to filter the contents of my gridview based on the values ​​of DropDownList. I managed to filter my gridview with a DropDownList but how to filter with multiple DropDownList?
This is my aspx code :
<asp:SqlDataSource ID="SqlDataSourceName" runat="server" ConnectionString="<%$ ConnectionStrings:connexionBase %>"
SelectCommand="SELECT DISTINCT nom FROM Utilisateur"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceEntite" runat="server" ConnectionString="<%$ ConnectionStrings:connexionBase %>"
SelectCommand="SELECT DISTINCT entite FROM Utilisateur"></asp:SqlDataSource>
<div style="height: 350px; width: 100%; overflow: auto;">
<asp:GridView ID="GridView1" Width="100%" Height="100%" HeaderStyle-BackColor="#21e3f0"
HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false" DataSourceID="GridDataSource"
ShowHeader="true">
<Columns>
<asp:BoundField DataField="Nom" HeaderText="Nom" ItemStyle-Width="80" />
<asp:BoundField DataField="Objectif operationnel" HeaderText="Objectif Opérationnel"
ItemStyle-Width="150" />
<asp:BoundField DataField="Description activite" HeaderText="Description activité"
ItemStyle-Width="80" />
<asp:BoundField DataField="Entite" HeaderText="Entité" ItemStyle-Width="80" />
<asp:BoundField DataField="Debut" HeaderText="Début" ItemStyle-Width="30" />
<asp:BoundField DataField="Fin" HeaderText="Fin" ItemStyle-Width="30" />
<asp:BoundField DataField="Cle" HeaderText="Clé" ItemStyle-Width="30" />
<asp:BoundField DataField="MAP" HeaderText="Map" ItemStyle-Width="30" />
<asp:BoundField DataField="SNS" HeaderText="SNS" ItemStyle-Width="30" />
</Columns>
</asp:GridView>
</div>
<!-- SQL DataSource avec un filterExpression pour filter le tableau selon la valeur des ListBox choisi-->
<asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:connexionBase %>"
SelectCommand="SELECT [Nom], [Objectif operationnel], [Description activite], [Entite], [Debut], [Fin], [Cle], [MAP], [SNS] FROM reponse"
FilterExpression="Nom = '{0}'">
<FilterParameters>
<asp:ControlParameter Name="Nom" ControlID="NomPersonne" PropertyName="SelectedValue"
ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="Nom" ControlID="NomEntite" PropertyName="SelectedValue"
ConvertEmptyStringToNull="false" />
</FilterParameters>
</asp:SqlDataSource>
EDIT : My DropDownList controls (choosen value by the user to filter the gridview)
<asp:DropDownList ID="NomPersonne" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourceName"
DataTextField="nom" DataValueField="nom" AppendDataBoundItems="true" CssClass="DropDownList">
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
<asp:DropDownList ID="NomEntite" runat="server" AutoPostBack="True" DataSourceID="SqlDataSourceEntite"
DataTextField="entite" DataValueField="entite" AppendDataBoundItems="true" CssClass="DropDownList">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
your filter expression should be look like this:
FilterExpression="Nom like '%{0}%' OR Entite like '%{1}%'">
and the expression could be vary depends on your field's data type and your conditions,
for example if your datafield values are integers and the condition is AND then you could have something like this:
FilterExpression="Nom={0} AND Entite={1}">
and you should also set a right name on <FilterParameters> for Name properties like this:
<FilterParameters>
<asp:ControlParameter Name="Nom" ControlID="NomPersonne"
PropertyName="SelectedValue" ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="Entite" ControlID="NomEntite"
PropertyName="SelectedValue" ConvertEmptyStringToNull="false" />
</FilterParameters>

Gridview not updating value

I am trying to make my gridview update a SQL record but it is not updating the values.
Here is the aspx code:
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
onrowcancelingedit="GridView1_RowCancelling"
OnRowDeleting ="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
DataKeyNames="RID"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:TemplateField Visible="false" HeaderText="RID">
<ItemTemplate>
<asp:Label runat="server" ID="RID" Text='<%#Bind("RID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Short Description">
<ItemTemplate>
<asp:Label runat="server" ID="short_lbl" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="SHORT_DESCRIPTION" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
<asp:RequiredFieldValidator runat="server" ID="valShort" ControlToValidate="SHORT_DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server" ID="desc_label" Text='<%#Bind("DESCRIPTION") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="DESCRIPTION" Text='<%#Bind("DESCRIPTION") %>' />
<asp:RequiredFieldValidator runat="server" ID="valLast" ControlToValidate="DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the update function itself.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
Label RID = GridView1.Rows[e.RowIndex].FindControl("RID") as Label;
TextBox SHORT_DESCRIPTION = GridView1.Rows[e.RowIndex].FindControl("SHORT_DESCRIPTION") as TextBox;
TextBox DESCRIPTION = GridView1.Rows[e.RowIndex].FindControl("DESCRIPTION") as TextBox;
String UpdateQuery = string.Format("UPDATE TAXONOMIES SET SHORT_DESCRIPTION='{0}', DESCRIPTION='{1}' WHERE RID = {2}", SHORT_DESCRIPTION.Text, DESCRIPTION.Text, Convert.ToInt32(RID.Text));
GridView1.EditIndex = -1;
BindGridData(UpdateQuery);
}
If I put a breakpoint and look at the value, I can see that is is not the newly typed in value. I'm not sure what is going on.
I think the problem is you are binding you grid on the page-load event.
try binding it in if (!IsPostBack)
Example
private void Page_Load()
{
if (!IsPostBack)
{
//bind your grid here.
}
}
use this code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="regGroupID" HeaderText="regGroupID"
SortExpression="regGroupID" />
<asp:BoundField DataField="amountReceived" HeaderText="amountReceived"
SortExpression="amountReceived" />
<asp:BoundField DataField="other" HeaderText="other" SortExpression="other" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Table]"
UpdateCommand="UPDATE [Table] SET [regGroupID] = #regGroupID, [amountReceived] = #amountReceived, [other] = #other WHERE [id] = #id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
<asp:Parameter Name="other" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
here I set id as the primary key
table structure is
Try changing the Eval to Bind. Eval is read-only so you can't actually update the value.

grid view won't update database (no errors)

I have a page with a details view and a grid view (each with their own data source. I can't figure out why the update says it's successful but don't update the table. the details view works fine. The grid view is only suppose to show data related to what's in the details view. If I hard code the values in the source it works but when I change it back it blows up. here's my code
<asp:SqlDataSource OnUpdated="dsCar_Updated" ID="dsCar" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT Car.* FROM Car WHERE (VIN = ?)"
UpdateCommand="UPDATE Car SET [Branch ID] = ?, [State Registration] = ?, [License Plate] = ?, Color = ?, Model = ?, [Car Year] = ?, [Plate Expiration] = ? WHERE (VIN = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="?" QueryStringField="VIN" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataSourceID="dsCar" Height="50px" Width="265px" CellPadding="4"
ForeColor="#333333" GridLines="None" DataKeyNames="VIN" DefaultMode="Edit">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:BoundField DataField="VIN" HeaderText="VIN" ReadOnly="True"
SortExpression="VIN" />
<asp:TemplateField HeaderText="Branch Name" SortExpression="Branch ID">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Branch Name"
DataValueField="Branch ID" SelectedValue='<%# Bind("[Branch ID]") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource3" DataTextField="Branch Name"
DataValueField="Branch ID" SelectedValue='<%# Bind("[Branch ID]") %>'>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Branch]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Branch Name"
DataValueField="Branch ID" Enabled="False" SelectedValue='<%# Bind("[Branch ID]") %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="State Registration" HeaderText="State Registration"
SortExpression="State Registration" />
<asp:BoundField DataField="License Plate" HeaderText="License Plate"
SortExpression="License Plate" />
<asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource4" DataTextField="Model Name"
DataValueField="ModelID" SelectedValue='<%# Bind("Model") %>'>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Model") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Model]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource4" DataTextField="Model Name"
DataValueField="ModelID" SelectedValue='<%# Bind("Model") %>'
Enabled="False">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Car Year" HeaderText="Car Year"
SortExpression="Car Year" />
<asp:TemplateField HeaderText="Plate Expiration"
SortExpression="Plate Expiration">
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT * FROM [Car]"></asp:SqlDataSource>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"
VisibleDate='<%# Eval("[Plate Expiration]") %>'
SelectedDate='<%# Bind("[Plate Expiration]") %>'></asp:Calendar>
<asp:TextBox ID="txtDateEdit" runat="server"
Text='<%# Bind("[Plate Expiration]") %>'></asp:TextBox>
<br />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"
VisibleDate='<%# Eval("[Plate Expiration]") %>'></asp:Calendar>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("[Plate Expiration]") %>' Visible="false"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<br />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("[Plate Expiration]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource OnUpdated="dsMaintance_Updated" ID="dsMaintance" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
DeleteCommand="DELETE FROM [Maintenance] WHERE [MainteanceID] = ?"
InsertCommand="INSERT INTO [Maintenance] ([MainteanceID], [VIN], [Procedure ID], [Date]) VALUES (?, ?, ?, ?)"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT MainteanceID, VIN, [Procedure ID], [Date] FROM Maintenance WHERE (VIN = ?)"
UpdateCommand="UPDATE Maintenance SET [Procedure ID] = ?, [Date] = ? WHERE (MainteanceID = ?)">
<DeleteParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
<asp:Parameter Name="VIN" Type="String" />
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="VIN" QueryStringField="VIN" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
<asp:Parameter Name="VIN" Type="String" />
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="MainteanceID" DataSourceID="dsMaintance" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="MainteanceID" HeaderText="MainteanceID"
InsertVisible="False" ReadOnly="True" SortExpression="MainteanceID" />
<asp:BoundField DataField="VIN" HeaderText="VIN" SortExpression="VIN" />
<asp:BoundField DataField="Procedure ID" HeaderText="Procedure ID"
SortExpression="Procedure ID" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
</Columns>
</asp:GridView>
You should pass the proper parameters to the UpdateParameters section. It looks like you have a copy-paste problem (maybe you copied the section from the InsertParameters section). Your UPDATE statement needs only 3 parameters.
<asp:SqlDataSource OnUpdated="dsMaintance_Updated" ID="dsMaintance" runat="server"
ConnectionString="<%$ ConnectionStrings:VehicalList %>"
DeleteCommand="DELETE FROM [Maintenance] WHERE [MainteanceID] = ?"
InsertCommand="INSERT INTO [Maintenance] ([MainteanceID], [VIN], [Procedure ID], [Date]) VALUES (?, ?, ?, ?)"
ProviderName="<%$ ConnectionStrings:VehicalList.ProviderName %>"
SelectCommand="SELECT MainteanceID, VIN, [Procedure ID], [Date] FROM Maintenance WHERE (VIN = ?)"
UpdateCommand="UPDATE Maintenance SET [Procedure ID] = ?, [Date] = ? WHERE (MainteanceID = ?)">
<DeleteParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="MainteanceID" Type="Int32" />
<asp:Parameter Name="VIN" Type="String" />
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="VIN" QueryStringField="VIN" Type="String" />
</SelectParameters>
<UpdateParameters>
<%--<asp:Parameter Name="MainteanceID" Type="Int32" />--%>
<%--<asp:Parameter Name="VIN" Type="String" />--%>
<asp:Parameter Name="Procedure_ID" Type="Int32" />
<asp:Parameter Name="Date" Type="DateTime" />
</UpdateParameters>
</asp:SqlDataSource>

Error when clicking edit in gridview

Error Message:
The variable name '#Bitnet' has already been declared. Variable names must be unique within a query batch or stored procedure. When trying to edit gridview.
I want to be able to update the checkbox field. This gridview is based on a table join.
<asp:Content ID="Content4" runat="server" contentplaceholderid="ContentPlaceHolder2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="INST_ID,BITNET" DataSourceID="SqlDataSource1" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="INST_ID" HeaderText="INST_ID" ReadOnly="True" SortExpression="INST_ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Prefix" HeaderText="Prefix" SortExpression="Prefix" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="Zip" HeaderText="Zip" SortExpression="Zip" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
<asp:BoundField DataField="BITNET" HeaderText="BITNET" ReadOnly="True" SortExpression="BITNET" />
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:CheckBox ID="Active" runat="server" Checked='<%# Eval("Active").ToString() == "1" ? true:false %>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="Active" runat="server" Checked="true" Enabled="false" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = #Bitnet)">
</asp:SqlDataSource>
You have to declare the parameter first. Then only you can use that. It should be something like this
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASPLOGINConnectionString %>" SelectCommand="SELECT IALO.INST_ID, IALO.FirstName, IALO.LastName, IALO.Prefix, IALO.Title, IALO.Address1, IALO.City, IALO.State, IALO.Zip, IALO.Country, IALO.Phone, IALO.Fax, IALO.BITNET, IALO.Active FROM CEOTable INNER JOIN IALO ON CEOTable.Inst_ID = IALO.INST_ID WHERE (CEOTable.Bitnet = #Bitnet)">
<SelectParameters>
<asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Bitnet" Type="Int32" DefaultValue="0" />
</UpdateParameters>
</asp:SqlDataSource>
I solved my own question issue was that I was not clearing my parameters first
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Clear();
}
That fixed the issue it works fine now.

Categories

Resources