Dear Friends,
I want to use Sqlparameters() in asp.net web forms.can any1 tell me how to use and pass parameters using it.
Thanks & Regards,
Aamir
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Anuja PawarPosted Dec 19, 2011, 9:13 AM
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
>
runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if (!Page.IsPostBack) {
SqlConnection MyConnection;
SqlCommand MyCommand;
SqlDataReader MyReader;
SqlParameter ProductNameParam;
MyConnection = new SqlConnection();
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString;
MyCommand = new SqlCommand();
MyCommand.CommandText = "SELECT * FROM PRODUCTS WHERE PRODUCTNAME = @PRODUCTNAME";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
ProductNameParam = new SqlParameter();
ProductNameParam.ParameterName = "@PRODUCTNAME";
ProductNameParam.SqlDbType = SqlDbType.VarChar;
ProductNameParam.Size = 25;
ProductNameParam.Direction = ParameterDirection.Input;
ProductNameParam.Value = "CHAI";
MyCommand.Parameters.Add(ProductNameParam);
MyCommand.Connection.Open();
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
GridView1.DataSource = MyReader;
GridView1.DataBind();
MyCommand.Dispose();
MyConnection.Dispose();
}
}
xmlns="http://www.w3.org/1999/xhtml">
runat="server">
id="form1" runat="server">
Satyapriya NayakPosted Dec 19, 2011, 5:14 AM
try this..
protected void Results()
{
string connectionstring = "server=MyServer;database=MyDatabaseDB;" +
"uid=superuser;password=superpwd" ;
SqlClient.SqlParameter sqlparams[3];
sqlparams[0] = new SqlClient.SqlParameter("@inputone", SqlDbType.Int);
sqlparams[0].Value = Convert.ToInt32(ddlInputOne.SelectedItem.Value);
sqlparams[1] = new SqlClient.SqlParameter("@inputtwo",SqlDbType.TinyInt);
sqlparams[1].Value = Convert.ToInt16(ddlInputtwo.SelectedItem.Value);
sqlparams[2] = new SqlClient.SqlParameter("@inputthree",SqlDbType.SmallInt);
sqlparams[2].Value = Convert.ToInt16(ddlInputthree.SelectedItem.Value);
dgMyDataGrid.DataSource() =
Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(connectionstring,
CommandType.StoredProcedure, "MyStoredProcedure",sqlparams);
dgMyDataGrid.DataBind();
}
SQL Parameters can also have a different syntax of:
SqlParameter[] sqlparams = {
new SqlParameter("@inputone"...),
new SqlParameter("@inputtwo"...),
new SqlParameter("@inputthree"...)
};
Please refer the below link
http://www.codeproject.com/KB/database/NET_Data_Access.aspx
Thanks