ASP.Net and Data Adapters

R

Robin

When adding a SQL Data Adapter to a page, is possible to configure it use a
connection string in code?
As I have found it regenerates the code each time and of the adapters are
modified and does not allow the generate dataset option to be used.
 
G

Guest

Unfortunately, you will have to keep reconnecting the Adapter if you use the
built in Drag and Drop stuff in the IDE. You can edit the connection string,
but the code is regenerated each time you muck with it. I tend to shy away
from drag-and-drop for this reason.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
R

Robin

How much coding is required? Are there any resource explaining how to create
data adapters (plus other required data objects) just in code?
 
J

James Thomas

It's not terribly difficult - here's a sample of how I (roughly) do
this. I actually have a class that handles all of my DB stuff for me,
but here's an approximation.

string conn = "<APPROPRIATE CONNECTION STRING>";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "<STORED PROC NAME>";
cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = new SqlConnection(conn);

try
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.SelectCommand.Connection = cmd.Connection;

DataSet _dset = new DataSet();

// Automatically opens DB, gets the information, then closes it
sda.Fill(_dset);
}

// Send an error if something went wrong.
catch (Exception ex)
{
emailErrorMsg(ex.Message);
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top