mySQL .NET Connector, GridView not updating - c#

Visual Studion 2012 Update 3 with mySQL .NET Connector 6.6.5.0
I cannot get a GridView to post back any updated data
My GridView is configured as:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Unit" />
<asp:BoundField DataField="Price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mysqlConn %>" ProviderName="<%$ ConnectionStrings:mysqlConn.ProviderName %>"
SelectCommand="SELECT ID, Name, Unit, Price FROM product"
UpdateCommand="UPDATE product SET Name = ?, Unit = ?, Price = ? WHERE ID = ?">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" Direction="Input" />
<asp:Parameter Name="Unit" Type="String" Direction="Input" />
<asp:Parameter Name="Price" Type="Single" Direction="Input" />
<asp:Parameter Name="ID" Type="Int64" Direction="Input" />
</UpdateParameters>
</asp:SqlDataSource>
The grid displays the list of products fine. However when I select Edit in the grid view and change a value and then click Update the resulting change is not posted back to the database. What am I doing wrong with my Update statement or parameters?
All pointers appreciated.

please use GridView1.DataBind();
then gridview will bind with new data.
please refer this link
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx

I was missing:
DataKeyNames="ID"
as part of the GridView definition which was preventing the UPDATE from happening.

Related

Row command event not firing when binding grid view by wizard in dev express

i have bind the grid view with the help of wizard. But, after generating the row command event I can't see it being fired.I know question is asked many time and tried many of them in my code but none of them is worked for me
here is my code-
<dx:ASPxGridView ID="ASPxGridView2" KeyFieldName="NodeID" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource" onrowcommand="ASPxGridView2_RowCommand" >
<Columns>
<dx:GridViewDataTextColumn FieldName="AlarmAlias" VisibleIndex="0" ReadOnly="True">
</dx:GridViewDataTextColumn>
<dx:GridViewDataCheckColumn FieldName="asu_SMSNight" VisibleIndex="1" ReadOnly="True">
</dx:GridViewDataCheckColumn>
<dx:GridViewDataTextColumn FieldName="NodeID" VisibleIndex="2" Caption="Action">
<DataItemTemplate>
<dx:ASPxButton ID="btnAllAlarmDelete" AutoPostBack="true" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("NodeID") %>' runat="server" >
</dx:ASPxButton>
</DataItemTemplate>
</dx:GridViewDataTextColumn >
</Columns>
<Settings ShowFilterRow="true" ShowFilterBar="Hidden" ShowFilterRowMenu="true" ShowFooter="True" ShowGroupedColumns="True" ShowGroupFooter="VisibleAlways" ShowHeaderFilterButton="True" ShowHorizontalScrollBar="False" ShowVerticalScrollBar="True" VerticalScrollableHeight="100" VerticalScrollBarStyle="Virtual" />
<SettingsText EmptyDataRow="Select a category to view alarms" />
</dx:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DTUConnectionString %>"
SelectCommand="AD_FetchAlarmByBuildingCategoryUser" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="0" Name="BuildingId" SessionField="sessionBuilding"
Type="Int32" />
<asp:SessionParameter DefaultValue="0" Name="AlarmCategoryid" SessionField="sessionCategory"
Type="Int32" />
<asp:SessionParameter DefaultValue="0" Name="UserId" SessionField="sessionUserid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
i m new to dev express. please help
It is solved don't know the exact reason but i closed my vs and open my project again and rebuild than it automatically start working

GridView doesn't appear when it selects values from a dropdownlist

I have a DropDownList that lists the users in a table of the database (.accdb) and a GridView that should list the Dates when the user logged in which are stored in another table of the same database.
I linked the dropdown with a ObjectDataSource that is linked to the users table and the gridview to an objectdatasource that is linked to the lof table and has a select query like this :
SELECT Data
FROM log
WHERE (User = ?)
I set the parameter to be what is selected in the DropDownList.
And here is my problem : the DropDownList is showing all the users correctly, but the GridView doesn't even show on the page. I tried with postback and GridView.DataBind() but none works.
I am pretty sure it's a stupid mistake i did, but i can't find it.
Generated ASPX code :
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="Utilizator"
DataValueField="ID"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataSet1TableAdapters.DataTable1TableAdapter">
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetDataBy" TypeName="DataSet1TableAdapters.logTableAdapter">
<InsertParameters>
<asp:Parameter Name="Data" Type="String" />
<asp:Parameter Name="Utilizator" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue=""
Name="Utilizator" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource2" Height="145px" Width="107px">
<Columns>
<asp:BoundField DataField="Data" HeaderText="Data" SortExpression="Data" />
</Columns>
</asp:GridView>

Sqldatasource where parameter

