Monday, July 6, 2009

Using Parameters with the SqlDataSource Control

When using the SqlDataSource control, you can specify SQL queries and statements that use parameters. This helps make your data-binding scenarios more flexible by
reading and writing database information based on values

how to Specifying Parameters in Commands
When you use the SqlDataSource control, you can set the control's command properties to parameterized SQL statements or to the name of a stored procedure.
The SqlDataSource control adds the value of the ParameterPrefix property to the beginning of all parameter names. (The default prefix is "@".)

Using Parameters with the SqlClient Provider:
By default, the SqlDataSource control uses the System.Data.SqlClient data provider to work with SQL Server as the data source. The System.Data.SqlClient provider supports named parameters as placeholders, as shown in the following example:

SELECT * FROM Stud WHERE Rollno = @Rno
AND Name = @Name

The following example shows how to use parameters in an SQL command for a SqlDataSource control that uses the System.Data.SqlClient provider

[C# code]

<asp:sqlDataSource ID="StudInfo"
SelectCommand="SELECT Marks,Rollno,ID FROM Stud WHERE Rollno = @Rno"
ConnectionString="<%$ ConnectionStrings:Stud %>" RunAt="server">
<asp:Parameter Name="Rno" Type="Int32" DefaultValue="0" />

//Suppose for insert query
<asp:Parameter Name="abc" Type="String" />
<asp:Parameter Name="xyz" Type="String" />

// for update
<asp:Parameter Name="abc" Type="String" />

//you can also use Seesion Parameter

DefaultValue="1" />

// also pass via querystring

<asp:QueryStringParameter Name="Rollno" QueryStringField="rno" Type="int" />


like this u can access Parameters with the SqlDataSource Control
also refer This Article

No comments:

Post a Comment