Insert values into DataSource.DataView programmatically??

Discussion in 'ASP .Net Web Controls' started by TenDot, Jul 8, 2006.

  1. TenDot

    TenDot Guest

    I'm using a wizard to collect data that will go into multiple tables. each
    Wizstep has a separate DataSource object. When the Complete wizstep is
    reached I want to update all the datasources with a common value in a column.
    That key will not be know till the user saves.

    I've tried:

    SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    DataView dv =
    (DataView)theSrce.Select(DataSourceSelectArguments.Empty);
    dv[0].BeginEdit();
    dv[0]["FK_MedRec"] = 1;//
    (int)HttpContext.Current.Session["MedRecId"];
    dv[0]["FK_Pat"] =
    1;//(int)HttpContext.Current.Session["MedRecId"];
    dv[0].EndEdit();
    theSrce.Insert();

    I get access to dv[0] but everytime I try to insert I get an error tellling
    me that dv[0]["FK_Pat"] is null (which is a constraint).

    Am I doing something wrong?

    TIA
    --
    TenDot
    TenDot, Jul 8, 2006
    #1
    1. Advertising

  2. TenDot

    TenDot Guest

    OK, I got it...

    here's the code:

    SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    theSrce.InsertParameters["FK_Pat"].DefaultValue = "1";
    theSrce.InsertParameters["FK_MedRec"].DefaultValue =
    medRecId.ToString();
    theSrce.Insert();

    it's' not in the DataView, you make the changes to the InsertParameters.
    It's kind of wierd since you can't access the parameter value directly. My
    sense is that DefaultValue only performs an assignment if it's null. I guess
    that's ok.


    --
    TenDot


    "TenDot" wrote:

    > I'm using a wizard to collect data that will go into multiple tables. each
    > Wizstep has a separate DataSource object. When the Complete wizstep is
    > reached I want to update all the datasources with a common value in a column.
    > That key will not be know till the user saves.
    >
    > I've tried:
    >
    > SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    > DataView dv =
    > (DataView)theSrce.Select(DataSourceSelectArguments.Empty);
    > dv[0].BeginEdit();
    > dv[0]["FK_MedRec"] = 1;//
    > (int)HttpContext.Current.Session["MedRecId"];
    > dv[0]["FK_Pat"] =
    > 1;//(int)HttpContext.Current.Session["MedRecId"];
    > dv[0].EndEdit();
    > theSrce.Insert();
    >
    > I get access to dv[0] but everytime I try to insert I get an error tellling
    > me that dv[0]["FK_Pat"] is null (which is a constraint).
    >
    > Am I doing something wrong?
    >
    > TIA
    > --
    > TenDot
    TenDot, Jul 8, 2006
    #2
    1. Advertising

  3. TenDot

    TenDot Guest

    I spoke too soon...

    Yeah I got the keys in there but somehow, the entered data is NOT there when
    the record is written to the table??

    Seems like I have to find the command to bind the data in the page controls
    to the DataSource. Can I do that if I'm handling the DataSource controls in
    a App_Code class??

    Any thoughts??

    Thanks
    --
    TenDot


    "TenDot" wrote:

    > OK, I got it...
    >
    > here's the code:
    >
    > SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    > theSrce.InsertParameters["FK_Pat"].DefaultValue = "1";
    > theSrce.InsertParameters["FK_MedRec"].DefaultValue =
    > medRecId.ToString();
    > theSrce.Insert();
    >
    > it's' not in the DataView, you make the changes to the InsertParameters.
    > It's kind of wierd since you can't access the parameter value directly. My
    > sense is that DefaultValue only performs an assignment if it's null. I guess
    > that's ok.
    >
    >
    > --
    > TenDot
    >
    >
    > "TenDot" wrote:
    >
    > > I'm using a wizard to collect data that will go into multiple tables. each
    > > Wizstep has a separate DataSource object. When the Complete wizstep is
    > > reached I want to update all the datasources with a common value in a column.
    > > That key will not be know till the user saves.
    > >
    > > I've tried:
    > >
    > > SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    > > DataView dv =
    > > (DataView)theSrce.Select(DataSourceSelectArguments.Empty);
    > > dv[0].BeginEdit();
    > > dv[0]["FK_MedRec"] = 1;//
    > > (int)HttpContext.Current.Session["MedRecId"];
    > > dv[0]["FK_Pat"] =
    > > 1;//(int)HttpContext.Current.Session["MedRecId"];
    > > dv[0].EndEdit();
    > > theSrce.Insert();
    > >
    > > I get access to dv[0] but everytime I try to insert I get an error tellling
    > > me that dv[0]["FK_Pat"] is null (which is a constraint).
    > >
    > > Am I doing something wrong?
    > >
    > > TIA
    > > --
    > > TenDot
    TenDot, Jul 9, 2006
    #3
  4. TenDot

    TenDot Guest

    Hi,

    I'm still hacking at this problem. I've approached it from several angles
    and still get get this done.

    To recap:

    I'm using a wizard to provide navigation through multiple data entry pages.
    Each page is a separate table in a sql database. Each entry screen is a
    FormView that's built as an ascx, which is dropped into the wizardstep.

    When the Complete page is reached, I've put a button to do the update. The
    update button calls code in the App_Code folder. The update method takes the
    Page as a parameter and recursively searches the wiz pages for sqlDataSources
    with a certain prefix and creates a list which it returns to the caller. The
    caller then passes the list of sqlDatasources to an update method which loops
    through the SqlDataSources. For each SqlDataSource 2 additional fields are
    populated into the DataSource's Parameters and then performs the Insert() on
    the data source.

    What's actually happening is that the Data tables (all 10 of them) ARE being
    inserted BUT only the last 2 fields that I've insertd values for, are getting
    written to the table, all the user inserted data is being left behind and
    show up null in the database. I know that this is happening because one of
    the values that I write into the table is an integer that's incremented each
    time I run a test.

    I've taken the approach that there needs to be a DataBind() executed. I
    tried that on the SqlDataSource, on the parent ascx and on the FormView each
    in different iterations of the code. Nothing seems to work. I've also put
    an OnInserting call back and can see the data in the Ascx textbox fields as
    the user entered it while the Insert is in progress.

    It seems like the goal is to update the InsertParameters but it seems kind
    of dorky to update those manually (though I'm considering it at this point
    :-( ).

    Here's an example of the insert code.



    while( sSrces.MoveNext() )
    {
    SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    foreach (Control ctrl in theSrce.Parent.Controls)
    {
    if (ctrl.GetType() == typeof(FormView))
    {
    ctrl.DataBind();
    }
    }

    theSrce.InsertParameters["FK_Pat"].DefaultValue =
    PK_Pat.ToString();
    theSrce.InsertParameters["FK_MedRec"].DefaultValue =
    medRecId.ToString();

    // I've also tried theSrce.DataBind();

    theSrce.Insert();
    }

    I've tried everything I can think of. Any ideas?

    Thanks
    --
    TenDot


    "TenDot" wrote:

    > I'm using a wizard to collect data that will go into multiple tables. each
    > Wizstep has a separate DataSource object. When the Complete wizstep is
    > reached I want to update all the datasources with a common value in a column.
    > That key will not be know till the user saves.
    >
    > I've tried:
    >
    > SqlDataSource theSrce = (SqlDataSource)sSrces.Current;
    > DataView dv =
    > (DataView)theSrce.Select(DataSourceSelectArguments.Empty);
    > dv[0].BeginEdit();
    > dv[0]["FK_MedRec"] = 1;//
    > (int)HttpContext.Current.Session["MedRecId"];
    > dv[0]["FK_Pat"] =
    > 1;//(int)HttpContext.Current.Session["MedRecId"];
    > dv[0].EndEdit();
    > theSrce.Insert();
    >
    > I get access to dv[0] but everytime I try to insert I get an error tellling
    > me that dv[0]["FK_Pat"] is null (which is a constraint).
    >
    > Am I doing something wrong?
    >
    > TIA
    > --
    > TenDot
    TenDot, Jul 11, 2006
    #4
    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. A.S.V. Rosales

    Cached Dataview insert into MS-SQL2000

    A.S.V. Rosales, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,058
    A.S.V. Rosales
    Jun 26, 2003
  2. Islamegy®

    DataView vs. Datasource requery

    Islamegy®, Mar 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    594
    S. Justin Gengo [MCP]
    Mar 1, 2006
  3. ElmoWatson

    DataView from a DataSource control

    ElmoWatson, Feb 9, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    272
    ElmoWatson
    Feb 9, 2007
  4. CodeMonkey
    Replies:
    1
    Views:
    714
    joyal jhaveri
    Feb 4, 2011
  5. fynali iladijas
    Replies:
    2
    Views:
    289
    samwyse
    Jul 28, 2007
Loading...

Share This Page