Refresh the Gridview - asp.net

I am updating the table in database by entering some fields in same page containing INPUT fields and Grid. but grid which is connected to that table is not refreshing after submit the value. I have to open that page in new tab to view the changes. Also i want those input fields empty after submit.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:con %>"
ProviderName="<%$ ConnectionStrings:con.ProviderName %>"
SelectCommand="SELECT * FROM [View_Intakesheet]"
InsertCommand="INSERT INTO [View_Intakesheet] [date_withdrawn], [date_delivered], [qc_by], [qc_date], [additional_notes], [Case_Status_Text], [Case_Type_Text]) VALUES (#date_withdrawn, #date_delivered, #qc_by, #qc_date, #additional_notes, #Case_Status_Text, #Case_Type_Text)"
UpdateCommand="UPDATE intakesheet SET date_withdrawn= #date_withdrawn, date_delivered= #date_delivered, qc_by = #qc_by, qc_date=#qc_date, additional_notes = #additional_notes WHERE (case_number = #case_number)">
<InsertParameters>
<asp:Parameter Name="date_withdrawn" />
<asp:Parameter Name="date_delivered" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" />
<asp:Parameter Name="additional_notes" />
<asp:Parameter Name="Case_Status_Text" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="assigned_to" SessionField="Name" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="case_status" />
<asp:Parameter Name="date_withdrawn" />
<asp:Parameter Name="date_delivered" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" />
<asp:Parameter Name="additional_notes" />
<asp:Parameter Name="case_number" />
<asp:Parameter Name="case_type" />
</UpdateParameters>
</asp:SqlDataSource>

You can do it by rebinding the gridview like this just after where your update is done.
GridViewIs.DataBind()

You will have to re-bind the grid once you are done with changes. Please let us know if you are doing that.

Related

detailed information for each entry in gridview

