1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Please Help Me, I Am to Be Fired!

Discussion in 'C#' started by webgaya, Aug 3, 2006.

  1. #1
    What I ask can be very familiar to most of you.
    So, please don't point me a URL in MSDN,
    I can't understand the stuff there. Instead please add
    the "lacking" codes and help me to complete the code.

    I need to store First Name and Last Name of users
    which come as user inputs through text boxes in
    a web form to an existing data table as a new data row.

    I used DataSets. I heard it is bulky. Is there any other
    way of achieving it? I have included the database
    connection in a method that is called for the click
    event of the submit button. Instead, Should I include
    it in the Page load event?

    I also want to know weather there is a way to give
    relative paths for database connection instead of
    an absolute one which won't be practical when
    I move the stuff to a web server.

    Below I have included the complete code even with
    HTML. The error I get is "Update requires a valid
    InsertCommand when passed DataRow collection
    with new rows"

    It occurs due to the statement,
    objCmd.Update(ds, "contacts")

    Please help me to complete the code. I am a
    trainee and this is a practice project that
    I should submit. I couldn't complete it for
    2 weeks. I made forum posts and got MSDN
    links. Please don't do that. Just tell me what code
    to where I should add. I really appreciate your
    help. Here is the code,

    <%@ Page Language="VB" Debug="true" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>

    <script runat="server">

    sub Add(obj as object, e as EventArgs)

    dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\users\database\users.mdb")

    dim objCmd as new OleDbDataAdapter("select * from contacts", objConn)
    dim ds as DataSet = new DataSet()
    objCmd.Fill(ds, "contacts")

    dim dr as DataRow = ds.Tables("contacts").NewRow()
    objCmd.Update(ds, "contacts")


    end sub



    <form runat="server">
    <table width="100%" border="0" cellspacing="0" cellpadding="10">

    <td width="40%" align="right"><strong>First Name</strong></td>
    <td width="60%">
    <asp:textbox ID="txtFirstName" runat="server" />

    <td align="right"><strong>Last Name</strong></td>
    <asp:textbox ID="txtLastName" runat="server" />

    <tr align="center">
    <td colspan="2">
    <asp:button ID="btnAdd" runat="server" OnClick="Add" Text="Add" />


    webgaya, Aug 3, 2006 IP
  2. fluid

    fluid Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Do you mean you need to update a table in a database or just the DataSet that you've created?

    I use DSN-less connections most of the time but even if you use DSN, there's no need to specify the full path to your database? For more information check: http://www.connectionstrings.com/

    If you answer my first question, i'll be able to help you more :)
    fluid, Aug 11, 2006 IP
  3. fluid

    fluid Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Yes! I was bored. So i solved it for you :p

    After this line:
    dim objCmd as new OleDbDataAdapter("select * from contacts", objConn)
    Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)

    And just before calling Update on your DataAdapter, add this line:

    Your code should work now :)

    The reason you were getting this error is because the objCmd.Update(ds, "contacts") requires an update statement which either you can manually build or have the CommandBuilder sort it for you (as in the above example).

    More information here:
    fluid, Aug 11, 2006 IP