One page - edit & insert

D

David Thielen

Hi;

I want to have a page where Datasource.aspx means create a new datasource
and Datasource.aspx?ID=5 means edit the datasource who's PK is 5.

I am not using a GridView or DetailsView for this data - there are a bunch
of dependencies so I have to just put in a bunch of form fields.

1) How do I best initialize all the fields when it is first called with ID=5?

2) How do I best tell the user if ID=5 and there is no record with a PK of 5?

3) If they come to the page once with ID=5, then type in the url
Datasource.aspx (no ID) - how do I handle this to turn it into a create? The
form appears to still be populated with the old values.
 
Y

Yuan Ren[MSFT]

Hi Dave,

Thanks for posting!

For the current issue, if you want to create a new data source which can
receive parameters, I think you can add the SqlDataSource object
programmatically in the Page_Load event and use the query string for the
SelectCommand property. The following fragment of the code is my sample:

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource mySqlDataSource = new SqlDataSource();
mySqlDataSource.ProviderName = "System.Data.SqlClient";
mySqlDataSource.ConnectionString = "Data Source=(local);Initial
Catalog=AdventureWorks;Integrated Security=True";
mySqlDataSource.SelectCommand = "select * from
[AdventureWorks].[Person].[Address]";
GridView1.DataSource = mySqlDataSource;
GridView1.DataBind();
}
"How do I best tell the user if ID=5 and there is no record with a PK of
5?"
After calling DataBind function, please check the count of the GirdView
control and you will get the result of the current data source.
"If they come to the page once with ID=5, then type in the url
Datasource.aspx (no ID) - how do I handle this to turn it into a create?
The form appears to still be populated with the old values."
My idea is also using the query string. And then, you can add your specific
logic code for the current requirement.

Hope this will be helpful!

Regards,

Yuan Ren
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights
 
D

David Thielen

Hello;

My question is more basic. I have a method to load the data into my object.
How do I then get that data into the fields?

And when the user clicks insert/update, how does it know if it is an insert
or an update?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



"Yuan Ren[MSFT]" said:
Hi Dave,

Thanks for posting!

For the current issue, if you want to create a new data source which can
receive parameters, I think you can add the SqlDataSource object
programmatically in the Page_Load event and use the query string for the
SelectCommand property. The following fragment of the code is my sample:

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource mySqlDataSource = new SqlDataSource();
mySqlDataSource.ProviderName = "System.Data.SqlClient";
mySqlDataSource.ConnectionString = "Data Source=(local);Initial
Catalog=AdventureWorks;Integrated Security=True";
mySqlDataSource.SelectCommand = "select * from
[AdventureWorks].[Person].[Address]";
GridView1.DataSource = mySqlDataSource;
GridView1.DataBind();
}
"How do I best tell the user if ID=5 and there is no record with a PK of
5?"
After calling DataBind function, please check the count of the GirdView
control and you will get the result of the current data source.
"If they come to the page once with ID=5, then type in the url
Datasource.aspx (no ID) - how do I handle this to turn it into a create?
The form appears to still be populated with the old values."
My idea is also using the query string. And then, you can add your specific
logic code for the current requirement.

Hope this will be helpful!

Regards,

Yuan Ren
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights
 
Y

Yuan Ren[MSFT]

Hi Dave,

Thanks for your reply!
"I have a method to load the data into my object."
I want to know which specific object you have used for the current issue.
From your description, you use the SqlDataSource object, if I have any
unclear, please let me know. In my opinion, the SqlDataSource is designed
for the SQL Server database functionality. It isn't like the DataSet
object. So, if you want to perform some action for the data, I recommend
you use the DataSet object in this scenario.
"And when the user clicks insert/update, how does it know if it is an
insert or an update?"
Actually, this is an internal implementation of the SqlDataSource control.
When you set the select command or the update command for the SqlDataSource
control, the SqlDataSource control will execute the select or the update
method for data binding. These methods will be implemented by the
SqlDataSourceView control. The model of the new control is like the
DataAdapter object does but actually not. This is only for convenience of
the developer. If you want to use the old way likes ASP.NET v1.1, the
functionality is also implement without problem.

Hope this will be helpful!

Regards,

Yuan Ren
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Hi;

I would like to use an ObjectDataSource which calls to a class I have
created. The basic issue here I think is I am trying to use one page for edit
& insert without using a FormView. The advantage of this is the controls are
the same instead of having to list them twice and they are directly
accessable in the code behind.

But this means there is no container type control like FormView to tie the
data to.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



"Yuan Ren[MSFT]" said:
Hi Dave,

Thanks for your reply!
"I have a method to load the data into my object."
I want to know which specific object you have used for the current issue.
From your description, you use the SqlDataSource object, if I have any
unclear, please let me know. In my opinion, the SqlDataSource is designed
for the SQL Server database functionality. It isn't like the DataSet
object. So, if you want to perform some action for the data, I recommend
you use the DataSet object in this scenario.
"And when the user clicks insert/update, how does it know if it is an
insert or an update?"
Actually, this is an internal implementation of the SqlDataSource control.
When you set the select command or the update command for the SqlDataSource
control, the SqlDataSource control will execute the select or the update
method for data binding. These methods will be implemented by the
SqlDataSourceView control. The model of the new control is like the
DataAdapter object does but actually not. This is only for convenience of
the developer. If you want to use the old way likes ASP.NET v1.1, the
functionality is also implement without problem.

Hope this will be helpful!

Regards,

Yuan Ren
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Y

Yuan Ren[MSFT]

Hi Dave,

Thanks for your reply!

As I mentioned before, if you don't want to use the new control in ASP.NET
v2.0, you can use the old way like we do in ASP.NET v1.1. So, we can update
or insert our record to the database by using the SqlDataAdapter class in
other pages which you want to build by yourself. Please tell me about your
concern to use the old way. Thanks!

Regards,

Yuan Ren
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top