So i have made a Web Form with a dropdownlist which contains different classes in school and if you select one you get all the students of the selected class in the GridView below. So what i want to make now is, that if i click on a student it should open a more detailed page about the student, but i have no clues how to do this. I get all my data of a .mdf database file.
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="schueler.aspx.cs" Inherits="ASPXUebung.schueler" %>
<asp:DropDownList ID="selectklassen" runat="server" DataSourceID="SqlDataSource1klassenselect" DataTextField="K_Bez" DataValueField="K_ID" AutoPostBack="true"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1klassenselect" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [K_Bez], [K_ID] FROM [klassen] ORDER BY [K_Bez]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="S_SCHNR" DataSourceID="SqlDataSource1schueler">
<Columns>
<asp:BoundField DataField="S_SCHNR" HeaderText="S_SCHNR" ReadOnly="True" SortExpression="S_SCHNR" />
<asp:BoundField DataField="S_Name" HeaderText="S_Name" SortExpression="S_Name" />
<asp:BoundField DataField="S_Vorname" HeaderText="S_Vorname" SortExpression="S_Vorname" />
<asp:BoundField DataField="S_Gebdat" HeaderText="S_Gebdat" SortExpression="S_Gebdat" />
<asp:BoundField DataField="S_Adresse" HeaderText="S_Adresse" SortExpression="S_Adresse" />
<asp:BoundField DataField="S_K_Klasse" HeaderText="S_K_Klasse" SortExpression="S_K_Klasse" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1schueler" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [schueler] WHERE [S_SCHNR] = #original_S_SCHNR AND (([S_Name] = #original_S_Name) OR ([S_Name] IS NULL AND #original_S_Name IS NULL)) AND (([S_Vorname] = #original_S_Vorname) OR ([S_Vorname] IS NULL AND #original_S_Vorname IS NULL)) AND (([S_Gebdat] = #original_S_Gebdat) OR ([S_Gebdat] IS NULL AND #original_S_Gebdat IS NULL)) AND (([S_Adresse] = #original_S_Adresse) OR ([S_Adresse] IS NULL AND #original_S_Adresse IS NULL)) AND (([S_K_Klasse] = #original_S_K_Klasse) OR ([S_K_Klasse] IS NULL AND #original_S_K_Klasse IS NULL))" InsertCommand="INSERT INTO [schueler] ([S_SCHNR], [S_Name], [S_Vorname], [S_Gebdat], [S_Adresse], [S_K_Klasse]) VALUES (#S_SCHNR, #S_Name, #S_Vorname, #S_Gebdat, #S_Adresse, #S_K_Klasse)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [schueler] WHERE ([S_K_Klasse] = #S_K_Klasse)" UpdateCommand="UPDATE [schueler] SET [S_Name] = #S_Name, [S_Vorname] = #S_Vorname, [S_Gebdat] = #S_Gebdat, [S_Adresse] = #S_Adresse, [S_K_Klasse] = #S_K_Klasse WHERE [S_SCHNR] = #original_S_SCHNR AND (([S_Name] = #original_S_Name) OR ([S_Name] IS NULL AND #original_S_Name IS NULL)) AND (([S_Vorname] = #original_S_Vorname) OR ([S_Vorname] IS NULL AND #original_S_Vorname IS NULL)) AND (([S_Gebdat] = #original_S_Gebdat) OR ([S_Gebdat] IS NULL AND #original_S_Gebdat IS NULL)) AND (([S_Adresse] = #original_S_Adresse) OR ([S_Adresse] IS NULL AND #original_S_Adresse IS NULL)) AND (([S_K_Klasse] = #original_S_K_Klasse) OR ([S_K_Klasse] IS NULL AND #original_S_K_Klasse IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_S_SCHNR" Type="Int16" />
<asp:Parameter Name="original_S_Name" Type="String" />
<asp:Parameter Name="original_S_Vorname" Type="String" />
<asp:Parameter Name="original_S_Gebdat" Type="DateTime" />
<asp:Parameter Name="original_S_Adresse" Type="String" />
<asp:Parameter Name="original_S_K_Klasse" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="S_SCHNR" Type="Int16" />
<asp:Parameter Name="S_Name" Type="String" />
<asp:Parameter Name="S_Vorname" Type="String" />
<asp:Parameter Name="S_Gebdat" Type="DateTime" />
<asp:Parameter Name="S_Adresse" Type="String" />
<asp:Parameter Name="S_K_Klasse" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="selectklassen" Name="S_K_Klasse" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="S_Name" Type="String" />
<asp:Parameter Name="S_Vorname" Type="String" />
<asp:Parameter Name="S_Gebdat" Type="DateTime" />
<asp:Parameter Name="S_Adresse" Type="String" />
<asp:Parameter Name="S_K_Klasse" Type="String" />
<asp:Parameter Name="original_S_SCHNR" Type="Int16" />
<asp:Parameter Name="original_S_Name" Type="String" />
<asp:Parameter Name="original_S_Vorname" Type="String" />
<asp:Parameter Name="original_S_Gebdat" Type="DateTime" />
<asp:Parameter Name="original_S_Adresse" Type="String" />
<asp:Parameter Name="original_S_K_Klasse" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Make the student name column a Hyperlink column like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="S_SCHNR" DataSourceID="SqlDataSource1schueler">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="S_SCHNR"
DataNavigateUrlFormatString="StudentInfo.aspx?id={0}
DataTextField="S_Name" "/>
<asp:BoundField DataField="S_SCHNR" HeaderText="S_SCHNR" ReadOnly="True" SortExpression="S_SCHNR" />
<asp:BoundField DataField="S_Name" HeaderText="S_Name" SortExpression="S_Name" />
<asp:BoundField DataField="S_Vorname" HeaderText="S_Vorname" SortExpression="S_Vorname" />
<asp:BoundField DataField="S_Gebdat" HeaderText="S_Gebdat" SortExpression="S_Gebdat" />
<asp:BoundField DataField="S_Adresse" HeaderText="S_Adresse" SortExpression="S_Adresse" />
<asp:BoundField DataField="S_K_Klasse" HeaderText="S_K_Klasse" SortExpression="S_K_Klasse" />
</Columns>
</asp:GridView>
Make another web page called StudentInfo.aspx. On that page, display the data using a DetailsView control, a SqlDataSource control and the QueryString parameter. Here's an example of how to do that: http://msdn.microsoft.com/en-us/library/xt50s8kz(v=vs.100).aspx
A more convenient way to do this without creating a new page would be to populate the data into a Bootstrap modal like this. It uses UpdatePanel, which may or may not be something you want to use though.
Add the Detail Event to the Gridview.
Create a div for the Bootstrap modal with a FormView
Call the jQuery from the C# code behind to populate the data in the FormView with the ID from the GridView.