I have a sqldatasource, in select command i have #status parameter. The parameter take the value from the textbox at runtime.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details] WHERE ([status] = #status)"
FilterExpression="title='{4}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.
Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.
Try this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details]"
FilterExpression="title='{0}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.
Updated
I have also changed the SelectParameters to FilterParameters
Update 2
I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:AWorks %>"
SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
FilterExpression="Title='{0}'"
runat="server">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
<asp:GridView
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
<asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
<asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
<asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>
try a condition like this:
(#Status is null or #Status ='' Or Status = #Status)
set ConvertEmptyStringToNull="true" and then try....
Add CancelSelectOnNullParameter="false" to your SqlDataSource.

How to delete data from gridview which is using datasource and SP using Wizard

I am using a gridview to select, delete and update data in database. I have written a single SP for doing all these operation. Based on a parameter SP decides which operation to perform.
Here is the image of my gridview
image of my grid http://www.freeimagehosting.net/uploads/0a5de50661.jpg
<asp:GridView ID="GridView1" runat="server" DataSourceID="dsDomain"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
DataKeyNames="DomainId" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="DomainId" HeaderText="DomainId"
InsertVisible="False" ReadOnly="True" SortExpression="DomainId">
</asp:BoundField>
<asp:BoundField DataField="Domain" HeaderText="Domain"
SortExpression="Domain">
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" >
</asp:BoundField>
<asp:BoundField DataField="InsertionDate" HeaderText="InsertionDate"
SortExpression="InsertionDate">
</asp:BoundField>
</asp:GridView>
Data Source that I am using is here
<asp:SqlDataSource ID="dsDomain" runat="server"
ConnectionString="<%$ ConnectionStrings:conLogin %>"
SelectCommand="Tags.spOnlineTest_Domain"
SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"
DeleteCommand="Tags.spOnlineTest_Domain" DeleteCommandType="StoredProcedure" OnDeleting="DomainDeleting">
<SelectParameters>
<asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="DomainId" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" />
<asp:Parameter DefaultValue="1" Name="OperationType" Type="Byte" />
</SelectParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="DomainId"
PropertyName="SelectedValue" Size="4" Type="Int32" />
<asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="4" Name="OperationType" Type="Byte" />
</DeleteParameters>
</asp:SqlDataSource>
Select operation is working fine.
But when I tried to delete, it says
Procedure or Function 'spOnlineTest_Domain' expects parameter '#Domain', which was not supplied
But I am supplying this parameter, as
My Stored procedure calling is like this
EXEC Tags.spOnlineTest_Domain NULL, NULL, NULL, 1 // For Select last parameter will be 1
EXEC Tags.spOnlineTest_Domain "SelectedRow's DomainId), NULL, NULL, 4 // For Delete last parameter will be 4
My procedure has 4 parameters where last parameter will be set by programmer which will tell the program for what kind of operation to be performed.
For Select only last parameter has to be Not Null.
For Delete first and last parameter cannot be NULL.
My first Delete parameter is Primary key of the table. I am passing this value, when a user selects a row and hit delete. I am not sure by using PropertyName="SelectedValue", will I get the right value of the ID.
http://www.freeimagehosting.net/uploads/0a5de50661.jpg />
If you have not implemented OnDeleting event of the ObjectDataSource, try the below
<asp:ObjectDataSource .... OnDeleting="YourDeletingEvent" ...
</asp:ObjectDataSource>
In your code behind:
private void YourDeletingEvent(object source, ObjectDataSourceMethodEventArgs e)
{
IDictionary paramsFromPage = e.InputParameters;
//In this case I assume your stored procedure is taking a DomainId as a parameter
paramsFromPage.Remove("Domain");
paramsFromPage.Add("Domain", (int)paramsFromPage["DomainId"]);
paramsFromPage.Remove("DomainId");
}
Please look here for more details.
u can write:(between gridView tags)
\< asp:TemplateField ShowHeader="false">
\asp:LinkButton ID="linkButton1" runat="server" CausesValidation="false" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
and after u can implement the "GridView1_RowDeleting" methode,like that:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//makeDelete("YourStoredProcedure");
//mydataBindGV();
}

updating a gridview always returns null parameters

I have a simple gridview (well it is now) it populates, I can edit it. but when it comes to updating I just cant get it to work.
The following code creates a gridview which searches for users whose name starts with a "c" (Thats part of my filtering I stripped out)
The problem is when I click the update button it won't update. The Stored procedure gets called but all parameters passed to it are null. therefore the database is not updated.
I know its a simple problem with a simple solution but I just can't see it.
I tried using ExtractValuesFromCell within the onupdating event and still only got nulls. I had to add the CausesValidation="false" because without it the update events were not even called.
Any and all help is appreciated
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource3"
DataKeyNames="UserId">
<Columns>
<asp:CommandField ShowEditButton="True" CausesValidation="false" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="MobileAlias" HeaderText="MobileAlias"
SortExpression="MobileAlias" />
<asp:BoundField DataField="DistrictId" HeaderText="DistrictId"
SortExpression="DistrictId" />
<asp:BoundField ReadOnly="true" DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
</Columns>
</asp:GridView> <asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:REMConnectionString_development_dev %>"
SelectCommand="LoadUser" SelectCommandType="StoredProcedure"
UpdateCommand="UpdateUser" UpdateCommandType="StoredProcedure"
onupdating="SqlDataSource3_Updating">
<SelectParameters>
<asp:Parameter DefaultValue="c" Name="UserName" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="DistrictID" Type="Int32" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="MobileAlias" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
the only code in the codebehind is
protected void SqlDataSource3_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
}
It is there so I can drop in a breakpoint and inspect the parameters, which are all there but with null values
Something else has to be at work here. I replicated the code you provided and when I break down the event arguments I see the values and it all works correctly. Could you edit your post and include all the code?
Ok I have had a chance to do some more research and confirmed the issue.
Setting the Master Page ID in the Page_load function is the issue. It needs to be set in the Page_Init.
This page provides a good explanation
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=469
So by moving
ID= "REM"; from page_load to Page_init the problem magically went away.
Unfortunately it is common to advise setting the ID in Page_Load which is what I relied on and turned out to be bad advice.
DC

Categories

Resources