Inserting Record Problem

Discussion in 'ASP .Net' started by thebison, Mar 13, 2006.

  1. thebison

    thebison Guest

    Hi all,

    I hope someone can help with this relatively simple problem.
    I am building a timesheet application using ASP.NET C# with Visual
    Studio 2003.As it is only a protoype application, my database has been
    made in MSDE.

    I have a 'Create Resource' page where new employees are added to the
    database. On my data-entry form I capture all the standard details for
    the Resource table. I have two drop down boxes which are data bound and
    populated with department IDs and manager IDs from other tables
    (datasets). I have a stored procedure for inserting a new resource, and
    when I click the 'Submit' button everything seems to work. However when
    I look at the values inserted to the Resource table, everything is
    correct apart from the two drop-down values which have defaulted to
    '1', even when I select '3', or '4' or any other option in the
    drop-down list. Whatever I do, when the record is written, it defaults
    to the value of '1'.

    The code for writing these two elements in the insert is:

    sqlParam = cmd.Parameters.Add("@ManagerID", SqlDbType.Int, 4);

    sqlParam.Value = ddManager.SelectedValue;


    sqlParam = cmd.Parameters.Add("@DeptID", SqlDbType.Int, 4);

    sqlParam.Value = ddDept.SelectedValue;

    The weird thing is that when I use the same code on another form for a
    drop-down which has 'hard-coded' values in the drop down list this
    works fine.

    Any help would be much appreciated! I'm sure it's something simple!

    Thanks

    Al
     
    thebison, Mar 13, 2006
    #1
    1. Advertising

  2. Please try pasting your aspx code i mean your
    Dropdownlists etc..
    Patrick

    "thebison" <> wrote in message
    news:...
    > Hi all,
    >
    > I hope someone can help with this relatively simple problem.
    > I am building a timesheet application using ASP.NET C# with Visual
    > Studio 2003.As it is only a protoype application, my database has been
    > made in MSDE.
    >
    > I have a 'Create Resource' page where new employees are added to the
    > database. On my data-entry form I capture all the standard details for
    > the Resource table. I have two drop down boxes which are data bound and
    > populated with department IDs and manager IDs from other tables
    > (datasets). I have a stored procedure for inserting a new resource, and
    > when I click the 'Submit' button everything seems to work. However when
    > I look at the values inserted to the Resource table, everything is
    > correct apart from the two drop-down values which have defaulted to
    > '1', even when I select '3', or '4' or any other option in the
    > drop-down list. Whatever I do, when the record is written, it defaults
    > to the value of '1'.
    >
    > The code for writing these two elements in the insert is:
    >
    > sqlParam = cmd.Parameters.Add("@ManagerID", SqlDbType.Int, 4);
    >
    > sqlParam.Value = ddManager.SelectedValue;
    >
    >
    > sqlParam = cmd.Parameters.Add("@DeptID", SqlDbType.Int, 4);
    >
    > sqlParam.Value = ddDept.SelectedValue;
    >
    > The weird thing is that when I use the same code on another form for a
    > drop-down which has 'hard-coded' values in the drop down list this
    > works fine.
    >
    > Any help would be much appreciated! I'm sure it's something simple!
    >
    > Thanks
    >
    > Al
    >
     
    Patrick.O.Ige, Mar 14, 2006
    #2
    1. Advertising

  3. thebison

    thebison Guest

    Hi
    Thanks for your quick reply. Here is the aspx code for the two DropDown
    lists. If I was to hard code the values into the dropdowns, the Insert
    statement would insert whichever value I selected, however when I do it
    as below, it inserts the value '1' (the first option) whichever value I
    select in the drop down. Very strange.

    <asp:dropdownlist id=ddManager style="Z-INDEX: 133; LEFT: 136px;
    POSITION: absolute; TOP: 288px" tabIndex=7 runat="server" Width="172px"
    DataValueField="ManagerID" DataSource="<%# dsManagerName1 %>"
    ></asp:dropdownlist>



    <asp:dropdownlist id=ddDept style="Z-INDEX: 135; LEFT: 136px; POSITION:
    absolute; TOP: 336px" tabIndex=8 runat="server" Width="172px"
    DataValueField="DeptID" DataSource="<%# dsDeptName1
    %>"></asp:dropdownlist>


    Hope you can help, thanks again!

    Al
     
    thebison, Mar 14, 2006
    #3
  4. thebison

    Jim Andersen Guest

    "thebison" <> skrev i en meddelelse
    news:...
    > the two drop-down values which have defaulted to
    > '1', even when I select '3', or '4' or any other option in the
    > drop-down list. Whatever I do, when the record is written, it defaults
    > to the value of '1'.


    You sure you dont have a re-bind of those DDboxes somewhere ? Like in the
    pageload event ? Someplace you call Databind ?

    /jim
     
    Jim Andersen, Mar 14, 2006
    #4
  5. thebison

    thebison Guest

    Jim,

    Thanks for your reply. You are right! It was because (being a careless
    newbie) i had forgotten the

    if(!IsPostBack)

    in my page_load..

    It now works!

    Thanks!
     
    thebison, Mar 14, 2006
    #5
    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. Justin Musick

    Inserting a Record Into SQL

    Justin Musick, Oct 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    391
    Justin Musick
    Oct 31, 2003
  2. Curt Emich

    Inserting A New Record

    Curt Emich, May 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    337
    Curt Emich
    May 6, 2004
  3. Shapper
    Replies:
    2
    Views:
    586
    Paul Clement
    Jul 7, 2005
  4. David
    Replies:
    3
    Views:
    124
    David Gordon
    Aug 4, 2004
  5. Navnath Gadakh

    problem in inserting record in ms access.

    Navnath Gadakh, Oct 4, 2012, in forum: Java
    Replies:
    5
    Views:
    408
    Arne Vajhøj
    Oct 6, 2012
Loading...

Share This Page