Search this insane blog:

Saturday, August 16, 2008

Connect to a stored procedure and playing with an array

have on hand: a label called lblVerse
a stored procedure letting you have the proper permissions to reach.

  1. web.config called db3

  2. Table called "verses"

  3. a column called "verse"

  4. a stored procedure called "rndVerse"
----- STORED PROCEDURE (SCRIPT)----

create procedure [dbo].[rndVerse]
as
select verse
into [#t]
from
(select verses.verse
from verses
inner join (select top (1)
newid() as Expr1, id
from verses as verses_1
order by Expr1
) as dt
on verses.id = dt.id) as dt ;
select verse from #t ;
drop table #t ;

GO


-----code behind:-------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)

{

string MyTable =
"verses";

string MySingleColumn = "verse";
string MyWebConfigString = "db3";string MyStoredProcedure = "rndVerse";

string connString =
ConfigurationManager.ConnectionStrings[MyWebConfigString].ConnectionString.ToString();

SqlConnection conn =
new SqlConnection(connString);


conn.Open();

SqlCommand command =
new SqlCommand(MyStoredProcedure, conn);


SqlDataAdapter adapter = new SqlDataAdapter(command);


DataSet ds = new DataSet();

adapter.Fill(ds, MyTable);

if (ds.Tables[MyTable].Rows.Count < 1)


{

Response.Write("I am sorry, there are no reports to be shown");

}

int theseRowsCounted = ds.Tables[MyTable].Rows.Count - 1;
// array length (rank of1 [column], of course...)


string thisColumnHeader = ds.Tables[MyTable].Columns[0].ToString();//grab header names:

string thisColumnValues = ds.Tables[MyTable].Rows[theseRowsCounted].ItemArray[ds.Tables[MyTable].Columns.IndexOf(MySingleColumn)].ToString();// that grab the values:

ArrayList theseRows = new ArrayList();
// please notice: there is only one row rendered:

theseRows.Add(thisColumnValues);


foreach (string n in theseRows)
//
kept this way just in case you want to render more rows in fun ways

{


lblVerse.Text = n;

}


}

}

No comments:

Post a Comment