MySql gridview delete command - c#

I have this GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete"
onclientclick="javascript:return confirm('are you sure you want to delete.');"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id"/>
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:BoundField DataField="first" HeaderText="first" SortExpression="first" />
<asp:BoundField DataField="last" HeaderText="last" SortExpression="last" />
<asp:BoundField DataField="gender" HeaderText="gender"
SortExpression="gender" />
<asp:BoundField DataField="birthday" HeaderText="birthday"
SortExpression="birthday" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
And this is the SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:clientConnectionString %>"
ProviderName="<%$ ConnectionStrings:clientConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM Client;"
DeleteCommand="DELETE FROM Client WHERE id = #id AND email = #email;">
<DeleteParameters>
<asp:Parameter Name="id" Type="String" />
<asp:Parameter Name="email" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
And when i try to excute a delete i get this error:
Parameter '#id' must be defined.
Exception Details: MySql.Data.MySqlClient.MySqlException: Parameter '#id' must be defined.

You may want to add the DataKeyNames attribute to your GridView
You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete. See this link
DataKeyNames="id,email"
In the context of your Gridview.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" DataKeyNames="id,email">

Related

pass querystring value of linkbnbutton placed in gridview to iframe source

I need to pass the querystring value, of linkbutton to iframe source here is the Code
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
Width="100%" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True" DataKeyNames="ItemCode">
<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" />
<Columns>
<asp:BoundField DataField="State" HeaderText="State"/>
<asp:BoundField DataField="City" HeaderText="City"/>
<asp:BoundField DataField="LandlineNo1" HeaderText="Contact"/>
<asp:BoundField DataField="Item_name" HeaderText="Name"/>
<asp:BoundField DataField="Qty" HeaderText="qty"/>
<asp:BoundField DataField="rate" HeaderText="Amount"/>
<asp:BoundField DataField="DeliveryDate" HeaderText="Deliverydate"/>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDetails" runat="server" Text="Release" PostBackUrl='<%# "~/popup.aspx?RowIndex=" + Container.DataItemIndex %>'></asp:LinkButton>
<cc1:modalpopupextender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="lnkDetails"
CancelControlID="Button2" BackgroundCssClass="Background">
</cc1:modalpopupextender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">
<iframe style=" width: 600px; height: 400px;" id="irm1" src="popup.aspx?id=<% =request.Querystring(" id=")%>" runat="server"></iframe>
<br/>
<asp:Button ID="Button2" runat="server" Text="Close" />
</asp:Panel>
how to pass the value of querystring to popup.aspx page for now its
going null

Is there a way to automatically execute the 'Select' command on a GridView, when the Grid loads?

