hello all..,
i have problem in update sqldatasource,
my code like that:
me.sqldatasource.updateparameter(index).defaultvalue=value
me.sqldatasource.update()
it can not update data, why?
but if i use insert or delete like:
me.sqldatasource.insertparameter(index).defaultvalue=value
me.sqldatasource.insert()
me.sqldatasource.deleteparameter(index).defaultvalue=value
me.sqldatasource.delete()
it can work for insert and delete data...
can anyone give me update command code in sqldatasouce?
plsss...
thx...
Here's your example:
ASPX
<%@. Page Language="VB" AutoEventWireup="false" CodeFile="1161930.aspx.vb" Inherits="MyPosts_DataAccess_1161930" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"><asp:label id="lblStatus" runat="server" /><br /><br />ShipperID:<asp:literal id="litShipperID" runat="server"></asp:literal><br />CompanyName:<asp:textbox id="txtCompanyName" runat="server" /><br />Phone:<asp:textbox id="txtPhone" runat="server" /><br /><br /><asp:button id="btnUpdate" runat="server" text="Update" /><br /><asp:sqldatasource id="sdsShippers" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString%>"selectcommand="SELECT * FROM [Shippers] WHERE [ShipperID] = @.ShipperID" updatecommand="UPDATE Shippers SET CompanyName = @.CompanyName, Phone = @.Phone WHERE (ShipperID = @.ShipperID)"><selectparameters><asp:querystringparameter defaultvalue="1" name="ShipperID" querystringfield="ShipperID" /></selectparameters><updateparameters><asp:parameter name="CompanyName" /><asp:parameter name="Phone" /><asp:querystringparameter defaultvalue="1" name="ShipperID" querystringfield="ShipperID" /></updateparameters></asp:sqldatasource> </form></body></html>
CODE-BEHIND
Imports System.DataImports System.Web.UIImports System.Web.UI.WebControlsPartialClass MyPosts_DataAccess_1161930Inherits System.Web.UI.PagePrivate Sub SetData()Dim drvAs DataRowView =CType(sdsShippers.Select(DataSourceSelectArguments.Empty), DataView)(0)litShipperID.Text = drv("ShipperID").ToStringtxtCompanyName.Text = drv("CompanyName").ToStringtxtPhone.Text = drv("Phone").ToStringEnd SubProtected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.LoadIf Not Me.IsPostBackThenMe.SetData()End IfEnd SubProtected Sub btnUpdate_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btnUpdate.ClicksdsShippers.Update()Me.SetData()End SubProtected Sub sdsShippers_Updating(ByVal senderAs Object,ByVal eAs SqlDataSourceCommandEventArgs)Handles sdsShippers.Updatinge.Command.Parameters("@.CompanyName").Value = txtCompanyName.Texte.Command.Parameters("@.Phone").Value = txtPhone.TextEnd SubProtected Sub sdsShippers_Updated(ByVal senderAs Object,ByVal eAs SqlDataSourceStatusEventArgs)Handles sdsShippers.UpdatedIf e.ExceptionIs Nothing ThenlblStatus.Text ="Record successfully updated!"End IfEnd SubEnd Class|||
ok, thx ec...
your methods is right...
it can update..
i have try it, u use update command
<asp:querystringparameterdefaultvalue="1" name="ShipperID" querystringfield="ShipperID" />
so the update in sqldatasource can work....
u make defaultvalue="1", it mean only for update data where shipperid=1..
but i want to update every data that i choose.. i try changedefaultvalue={0}.but it can not work..
how can i do?
thx..|||
Modify the Parameter within the SqlDataSource.Selecting event as you do for the SqlDataSource.Updating event.
Protected Sub sdsShippers_Selecting(ByVal senderAs Object,ByVal eAs SqlDataSourceSelectingEventArgs)Handles sdsShippers.Selectinge.Command.Parameters("@.ShipperID").Value ="Whatever you want here"End Sub|||
helo ec...
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
Dim shipperId As String = GridView1.Rows(0).Cells(0).Text
e.Command.Parameters("@.shipper_id").Value = shipperId
End Sub
this is error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
it means error index in row or cell, but my index look right...
i display in gridview like that:
shipperid name status
1 andy sendbutton checkbox
2 budy sendbutton checkbox
my code like that:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
if e.commandname="send" then
me.sqldatasource1.update()
end if
end sub
Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Updating
Dim name As String = GridView1.Rows(0).Cells(1).Text
e.Command.Parameters("@.name").Value = name
e.Command.Parameters("@.status").Value = "true"
end sub
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
Dim shipperid As String = GridView1.Rows(0).Cells(0).Text
e.Command.Parameters("@.shipperid").Value = shipperid
End Sub
pls...
thx...
|||I'm confused, it looks like the SqlDataSource1 control is already associated to GridView1 as the DataSourceID. Is this correct?
|||i have make the new page, but it same error...
it like can not use in selecting event...
i have many try it..
pls... ec...
thx...
|||I gotta see your code before I can do anything.
|||okay, this is my full code in one page...
test3.aspx
<%@. Page Language="VB" AutoEventWireup="false" CodeFile="test3.aspx.vb" Inherits="test3" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GAMESConnectionString %>"
DeleteCommand="DELETE FROM [Shippers] WHERE [shipperId] = @.shipperId" InsertCommand="INSERT INTO [Shippers] ([shipperId], [CompanyName], [status]) VALUES (@.shipperId, @.CompanyName, @.status)"
SelectCommand="SELECT * FROM [Shippers]" UpdateCommand="UPDATE [Shippers] SET [CompanyName] = @.CompanyName, [status] = @.status WHERE [shipperId] = @.shipperId">
<DeleteParameters>
<asp:Parameter Name="shipperId" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="status" Type="String" />
<asp:querystringparameter name="shipperid" querystringfield="shipperid" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="shipperId" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="status" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="shipperId"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="shipperId" HeaderText="shipperId" ReadOnly="True" SortExpression="shipperId" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:ButtonField CommandName="send" Text="sendbutton" />
<asp:TemplateField HeaderText="status" SortExpression="status">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("status") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("status") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
test3.aspx.vb
Partial Class test3
Inherits System.Web.UI.Page
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "send" Then
Me.SqlDataSource1.Update()
End If
End Sub
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
'Dim shipper As String = Me.GridView1.Rows(0).Cells(0).Text
'e.Command.Parameters("@.shipperid").Value = shipper
End Sub
Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Updating
Dim company As String = Me.GridView1.Rows(0).Cells(1).Text
e.Command.Parameters("@.companyname").Value = company
e.Command.Parameters("@.status").Value = "true"
End Sub
End Class
thx...
|||Ok, your selecting all the records from your table in the select command so there's no need to even use the SqlDataSource.Selecting event so get rid of it. As far as your update goes, change the QueryStringParameter to Parameter so that it is automatcially populated by the DataKeys collection, and remove your SqlDataSource.Updating event as these parameters will automatically be populated by the BoundFields you have declared.
|||ok.. many thx ec..
i have put commandname=update in sendbutton... it can work..
yes... ok, i have many learn from you since i join in this forums..
the problem that u resolve for me...:
1.formview is ok
2.gridview is ok
3.store procedure is ok
4.searching many word?
the next problem is searching, but i will post new thread in other time...
ok, i want sleeping...
thx....
hello ec... my code like in the top on this post...
pls.. ec.. or i give it again..
this is my code:
<%@. Page Language="VB" AutoEventWireup="false" CodeFile="test3.aspx.vb" Inherits="test3" %
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GAMESConnectionString %>"
DeleteCommand="DELETE FROM [Shippers] WHERE [shipperId] = @.shipperId" InsertCommand="INSERT INTO [Shippers] ([shipperId], [CompanyName], [status]) VALUES (@.shipperId, @.CompanyName, @.status)"
SelectCommand="SELECT * FROM [Shippers]" UpdateCommand="UPDATE [Shippers] SET [CompanyName] = @.CompanyName, [status] = @.status WHERE [shipperId] = @.shipperId ">
<DeleteParameters>
<asp:Parameter Name="shipperId" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="status" Type="String" />
<asp:parameter name="shipperid" type="string" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="shipperId" Type="String" />
<asp:Parameter Name="CompanyName" Type="String" />
<asp:Parameter Name="status" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="shipperId"
DataSourceID="SqlDataSource1" Visible="False">
<Columns>
<asp:BoundField DataField="shipperId" HeaderText="shipperId" ReadOnly="True" SortExpression="shipperId" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:ButtonField CommandName="update" Text="sendbutton" />
<asp:TemplateField HeaderText="status" SortExpression="status">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("status") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("status") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
pls.. thx...
No comments:
Post a Comment