Insert values into DataSource.DataView programmatically??

T

TenDot

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
 
T

TenDot

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

TenDot

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 said:
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 said:
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
 
T

TenDot

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

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top