How to use WebMethods that Update/ Add record from sql server with FormView or DetailsView Control

I

Inna

Hi,

I'm trying to create a web site that has a grid view that shows a list of
registered students. when I select a student on GridView it shows me a
DetailsView (Or FormView is ok either) with more student details. I have to
use WebMethods for my records manipulation(like updating or adding data)

So I created 2 WebMethods:
----------------------------------------------------------------------------------------------------------------------------------------
[WebMethod]
public void UpdateStudent(int stuID, string fn, string ln, string adr,
string phone)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE Students SET FirstName = " + fn + ",
LastName=" + ln + "Address = " + adr + ", Phone = " + phone + "WHERE
StudentID = " + stuID;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

[WebMethod]
public Boolean AddStudent(string fn, string ln, string adr, string ph)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into Students VALUES('" + fn + "', '"
+ ln + "', '" + adr + "', '" + ph "')";
cmd.Connection = con;

con.Open();
cmd.ExecuteNonQuery();
con.Close();

}
-----------------------------------------------------------------------------------------------------------------------------
Then in my aspx web page I configured ObjectDataSource to use these methods
(I also have select method that retreives the data properly) for update and
insert tabs. What else I suppose to define to make FormView control to
update and insert data properly.
How I define parameters to get data from updated fields and take these
parameters to WebMethod. Maybe I should change something in my WebMethods?
I must to Select, Update, Insert, Delete and Search my Student using
WebMethods.
I tried the following, but it doesnt work:
----------------------------------------------------------------------------------------------------------------------------------------
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
DeleteMethod="DeleteStudent" InsertMethod="AddStudent"
SelectMethod="GetStudent" TypeName="ws.SQL_Operations"
UpdateMethod="UpdateStudent">
<DeleteParameters>
<asp:parameter Name="StuID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="StudentID" Type="Int32" />
<asp:parameter Name="FirstName" Type="String" />
<asp:parameter Name="LastName" Type="String" />
<asp:parameter Name="Address" Type="String" />
<asp:parameter Name="Phone" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="gvStudents" Name="stuID"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:parameter Name="fn" Type="String" />
<asp:parameter Name="ln" Type="String" />
<asp:parameter Name="adr" Type="String" />
<asp:parameter Name="ph" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>


<asp:FormView ID="fvStudent" runat="server" DataKeyNames="StudentID"
DataSourceID="ObjectDataSource2" >
<ItemTemplate>
<table style="font-size: medium;">
<tr><td>Student ID</td>
<td style="background: gray; font-weight: bold;"><%# Eval("StudentID")
%><br></td></tr>
<tr><td>First Name</td>
<td><%# Eval("FirstName") %><br></td></tr>
<tr><td>Last Name</td>
<td><%# Eval("LastName")%><br></td></tr>
<tr><td>Phone</td>
<td><%# Eval("Phone") %><br></td></tr>
<tr><td>Address</td>
<td><%# Eval("Address") %><br></td></tr>
<tr><td colspan="2">
<asp:Button ID="btnEdit" CommandName="Edit" Text="Edit" runat="server" />
<asp:Button ID="btnDelete" CommandName="Delete" Text="Delete"
runat="server"/>
<asp:Button ID="btnAdd" CommandName="New" Text="New"
runat="server"/></td></tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr><td>Student ID</td>
<td><%# Eval("StudentID") %><br></td></tr>
<tr><td>First Name</td>
<td><asp:TextBox Runat="server" ID = "txtFName" Text='<%# Bind("FirstName")
%>' /></td>
</tr>
<tr><td>Last Name</td>
<td><asp:TextBox Runat="server" ID="txtLName" Text='<%# Bind("LastName") %>'
/></td>
</tr>
<tr><td>Address</td>
<td><asp:TextBox Runat="server" ID="txtAddress" Text='<%# Bind("Address")
%>' /></td></tr>
<tr><td>Phone</td>
<td><asp:TextBox Runat="server" ID = "txtPhone" Text='<%# Bind("Phone") %>'
/></td></tr>
<tr><td colspan="2">
<asp:Button ID="btnUpdate" CommandName="Update" Text="Update Student"
runat="server"/>
<asp:Button ID="btnCancel" CommandName="Cancel" Text="Cancel"
runat="server"/></td></tr></table>
</EditItemTemplate>
<InsertItemTemplate>
<table>
<tr><td><b>First Name</b></td>
<td><asp:TextBox Runat="server" ID="txtFirstName2" Text='<%#
Bind("FirstName") %>' /></td></tr>
<tr><td><b>Last Name</b></td>
<td><asp:TextBox Runat="server" ID="txtLastName2" Text='<%# Bind("LastName")
%>' /></td></tr>
<tr><td><b>Address</b></td>
<td><asp:TextBox Runat="server" ID="txtAddress2" Text='<%# Bind("Address")
%>' /></td></tr>
<tr><td><b>Phone</b></td>
<td><asp:TextBox Runat="server" ID="txtPhone2" Text='<%# Bind("Phone") %>'
/></td></tr>

<tr><td colspan="2">
<asp:Button ID="btnInsert" CommandName="Insert" Text="Save" runat="server"/>
<asp:Button ID="btnCancel2" CommandName="Cancel" Text="Cancel"
runat="server"/></td></tr>
</table>
</InsertItemTemplate>
</asp:FormView>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top