Update Gridview Error - "oldValues is empty"

So I'm trying to update my GridView, and I get this error:
You have specified that your update command compares all values on
SqlDataSource 'SqlDataSource1', but the dictionary passed in for
oldValues is empty. Pass in a valid dictionary for update or change
your mode to OverwriteChanges.
Here is the SQL Data Source in question:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT field1, field2, field3, field4, field5, field6
FROM table
ORDER BY field4"
UpdateCommand="UPDATE table2
SET field2 = #field2 , field3 = #field3, field4 = #field4
WHERE (field1 = #original_field1) AND (field6 = #original_field6)"
FilterExpression="field1 LIKE '{0}'">
<FilterParameters>
<asp:ControlParameter ControlID="SearchBox" Name="field1" Type="String" DefaultValue="" />
</FilterParameters>
<SelectParameters>
<asp:ControlParameter ControlID="SearchBox" Name="field1" Type="String" DefaultValue="" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="field2" Type="String" />
<asp:Parameter Name="field3" Type="String" />
<asp:Parameter Name="field4" Type="Int32" />
<asp:Parameter Name="original_field1" Type="String" />
<asp:Parameter Name="original_field5" Type="Int32" />
<asp:Parameter Name="original_field2" Type="String" />
<asp:Parameter Name="original_field3" Type="String" />
<asp:Parameter Name="original_field4" Type="Int32" />
<asp:Parameter Name="original_field6" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Now I understand one way to get around this is to change 'CompareAllValues' to 'OverwriteChanges', however I have never needed to do this before (I have other GridViews on the page that update just fine) and it never seems to update the data when I DO try anyway.
Is it the nature of the data that would cause this Data Source to act differently to the rest? Or have I done something stupid?
Thanks! :)
I discovered the answer! The data field 'field6' was not included in my Gridview as a field.
Once I added it as a column in the GridView I was able to make it work :)
(Can also add it as a DataKeyName)

No value given for one or more required parameters. showing error

<asp:AccessDataSource ID="productsubtype" Runat="server" DataFile="~/access_db/db_tables.mdb"
SelectCommand="SELECT [type], [id],[subtype] FROM [prod_subtype]"
UpdateCommand="UPDATE [prod_subtype] SET [subtype]=#subtype WHERE [id]=#id"
DeleteCommand="DELETE FROM [prod_subtype] WHERE [id]=#id"
FilterExpression="type='{0}'">
<UpdateParameters>
<asp:Parameter Name="subtype" Type="string" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="string" />
</DeleteParameters>
<FilterParameters>
<asp:ControlParameter Name="type" DefaultValue="-1"
Type="String" ControlID="gdproducttype"
PropertyName="SelectedValue"></asp:ControlParameter>
</FilterParameters>
</asp:AccessDataSource>
When i press delete button in gridview i am getting error
Try changing your UpdateParameters to the following:
<UpdateParameters>
<asp:Parameter Name="id" Type="string" />
<asp:Parameter Name="subtype" Type="string" />
</UpdateParameters>

Visual Web Developer 2008 - ASP - Formview and Inserts

