Updating gridview with SqlDataSource in asp.net

▼魔方 西西 提交于 2019-12-01 10:57:20

Found the solution: The Select Columns and the Update Parameters should match in order to update using SqlDataSource, that means if you select(query or procedure) is returning 3 fields in gridview, then all of them should be the parameter for the update, you can miss out the actual updations in database if not required, but the <UpdateParameters> should have all the fields: say for example if below is my select query

SELECT USERNAME, MOBILENUMBER, EMAIL FROM USERS

then the update parameters should be

<UpdateParameters>
   <asp:Parameter Name="UserName" Type="String" />
   <asp:Parameter Name="MobileNumber" Type="Int64" />
   <asp:Parameter Name="Email" Type="String" />
<UpdateParameters>

you cannot skip any of the parameters, even though you do not intend to update that field Hope this will help others, as i wasted lots of time researching on this.

I dont think your solution is OK, since I have to use the atribute OldValuesParameterFormatString="original_{0}" that means that I would have doble parameters, the ones with the original values and the ones with edited values. So there is no way to match the order.

I have 4 parameters, Im getting the right values for 2 of them, an null at the others.

I tried your solution and did not work. Thanks anyway.

Read this: The order of the parameters in the UpdateParameters collection might be important, depending on the ADO.NET provider. The System.Data.OleDb and System.Data.Odbc providers associate the parameters in the collection according to the order that the parameters appear in the parameterized SQL query. The System.Data.SqlClient provider, which is the default ADO.NET provider for the SqlDataSource control, associates the parameters in the collection by matching the name of the parameter with a placeholder alias in the SQL query. For more information about parameterized SQL queries and commands, see Using Parameters with the SqlDataSource Control.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updateparameters(v=vs.110).aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!