ASP.NET SQLDataSource.SelectCommand

  • Thread starter Thread starter mark.magill
  • Start date Start date
M

mark.magill

Guys using the below code how do I obtain the results from the select
query?


//=======================================================
// Create new SQLDataSource
//=======================================================
SqlDataSource SQLDataSourceUsersDB = new SqlDataSource();
SQLDataSourceUsersDB.ConnectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

//=======================================================
// Define SQLDataSource Command Types
//=======================================================
SQLDataSourceUsersDB.SelectCommandType =
SqlDataSourceCommandType.Text;

//=======================================================
// Create Insert and Select Commands
//=======================================================
SQLDataSourceUsersDB.SelectCommand = "SELECT * FROM Users
WHERE Email = @Email";


//=======================================================
// Define parameters for Insertion and Selection
//=======================================================
SQLDataSourceUsersDB.SelectParameters.Add("Email",
txtEmail.Text.ToString());
 
Guys using the below code how do I obtain the results from the select
query?

we're missing some context here; where will your code run? in the Page_Load
handler? in any case, you'll have to bind SQLDataSourceUsersDB to an
appropriate control, like a GridView ... or perhaps one of your own devise

the "conventional" way of using a SqlDataSource in ASP.NET is something
along the lines of:

<HTML>
<BODY>
<FORM runat="server">

<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
</asp:SqlDataSource>

<asp:GridView
id="GridView1"
runat="server"
DataSourceID="SqlDataSource1">
</asp:GridView>

</FORM>
</BODY>
</HTML>

of course there is no particular need to be conventional; you can define
your DataSource however you want (and your GridView too)
GridView grid = new GridView();

the point is that a data source alone will yield nothing at all ... and you
will still need some code to supply your @email parameter: something like
this nested in the <asp:SqlDataSource> ... </asp:SqlDataSource> block:

<SelectParameters><asp:ControlParameter ControlID="textBox1" Name="@email"
/></SelectParameters>

// assuming you have a TextBox control with an email address in it; there
are other options for the parameter source

IF you really don't like using the Page Designer surface in VS, you can gin
this all up in your .CS file:

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource ds =
new
SqlDataSource(WebConfigurationManager.ConnectionStrings["yourString"].ConnectionString,
"SELECT TOP 10 * FROM yourTable");
ds.SelectCommandType = SqlDataSourceCommandType.Text;

GridView grid = new GridView(); // who wants to drag a control onto
a page surface?
DIV1.Controls.Add(grid); // and it's nice to know what's
going on (sort of, anyway)
grid.DataSource = ds;
grid.DataBind();
}

just make sure you have a container like DIV1 here in your ASPX file to host
the GridView control; I guess there has to be a way to not use the ASPX
file at all but I have not taken the time to figure out how to do the whole
thing in pure C# code ... Page p = new Page() ... ??

Liz

//=======================================================
// Create new SQLDataSource
//=======================================================
SqlDataSource SQLDataSourceUsersDB = new SqlDataSource();
SQLDataSourceUsersDB.ConnectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

//=======================================================
// Define SQLDataSource Command Types
//=======================================================
SQLDataSourceUsersDB.SelectCommandType =
SqlDataSourceCommandType.Text;

//=======================================================
// Create Insert and Select Commands
//=======================================================
SQLDataSourceUsersDB.SelectCommand = "SELECT * FROM Users
WHERE Email = @Email";


//=======================================================
// Define parameters for Insertion and Selection
//=======================================================
SQLDataSourceUsersDB.SelectParameters.Add("Email",
txtEmail.Text.ToString());
 

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

Back
Top