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

Discussion in 'ASP .Net Web Services' started by Inna, Mar 12, 2009.

  1. Inna

    Inna Guest

    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>
    ----------------------------------------------------------------------------------------------------------

    I appreciate any help or hints!

    Thanks!
     
    Inna, Mar 12, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JonBosker
    Replies:
    1
    Views:
    469
    Alexey Smirnov
    Feb 1, 2007
  2. Frits van Soldt

    How to insert a new record in detailsview when there are no record

    Frits van Soldt, Nov 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    746
    Frits van Soldt
    Nov 17, 2005
  3. Feege
    Replies:
    0
    Views:
    463
    Feege
    Dec 20, 2005
  4. Replies:
    0
    Views:
    327
  5. Jimmy Tran
    Replies:
    1
    Views:
    238
    Dan Brussee
    Oct 3, 2003
Loading...

Share This Page