I have a ASP.NET code, I use it to insert and view my data from a *.mdb file.
When I reload the browser reload, it insert the same old data.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="user_id" DataSourceID="AccessDataSource1" Height="50px"
Width="125px" DefaultMode="Insert">
<Fields>
<asp:BoundField DataField="user_id" HeaderText="user_id" InsertVisible="False"
ReadOnly="True" SortExpression="user_id" />
<asp:BoundField DataField="user_name" HeaderText="user_name"
SortExpression="user_name" />
<asp:BoundField DataField="user_pass" HeaderText="user_pass"
SortExpression="user_pass" />
<asp:BoundField DataField="user_email" HeaderText="user_email"
SortExpression="user_email" />
<asp:CommandField ButtonType="Button" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<div ID="Div1" runat="server"></div>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/esn.mdb"
SelectCommand="SELECT * FROM [user]"
InsertCommand="INSERT INTO [user] ([user_name], [user_pass], [user_email]) VALUES (#user_name, #user_pass, #user_email)">
<InsertParameters>
<asp:Parameter Name="user_name" Type="String" />
<asp:Parameter Name="user_pass" Type="String" />
<asp:Parameter Name="user_email" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="user_id"
DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="user_id" HeaderText="user_id" InsertVisible="False"
ReadOnly="True" SortExpression="user_id" />
<asp:BoundField DataField="user_name" HeaderText="user_name"
SortExpression="user_name" />
<asp:BoundField DataField="user_pass" HeaderText="user_pass"
SortExpression="user_pass" />
<asp:BoundField DataField="user_email" HeaderText="user_email"
SortExpression="user_email" />
</Columns>
</asp:GridView>
</form>
I think sendig user to a success insert page is an option!
Try look here:
detect duplicate insert
or here:
ASP.Net - Prevent Duplicate(Double) Inserts when Page is refreshed
How to Prevent Duplicate Record Insertion on Refresh Click in Browser in ASP.Net ?
Here a Microsoft article:
Trap the Browser Refresh
use viewstate or session to store a flag "hasSaved". when the page loads for the first time :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ViewState["hasSaved"] = false;
}
and change the value of "hasSaved" to true when the the data is saved note that save the value if "hasSaved" is false
See the full code here
Avoid Repeated insertion due to page refresh
Related
I use griview propertied while editing or deleting records without coding
When I click on edit button my image is also converted into a TextBox control,
I want to take
Fileupload control instead of textBox control.
For it which property shoule i set?
my code is as follow
<asp:GridView ID="GridView1" runat="server" Width="197px"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource2" AllowPaging="True">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:ImageField DataImageUrlField="Image" HeaderText="PHOTO">
<ControlStyle Height="100px" Width="100px" />
</asp:ImageField>
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="status" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
And here is the code of SqlDataSource2:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringnewdb %>"
DeleteCommand="delete from Product where ProductID=#ProductID"
SelectCommand="SELECT * FROM [Product]"
UpdateCommand="update Product set ProductName=#ProductName,Status=#Status where ProductID=#ProductID ">
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="ProductID"
PropertyName="SelectedValue" />
</DeleteParameters>
<UpdateParameters>
<asp:ControlParameter ControlID="GridView1" Name="ProductName"
PropertyName="SelectedValue" />
<asp:Parameter Name="ProductID" />
</UpdateParameters>
</asp:SqlDataSource>
Use the EditItemTemplate and add the FileUpload control:
... other columns ...
</EditItemTemplate>
<ItemTemplate>
<asp:FileUpload id="FileUpload1" runat="server">
</asp:FileUpload>
</ItemTemplate>
</asp:TemplateField>
... other columns ...
Here's a tutorial: http://msdn.microsoft.com/en-us/library/ms972948.aspx
I'm trying to use a Gridview to show a datatable from an Object data source. It's giving me the error:
ObjectDataSource 'odsStores' could not find a non-generic method 'ProcessDelete' that has parameters: ProcessID.
I've read a lot of other answers to this question about matching case, matching format, variables but I think I've done all of those correctly. Here's the aspx page:
<asp:GridView ID="gridStores" runat="server" AllowSorting="False" AutoGenerateColumns="False"
CssClass="grid-main" DataSourceID="odsStores" EnableViewState="False" OnDataBound="gridStores_DataBound"
OnRowDataBound="gridStores_RowDataBound">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Image ID="imgModel" runat="server" AlternateText="Click to See Details" CssClass="img-details"
EnableViewState="False" ImageUrl="~/img/detail.gif" />
</ItemTemplate>
<ItemStyle CssClass="grid-main-detail" />
</asp:TemplateField>
<asp:BoundField DataField="ProcessID" HeaderText="ProcessID" />
<asp:BoundField DataField="ProcessName" HeaderText="Process Name" ReadOnly="False" />
<asp:BoundField DataField="ProcessDescription" HeaderText="Process Description" ReadOnly="False" />
<asp:BoundField DataField="UpdateUserID" HeaderText="Last Updated By" ReadOnly="True" />
<asp:BoundField DataField="UpdateTimestamp" HeaderText="Last Updated" ReadOnly="True" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
Here's the code behind, all I have is a break point and it never hits it.
<DataObjectMethod(DataObjectMethodType.Delete)> _
Private Sub ProcessDelete(ByVal ProcessID As String)
Dim x As Integer = 0
x = x + 1
End Sub
Here's the object datasource:
<asp:ObjectDataSource ID="odsStores" runat="server" EnableViewState="False" OldValuesParameterFormatString="original_{0}"
SelectCountMethod="GetRowCount" SelectMethod="GetData" TypeName="DataWarehouseUserInterface.ProcessBSL"
UpdateMethod="ProcessUpdate" DeleteMethod="ProcessDelete" >
<UpdateParameters>
<asp:FormParameter Name="ProcessName" Type="String" FormField="ProcessName" />
<asp:FormParameter Name="ProcessDescription" Type="String" FormField="ProcessDescription" />
</UpdateParameters>
<DeleteParameters>
<asp:FormParameter Name="ProcessID" Type="String"/>
</DeleteParameters>
</asp:ObjectDataSource>
My Object data source was looking in my ProcessBSL for a method called ProcessDelete with the corresponding signature. I had written my methods in the code behind ProcessBSL file.
This was the line of code that effected it:
TypeName="DataWarehouseUserInterface.ProcessBSL"
Summary:
If it's throwing this error for you, ensure your method signature is correct. Ensure that the BSL layer is correct. Make sure you have _ - that also stopped mine from working. Hope this helps someone else.
Cheers.
Try making the method public to see if that works.
I want to add a Record Delete Message when the Delete button is selected. The message would appear for 5 seconds, then disappear. Here's the code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" DataKeyNames="ScheduleId">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="SDate" HeaderText="SDate" SortExpression="SDate" />
<asp:BoundField DataField="STime" HeaderText="STime" SortExpression="STime" />
<asp:BoundField DataField="SNoPlaces" HeaderText="SNoPlaces"
SortExpression="SNoPlaces" />
<asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId"
InsertVisible="False" ReadOnly="True" SortExpression="ScheduleId" Visible="false" />
<asp:BoundField DataField="STourId" HeaderText="STourId" SortExpression="STourId" Visible="false" />
<asp:BoundField DataField="TName" HeaderText="Tour Name" SortExpression="TName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>"
DeleteCommand="DELETE FROM Schedule WHERE (ScheduleId = #ScheduleId)"
SelectCommand="SELECT SDate, STime, SNoPlaces, ScheduleId, STourId, TName FROM Schedule INNER JOIN Tour ON Schedule.STourId = Tour.TourId">
<DeleteParameters>
<asp:Parameter Name="ScheduleId" />
</DeleteParameters>
</asp:SqlDataSource>
Regards
Tea
Use this gridview event:
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
LabelMsg.Text = "Record Deleted";
}
I would use jQuery to always Fadeout the LabelMsg element on $(document).ready(). So no matter what you put in it, from the server side, it will fade out in the time you decide.
I don't think you can accomplish this naively as part of a GridView but you can wire up the RowDeleted event to inject some JavaScript using the ClientScriptManager.RegisterStartupScript method.
I would suggest using toastr to display your actual message and it can be added to your project with its associated nuget package.
RowDeleted
RegisterStartupScript
toastr
I have the "Delete" option showing on my GridView that is populated via SQL Data Source. I am trying to implement a way where the user will be asked to verify that they wish to delete the row of information. I have been following the guide from http://msdn.microsoft.com/en-us/library/ms972940.aspx.
The issue I'm currently having is when I add my ObjectDataSource, I select the class and when I go to choose the desired method it is not listed (Fig. 37).
Addition information - I originally created the GridView to be populated via SQLDataSource, now I'm trying to transition over to ObjectDataSource and add my DeleteMethod. I am stuck at this part and do not yet have the popup window that will ask the user to continue. I will work on that after I overcome the current challenge.
Here is my aspx code:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContentAdmin" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContentAdmin" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="CourseSection_ID" DataSourceID="SqlDataSource1"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="CourseSection_ID" HeaderText="CourseSection_ID"
InsertVisible="False" ReadOnly="True" SortExpression="CourseSection_ID" />
<asp:BoundField DataField="section" HeaderText="section"
SortExpression="section" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MGT598DBConnectionString1 %>"
SelectCommand="SELECT * FROM [Course_Section]"
DeleteCommand="DELETE FROM [Course_Section] WHERE [CourseSection_ID] = #CourseSection_ID"
InsertCommand="INSERT INTO [Course_Section] ([section]) VALUES (#section)"
UpdateCommand="UPDATE [Course_Section] SET [section] = #section WHERE [CourseSection_ID] = #CourseSection_ID">
<DeleteParameters>
<asp:Parameter Name="CourseSection_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="section" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="section" Type="String" />
<asp:Parameter Name="CourseSection_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
</asp:ObjectDataSource> </asp:Content>
My code behind file:
namespace MGT598GraduateProject.View
{
public partial class ViewCourse_Section : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//http://msdn.microsoft.com/en-us/library/ms972940.aspx
}
public static void DeleteMethod(int CourseSection_ID)
{
// deletes a specified Order Details record
// from the Northwind Products table
string sql = "DELETE FROM [Order Details] WHERE OrderID = " + "#OrderID";
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MGT598DBConnectionString1"].ConnectionString))
{
SqlCommand myCommand = new SqlCommand(sql, myConnection);
myCommand.Parameters.Add(new SqlParameter("#CourseSection_ID", CourseSection_ID));
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
}
There are two issues here.
First, you have a few fundamental basics wrong.
You do not need an SQLDataSource AND an ObjectDataSource.
As you have pointed the Gridview to the SqldataSource (DataSourceID="SQLDataSource1") the object data source is infact useless.
You can delete the following code from the aspx:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
<asp:ObjectDataSource> </asp:Content>
And delete the entire DeleteMethod from the code behind.
If you read the tutorial you linked us to you will see that they are two seperate sections and not to be done together.
Second, you need to have the user 'verify' the deletion.
To do this, modify your block to match the following:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Runat="server"
OnClientClick="return confirm('Are you sure you want to delete this record?');"
CommandName="Delete">Delete Item
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CourseSection_ID"
HeaderText="CourseSection_ID"
InsertVisible="False"
ReadOnly="True"
SortExpression="CourseSection_ID" />
<asp:BoundField DataField="section"
HeaderText="section"
SortExpression="section" />
</Columns>
Sorry for the convoluted title, but that is the best way I could think to phrase it.
Basically, I have a column of data that is a URL (lets call it URL-A), and another that is hidden which is also a URL(URL-B). I have a function called ConvertURL(string URLin) in the codebehind that will take URL-A and turn it into URL-B.
So in this gridview, if someone were to edit an entry using the native gridview/datasource functionality and change URL-A, how do I get it to run this new URL-A through ConvertURL() and update the URL-B field?
Here is a simple representation of the code (not the actual code):
Code-behind function:
public string ConvertURL(string URLin)
{
Int32 iLocation = URLin.IndexOf("WWW.XYZ.COM");
if (iLocation >= 0)
{
string checkurl = URLin.Replace("WWW.XYZ.COM", "STAGING.XYZ.COM");
return checkurl;
}
else
{
return URLin;
}
}
Data-Source:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:My_AppConnectionString %>"
SelectCommand="SELECT [URL_A], [URL_B], [URLName] FROM [My_App] WHERE ([IsActive] = #IsActive)"
OldValuesParameterFormatString="original_{0}"
UpdateCommand="UPDATE [My_App] SET [URL_A] = #URL_A, [URL_B] = #URL_B, [URLName] = #URLName, [IsActive] = #IsActive WHERE [ID] = #original_ID">
<SelectParameters>
<asp:Parameter DefaultValue="True" Name="IsActive" Type="Boolean" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="URL_A" Type="String" />
<asp:Parameter Name="URL_B" Type="String" />
<asp:Parameter Name="URLName" Type="String" />
<asp:Parameter Name="IsActive" Type="boolean" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_URL_A" Type="String" />
<asp:Parameter Name="original_URL_B" Type="String" />
<asp:Parameter Name="original_URLName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
And Lastly the GridView:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
onrowediting="GridView1_RowEditing"
AutoGenerateColumns="False" CellPadding="3" DataKeyNames="ID"
DataSourceID="SqlDataSource1" EnableModelValidation="False" ForeColor="Black"
GridLines="Vertical" AllowSorting="True" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
AutoGenerateEditButton="True" CausesValidation="false">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="URL_A" HeaderText="URL A"
SortExpression="URL_A" />
<asp:BoundField DataField="URL_B" HeaderText="URL B"
SortExpression="URL_B" **Visible="False"** />
<asp:BoundField DataField="URLName" HeaderText="URL Name"
SortExpression="URLName" />
<asp:CheckBoxField DataField="IsActive" HeaderText="Active"
SortExpression="IsActive" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
So to summarize: A user will see a gridview with 3 fields to edit, one of which is URL_A (URL_B is hidden). When they hit edit and update the URL_A field I want to run URL_A through the ConvertURL() function and use that output as the data for URL_B when the update is performed.
I appreciate the help in advance!
Use the RowUpdating event of the grid view and change the new value for URL_B. In that event you will have access to the old values of the row as well as the new values. You can change the new values before they are sent to the data source for the actual update.
Add the event handler to the grid view in the ASPX:
<asp:GridView ID="GridView1" runat="server"
...
OnRowUpdating="GridView1_RowUpdating">
And in the code file handle the event:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Find the current value for URL_A
string urlA = (string)e.NewValues["URL_A"];
// Get the corresponding value for URL_B from the method
string urlB = ConvertURL(urlA);
// Set the new value for the URL_B column before it's sent to the SqlDataSource
e.NewValues["URL_B"] = urlB;
}