Update Gridview Error - "oldValues is empty" - asp.net

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)

Related

Refresh the Gridview

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.

order gridview list decending

I currently have a table of data which is ordered in ID order, but i want the list to be ordered in descending order. This i am sure is a simple question but i just can not figure out how to do it.
Here is the ObjectDataSource etc. I am just not sure where to change the code...
Thanks
<asp:ObjectDataSource
ID="odsTours"
runat="server"
SelectMethod="GetFiltered"
TypeName="blive.Shop.Tour"
SortParameterName="sortExpression">
<SelectParameters>
<asp:Parameter
Name="isActive"
ConvertEmptyStringToNull="true"
DefaultValue="" />
<asp:Parameter
Name="isInThePast"
ConvertEmptyStringToNull="true"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource
ID="odsBookings"
runat="server"
SelectMethod="GetFiltered"
TypeName="blive.Shop.Booking"
SortParameterName="sortExpression">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlTourFilter"
PropertyName="SelectedValue"
Name="tourId"
Type="Int32"
ConvertEmptyStringToNull="true" />
<asp:Parameter
Name="username"
ConvertEmptyStringToNull="true"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
It should be done in the select method of the object data source.
In your case it should be in
GetFiltered method.
You can apply the order by in this method.

Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query

I am creating a website now using Microsoft Visual Web Developer 2010 Express and I used the toolbox of "createUserWizard". Then I put the userId for my "CustomerInfo" table to the "data type=uniqueidentifier" because I need to link it to the user name in the aspnet_ table.
Later on, I need to link my "Order" table to the "CustomerInfo" table so I put my orderId data type=uniqueidentifier. Then, I plan insert my order details to the "Order" table but I have the problem of:
"Implicit conversion from data type
sql_variant to uniqueidentifier is not
allowed. Use the CONVERT function to
run this query".
I search and find some answer like datatype of the parameter set to "Empty"or delete it. but then I have this error
" Conversion failed when converting from character string to uniqueidentifier."
This is my SQL
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Order] WHERE [orderId] = #orderId"
InsertCommand="INSERT INTO [Order] ([orderId], [userId], [Services], [PickUpDate], [PickUpTime], [SpecialDate], [SpecialTime]) VALUES (#orderId, #userId, #Services, #PickUpDate, #PickUpTime, #SpecialDate, #SpecialTime)"
SelectCommand="SELECT * FROM [Order]"
UpdateCommand="UPDATE [Order] SET [userId] = #userId, [Services] = #Services, [PickUpDate] = #PickUpDate, [PickUpTime] = #PickUpTime, [SpecialDate] = #SpecialDate, [SpecialTime] = #SpecialTime WHERE [orderId] = #orderId">
<DeleteParameters>
<asp:Parameter Name="orderId" Type="Object" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="orderId" Type="Object" />
<asp:Parameter Name="userId" Type="Object" />
<asp:Parameter Name="Services" Type="String" />
<asp:Parameter Name="PickUpDate" Type="String" />
<asp:Parameter Name="PickUpTime" Type="String" />
<asp:Parameter Name="SpecialDate" Type="String" />
<asp:Parameter Name="SpecialTime" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="userId" Type="Object" />
<asp:Parameter Name="Services" Type="String" />
<asp:Parameter Name="PickUpDate" Type="String" />
<asp:Parameter Name="PickUpTime" Type="String" />
<asp:Parameter Name="SpecialDate" Type="String" />
<asp:Parameter Name="SpecialTime" Type="String" />
<asp:Parameter Name="orderId" Type="Object" />
</UpdateParameters>
</asp:SqlDataSource>
Now that I think of it, maybe there is a problem with my code in vb.
I will put it here, please tell me how to do the correct way of inserting data to the database.
Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
SqlDataSource1.InsertParameters(0).DefaultValue = Now
SqlDataSource1.Insert()
Response.Redirect("~/Customer/AfterOrder.aspx")
End Sub
If you want to insert GUID from your code, you should make parameter type GUID
<asp:Parameter Name="ParamName" DbType="Guid" />
And you should pass GUID value as (c#):
SqlDataSource1.InsertParameters["ParamName"].DefaultValue = System.Guid.NewGuid().ToString();
If you want to pass a string value, you have to use CONVERT(UNIQUEIDENTIFIER,'YOUR VALUE') in the insert query like this:
InsertCommand="INSERT INTO [Order] ([UserId], [etc])
VALUES (CONVERT(UNIQUEIDENTIFIER,#UserId), #etc)"

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>

How to write select SQL to use like when using control parameter?

How to change to use like instead of = ?
([Card_no] = #Search)
Moreover, if i want to show all in default, this method force to user to type
something first. How to show all in default when using control parameter?
SelectCommand="SELECT [Serial_no], [Transaction_Date], [Card_no], [Company],[IsCredit], [Fee] FROM [CarPark] WHERE ([Card_no] = #Search) or ([Company] = #Search)"
UpdateCommand="UPDATE [CarPark] SET
Card_no=#Card_no,
Company=#Company,
IsCredit=#IsCredit,
Fee=#Fee
WHERE Serial_no=#Serial_no">
<UpdateParameters>
<asp:Parameter Name="Card_no" Type="String" />
<asp:Parameter Name="Company" Type="String" />
<asp:Parameter Name="IsCredit" Type="Boolean" />
<asp:Parameter Name="Fee" Type="Double" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="Search_TextBox" DefaultValue="" Name="Search"
PropertyName="Text" Type="String" />
</SelectParameters>
Try this:
Card_no like '%{0}%'
Or if you must use named parameters:
Card_no like '%'+#search +'%'

Resources