Showing posts with label call. Show all posts
Showing posts with label call. Show all posts

Saturday, March 24, 2012

Populate Labels with DataSet in code

I have several Label controls in my page and a stored procedure that always return 1 row. I want to call the same stored procedure with different parameter. Can anybody who me how to do this?

I have a function that contains:

 DataSet objDs =new DataSet();
System.Data.SqlClient.SqlConnection myConnection =new System.Data.SqlClient.SqlConnection(
System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

System.Data.SqlClient.SqlDataAdapter cmd;
cmd =new System.Data.SqlClient.SqlDataAdapter("sp_getCounter1", myConnection);

cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
cmd.SelectCommand.Parameters.Add(
new SqlParameter("@dotnet.itags.org.param1", mode));
DataTable dtMasterListe =new DataTable("mytable");

myConnection.Open();
cmd.Fill(objDs);

With the above code it is easy to e.g bind to datagrid, but I want to achiveve the following Label1.Text = objDs.getstring(etc).

Any clues?

public string FunctionName(){using (SqlConnection cn =new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { SqlCommand cmd =new SqlCommand("StoredProcedureName", cn); cmd.CommandType = CommandType.StoredProcedure;// Add parameters if needed cn.Open();return (string)ExecuteScalar(cmd); }}void Page_Load(){ Label1.Text = FunctionName()}

This is what I usually do:

If you already sure the dataset only contains 1 table and 1 row only, then you can use the following to get the data you want:
Label1.Text = ds.Tables[0].Rows[0][0].ToString();

If you know the table name returned in the dataset, you can also pass in the tablename:
Label1.Text = ds.Tables["tablename"].Rows[0][0].ToString();

It will even be better if you can specify the column name:
Label1.Text = ds.Tables["tablename"].Rows[0]["Name"].ToString();

I usually check if the number of tables & rows > 0 then only get the value:
if ((ds.Tables.Count == 0) && (ds.Tables["tablename"].Rows.Count == 0))

Hope this helps. ^_^



Hi,

If you know that your stored procedure will return only one row all the time, then go for theExcecuteScalar method.


Works perfect!

Had to make a minor change:


int ret = (int)cmd.ExecuteScalar();
return ret.ToString();

to make ExecuteScalar to work.


Thanks.

Wednesday, March 21, 2012

Populating a dropdownlist

Hi Everyone,

I am trying to figure out how to populate my dropdown list with data from my table. What happens is I call a method that expects an id by using this method I want to keep looping through the recordset and populate my dropdown list with teh values I require.

Here is teh code I have attempted to write this code appears in my page load


ddlVersions.Items.Clear()

Dim _CVersion As Content = ContentManager.GetContentVersionList(Page.Request.Params("PID"))
Dim Version As Content

For Each Version In _CVersion
ddlVersions.DataTextField = "CoPgVersion"
ddlVersions.DataValueField = "CoPgID"
ddlVersions.DataBind()
Next

Here is my method


Public Shared Function GetContentVersionList(ByVal CoPgID As Integer) As Content
' Get the list of Content form the datastore
Dim _Data As SqlDataReader
Dim _Content As New Content

_Data = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings("DataStoreConnection"), CommandType.StoredProcedure, "Content_get", New SqlParameter("@dotnet.itags.org.CoPgID", CoPgID))

While _Data.Read
_Content = PopulateObjectFromSqlDataReader(_Data)
End While

_Data.Close()

Return _Content

End Function

My stored procedure is basically the following SQL Statement


Select * from content where CoPgID = @dotnet.itags.org.CoPgID

I am simply getting teh syntax wrong but I am unsure on how to correctly code the dropdownlist so it populates.

While I am here once I get the drop down list to work is there a way I can make the drop down list highlight as if selected the most current record.

Basically the dropdownlist will display all teh version of my content if I am at version 4 I want version for to appear along with the rest of teh versions but for it to be select rather than "Please Select" is this doable?

I look forward to any help one may be able to give

Kind Regards

BradHi I will try to help a bit but take it with reservation as I am just beginner using Matrix which generates code for me, but just to say that when I was populating drop down list following instructions from the matrix book I did not loop through the recordset.
Steps were:
-to create function with sql statement in it and reterning the recordset
-to assign this function to data source of drop down list so:
dropdownlist.DataSource=above function
dropdownlist.DataBind()

these 2 lines are putting your recordset in the list already but they are always within some event
for example page load event
i hope this helps
anqa
Anga is quite correct. This is the way to bind data to controls in ASP.NET.

Seethis example on LearnASP.com for more details.