Im using:
Visual Web Developer 2009
Access 2007
I am trying to insert into a products table, however i keep recieveing the error:
"Data type mismatch in criteria expression."
The Edit and Delete sections work fine..
Edit Products
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="product_id" DataSourceID="AccessDataSource1" DefaultMode="Insert"
Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="product_id" HeaderText="product_id"
InsertVisible="False" ReadOnly="True" SortExpression="product_id" />
<asp:BoundField DataField="product_name" HeaderText="product_name"
SortExpression="product_name" />
<asp:BoundField DataField="product_desc" HeaderText="product_desc"
SortExpression="product_desc" />
<asp:BoundField DataField="product_cost" HeaderText="product_cost"
SortExpression="product_cost" />
<asp:BoundField DataField="product_amount" HeaderText="product_amount"
SortExpression="product_amount" />
<asp:BoundField DataField="category_id" HeaderText="category_id"
SortExpression="category_id" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Fandangles.mdb"
DeleteCommand="DELETE FROM [product] WHERE [product_id] = ?"
InsertCommand="INSERT INTO [product] ([product_name], [product_desc], [product_cost], [product_amount], [category_id]) VALUES (?, ?, ?, ?, ?)"
SelectCommand="SELECT * FROM [product]"
UpdateCommand="UPDATE [product] SET [product_name] = ?, [product_desc] = ?, [product_cost] = ?, [product_amount] = ?, [category_id] = ? WHERE [product_id] = ?">
<DeleteParameters>
<asp:Parameter Name="product_id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="product_name" Type="String" />
<asp:Parameter Name="product_desc" Type="String" />
<asp:Parameter Name="product_cost" Type="Decimal" />
<asp:Parameter Name="product_amount" Type="Int32" />
<asp:Parameter Name="category_id" Type="Int32" />
<asp:Parameter Name="product_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="product_id" Type="Int32" />
<asp:Parameter Name="product_name" Type="String" />
<asp:Parameter Name="product_desc" Type="String" />
<asp:Parameter Name="product_cost" Type="Decimal" />
<asp:Parameter Name="product_amount" Type="Int32" />
<asp:Parameter Name="category_id" Type="Int32" />
</InsertParameters>
</asp:AccessDataSource>
</p>
</asp:Content>
DB: Access DB
product_id - Autonumber
product_name - Text
product_desc - Text
product_cost - Currency
product_amount - Number
category_id - Number - This is a lookup field
Any ideas?
Do you really need to pass your primary key "product_id" into the insert? If its an identity / PK its not needed.
Its not a param in your insert SQL yet it is a param in the datasoruces insert params.
Try this
<InsertParameters>
<asp:Parameter Name="product_id" Type="Int32" />
<asp:Parameter Name="product_name" Type="String" />
<asp:Parameter Name="product_desc" Type="String" />
<asp:Parameter Name="product_cost" Type="Decimal" />
<asp:Parameter Name="product_amount" Type="Int32" />
<asp:Parameter Name="category_id" Type="Int32" />
</InsertParameters>

Add aspnet_users UserName value into my own table

I would like to insert UserName from aspnet_users into my own table called Members. My sqldatasource looks like as follows. At the end in the InsertParameters I have added ProfileParameter but its not working. Just to get the values going, I have hardcoded 'testhere' value in the insertcommand. My SQLDatasource looks as follows.
Please help !!!
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
OnInserted= "on_inserted"
OnUpdated = "on_updated"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
DeleteCommand="DELETE FROM [member] WHERE [id] = #id"
InsertCommand="INSERT INTO [member] ([firstname], [lastname],[username]) VALUES (#firstname, #lastname,'testhere');
set #id = ##identity"
SelectCommand="SELECT * FROM [member] WHERE ([id] = #id)"
UpdateCommand="UPDATE [member] SET [firstname] = #firstname, [lastname] = #lastname WHERE [id] = #id"
onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type=Int32 Direction=Output />
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:ProfileParameter Name="username" Type=String />
</InsertParameters>
</asp:SqlDataSource>
Okay.. as a simple one:
Try changing the InsertParameter (username) to be an asp:Parameter.
Add a asp:HiddenField control with ID of username
onpageload, set the value of the username hiddenfield to be Page.User.Identity.Name
Magic!

Resources