I've got a GridView who's DataSource SELECTs based on the SelectedValue of another GridView.
On the GridView with the SelectedValue (DataKeyName), I need to execute the Select command when the grid loads, rather that using a "Select" button.
I've tried using the SelectedIndex attribute of the GridView but this wont do it for me.
I've also looked at various methods and events but I cant seem to find a good way of doing this.
In the code below, there is a ButtonField with a "Select" button but I would like the Select command to take place as or after the grid is populated.
Thanks in advance.
<asp:GridView ID="ClientDetailsGridView" runat="Server" AutoGenerateColumns="False" DataKeyNames="AccountNumber" DataSourceID="ZeNetAccounts" CellPadding="2" ForeColor="#333333" GridLines="None" Width="749px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Select" HeaderText="More Information" Text="View" />
<asp:BoundField DataField="AccountNumber" HeaderText="Account #" SortExpression="AccountNumber" />
<asp:BoundField DataField="AccountName" HeaderText="Client Name" SortExpression="AccountName" />
<asp:BoundField DataField="Address1" HeaderText="Address" SortExpression="Address1" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="PostalCode" HeaderText="Zip" SortExpression="PostalCode" />
<asp:BoundField DataField="PrimaryPhoneNumberFormatted" HeaderText="Phone" SortExpression="PrimaryPhoneNumberFormatted" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="White" Height="25px" CssClass="GridHeaderBasic" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SelectedRowStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedAscendingCellStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedAscendingHeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedDescendingCellStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
<SortedDescendingHeaderStyle BackColor="Transparent" Font-Bold="True" ForeColor="#333333" Height="25px" CssClass="GridDataRows" />
</asp:GridView>
<br />
<br />
<asp:GridView ID="CustomFieldsGridView" runat="server" AutoGenerateColumns="False" SelectedIndex="0" DataKeyNames="DocumentNumber,DocumentType,CustomFieldDefinitionKeyID" DataSourceID="ZeNetCustomFields">
<Columns>
<asp:BoundField DataField="DocumentNumber" HeaderText="DocumentNumber" ReadOnly="True" SortExpression="DocumentNumber" />
<asp:BoundField DataField="DocumentType" HeaderText="DocumentType" ReadOnly="True" SortExpression="DocumentType" />
<asp:BoundField DataField="CustomFieldDefinitionKeyID" HeaderText="CustomFieldDefinitionKeyID" ReadOnly="True" SortExpression="CustomFieldDefinitionKeyID" />
<asp:BoundField DataField="CustomFieldValue" HeaderText="CustomFieldValue" SortExpression="CustomFieldValue" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="ZeNetAccounts" runat="Server" ConnectionString="<%$ ConnectionStrings:Ze-Net_Technologies %>"
SelectCommand="SELECT * FROM [tblAccounts] WHERE ([AccountName] = #AccountName)">
<SelectParameters>
<asp:ControlParameter ControlID="ClientSearch" Name="AccountName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ZeNetCustomFields" runat="Server" ConnectionString="<%$ ConnectionStrings:Ze-Net_Technologies %>"
SelectCommand="SELECT * FROM [tblCustomFieldValues] WHERE ([DocumentNumber] = #DocumentNumber)">
<SelectParameters>
<asp:ControlParameter ControlID="ClientDetailsGridView" Name="DocumentNumber" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

Required field and regular expression validators for the bound field elements inside the grid view

