Following is my stored procedure.
ALTER PROCEDURE SP_GetModels
(
@CategoryID bigint
)
AS
BEGIN
Select ModelID,ModelName From Model where CategoryID=@CategoryID
END
and i am calling stored procedure in code behind as
public SqlConnection conn;
public SqlDataReader GetModels()
{
DataTable dt = new DataTable();
public void DbConnection()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleCs"].ConnectionString);
conn.Open();
}
DbConnection();
SqlCommand cmd = new SqlCommand("SP_GetModels", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CategoryID", SqlDbType.BigInt, 10).Value = CategoryID;
// SqlDataAdapter madap = new SqlDataAdapter(cmd, conn);
SqlDataReader dreader= cmd.ExecuteReader();
//madap.Fill(dt);
return dreader;
}
I have a dropdownlist to which i have to bind datareader object which contain modelname. how can i set datasource to dropdownlist as datareader
We can pass the DataTable to the Stored Procedure using ADO.Net in the same way as we provided using the System. Data. SqlParameter class, but needs a few changes in the datatype. Normally we provide DbType of SqlParameter for a normal parameter like varchar, nvarchar, int and so on as in the following code.
In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.
First a Table Variable of User Defined Table Type has to be created of the same schema as that of the Table Valued parameter. Then it is passed as Parameter to the Stored Procedure and the Stored Procedure is executed using the EXEC command in SQL Server.
A DataTable represents one table of in-memory relational data; the data is local to the . NET-based application in which it resides, but can be populated from a data source such as Microsoft SQL Server using a DataAdapter For more information, see Populating a DataSet from a DataAdapter.
private void PopDataBaseName()
{
try
{
SqlCommand cmd = new SqlCommand("sp_generate_report", con);
cmd.Parameters.Add("@TABLE_NAME", SqlDbType.VarChar,100).Value = TextBox1.Text;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
}
catch (Exception ex)
{
}
}
You should be able to directly bind the SqlDataReader to the drop down list like this:
MyDropDownList.DataSource = GetModels();
MyDropDownList.DataTextField = "ModelName";
MyDropDownList.DataValueField = "ModelID";
You need to also specify which member (property) is going to be displayed (DataTextField), and which one will be used as value when an entry is selected in the drop down list (DataValueField).
I would strongly recommend you grab the data from the SqlDataReader in your GetModels()
procedure, create instances of a Model
class which will hold those fields you have and need, close the SqlDataReader, and then return it as a List<Model>
and bind that list to the drop down list. MUCH better than directly binding a SqlDataReader!
public class Model
{
public int ModelID { get; set; }
public string ModelName { get; set; }
}
And in your GetModels():
public List<Model> GetModels()
{
List<Model> result = new List<Model>();
using(SqlConnection conn = new SqlConnection(ConfigurationManager.
ConnectionStrings["SampleCs"].ConnectionString))
{
using(SqlCommand cmd = new SqlCommand("SP_GetModels", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CategoryID", SqlDbType.BigInt, 10).Value = CategoryID;
conn.Open();
using(SqlDataReader dreader = cmd.ExecuteReader())
{
while(dreader.Read())
{
Model workItem = new Model()
{ ModelID = dreader.GetInt(0),
ModelName = dreader.GetString(1) };
result.Add(workItem);
}
reader.Close();
}
conn.Close();
}
}
return result;
}
Marc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With