Here is my gridview and I have added a command field Edit which will enable to edit the particular values in the grid view.The code is working fine.i just need to know how to add a required field validator and regular expression validator for the bound field.
As far as I know adding a textbox inside item template and then validating using ControlToValidate =”id” is possible,but is there any chances of adding required field validator for Bound Field
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" RowStyle-HorizontalAlign="Center"
style="top: 538px; left: 216px; position: absolute; height: 133px; width: 534px"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
Visible="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Sr.No.">
<ItemTemplate>
<asp:Label ID="lblserial" runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FID" HeaderText="Faculty ID" ReadOnly="true"
ItemStyle-HorizontalAlign="Center" SortExpression="FID" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString ="{0:d}" SortExpression="Date" ReadOnly="true"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Theme" HeaderText="Theme" SortExpression="Theme"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Duration" HeaderText="Duration" ItemStyle-HorizontalAlign="Center"
SortExpression="Duration" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Organizer" HeaderText="Role" ItemStyle-HorizontalAlign="Center"
SortExpression="Organizer" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Venue" HeaderText="Venue" SortExpression="Venue" ReadOnly="true"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="UpdateDate" HeaderText="UpdateDate" DataFormatString ="{0:d}" ReadOnly="true"
SortExpression="UpdateDate" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate><h3 align="center" style="font-weight: bold; font-size: large">No Records Found</h3></EmptyDataTemplate>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT * FROM [WorkshopSC_det] WHERE ([FID] = #FID)"
UpdateCommand="UPDATE [WorkshopSC_det] SET Duration=#Duration,Organizer=#Organizer WHERE(Theme=#Theme) ">
<SelectParameters>
<asp:SessionParameter Name="FID" SessionField="FID" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Theme"/>
<asp:Parameter Name="Duration" />
<asp:Parameter Name="Organizer" />
<asp:Parameter Name="Role" />
</UpdateParameters>
</asp:SqlDataSource>
I had the same issue with Bound fields and as per my understanding bound fields are not supported for field validators.
I tried two solutions.
I followed this article and it worked.But in my case I wanted to add regex and required field validators and this approach is not consistent when there are more than one validator.
But you can try it.
Replace bound fields with Template fields and add item templates and field validator inside the template field as below. It is for the faculty ID field and you can repeat the same for other columns.
The other advantage of Template fields is it allows you to add footer and header rows to the column which might be handy in case of row insertion to the grid.
<asp:TemplateField HeaderText="Faculty ID" SortExpression="FID" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="fidText" runat="server" Text='<%# Bind("FID") %>' ReadOnly="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqValidatorId" runat="server" ControlToValidate="fidText" Display="Dynamic" ForeColor="Red" ErrorMessage="Value is required."> * </asp:RequiredFieldValidator>
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ControlToValidate="fidText" ErrorMessage="Enter a valid value" ForeColor="Red" ValidationExpression="YOUR REGEX EXPRESSION">Enter a valid value for Faculty Id.</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>

GridView with Hyperlink column

I have this GridView in my page:
<asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource2"
AllowPaging="True" PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None">
<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>
And this is the SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT country AS Country,count(country) As Count FROM client GROUP by country ORDER BY count(country) DESC;" ></asp:SqlDataSource>
And i want that the name will be hyper link to somethis like this:
/Details.aspx?country=countryname
How i can implement it?
Use a <TemplateField> with a HyperLink control in it, like this:
<asp:GridView ...>
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<%# "Details.aspx?country=" + Eval("Country")%>'
Text='Details'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HyperLink runat="server" ID="HyperLink1"
NavigateUrl='<%# "/Details.aspx?country=" + Eval("Country")%>'
Text='Details'>
</asp:HyperLink>

updating a sql table with a datagridview using template field

so the thing is i want to limit what can the user insert when updating through a datagridview. i thought of converting that specific field into a template and make it a dropdownlist with limited options to update such field. the thing is that when i run tests with this it updates the field as null. the sqldatasource bound to the datagridview updates perfectly fine without the template field.
i was able to make this work on a table without much problem but when i tried the same thing for another table it kept giving me a null value on the dropdownlist. this latter is bound to a SqlDataSource. i checked the code on the gridview and the data source and its pretty much the same. i don't see whats the problem.
This is the code of my SqlDataSource
<asp:SqlDataSource ID="SqlCoord" runat="server"
ConnectionString="<%$ ConnectionStrings:sistema_gestion_calidadConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT profesor.nombre + ' ' + profesor.apellidos AS Expr1, materia.nombre_materia, coordinadores.id_coordinador, coordinadores.id_materia, coordinadores.id_profesor FROM coordinadores INNER JOIN materia ON coordinadores.id_materia = materia.id_materia INNER JOIN profesor ON coordinadores.id_profesor = profesor.id_profesor"
UpdateCommand="UPDATE coordinadores SET id_materia = #id_materia, id_profesor = #id_profesor FROM coordinadores INNER JOIN profesor ON coordinadores.id_profesor = profesor.id_profesor INNER JOIN materia ON coordinadores.id_materia = materia.id_materia WHERE (coordinadores.id_coordinador = #original_id_coordinador)">
<UpdateParameters>
<asp:Parameter Name="id_materia" />
<asp:Parameter Name="id_profesor" />
<asp:Parameter Name="original_id_coordinador" />
</UpdateParameters>
</asp:SqlDataSource>
And this is my DataGridview
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id_coordinador"
DataSourceID="SqlCoord" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="Profesor" SortExpression="Profesor">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlProfs"
DataTextField="Expr1" DataValueField="id_profesor">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlProfs" runat="server"
ConnectionString="<%$ ConnectionStrings:sistema_gestion_calidadConnectionString %>"
SelectCommand="SELECT id_profesor, nombre + ' ' + apellidos AS Expr1 FROM profesor">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Expr1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="nombre_materia" SortExpression="nombre_materia">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlMats"
DataTextField="nombre_materia" DataValueField="id_materia">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlMats" runat="server"
ConnectionString="<%$ ConnectionStrings:sistema_gestion_calidadConnectionString %>"
SelectCommand="SELECT [id_materia], [nombre_materia] FROM [materia]">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("nombre_materia") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id_coordinador" HeaderText="id_coordinador" ReadOnly="True"
SortExpression="id_coordinador" InsertVisible="False" Visible="False" />
<asp:BoundField DataField="id_materia" HeaderText="id_materia"
SortExpression="id_materia" Visible="False" />
<asp:BoundField DataField="id_profesor" HeaderText="id_profesor"
SortExpression="id_profesor" Visible="False" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>

Categories

Resources