Showing posts with label purpose. Show all posts
Showing posts with label purpose. Show all posts

Friday, March 23, 2012

Nesting a SP within another SP?

I have a stored procedure that calls some UDF User Defined Functions,
the purpose of which is to create row strings out of numerous column
strings for matching uniqueIDs.

The problem is I need to join that SP with some other tables.

The SP I have reads something like:

mySPName
@.myUserID int
SELECT myUniqueID, dbo.fn_myFunctionName(UniqueID) As myRunningString
FROM myTEMPTableName
GROUP BY myUniqueID
WHERE myTEMPTableName.UserID = @.myUserID

I need to join that result with myTableName on myUniqueID such as:
Select myTableName.myField1, myTableName.myField2,
mySPName.myRunningString
From ...
-- joining myTableName.myUniqueID = mySPName.myUniqueID

Can this be done?
The reason I don't just do it with a View instead of an SP is that I
have that parameter that must be passed to filter the records in
myTEMPTableName.

Any help is appreciated.
lq

oh...
the UDF looks like:

Create Function dbo.fn_myFunctionName(@.myUniqueID as int) returns
nvarchar(500)
AS
BEGIN
DECLARE @.ret_value nvarchar(500)
SET @.ret_value=''
SELECT @.ret_value=@.ret_value + ';' + myString
FROM myTEMPTableName
WHERE
myUniqueID =@.myUniqueID
RETURN RIGHT(@.ret_value,Len(@.ret_value)-2)
ENDlaurenquantrell@.hotmail.com (Lauren Quantrell) wrote in message news:<47e5bd72.0401272245.449a0756@.posting.google.com>...
> I have a stored procedure that calls some UDF User Defined Functions,
> the purpose of which is to create row strings out of numerous column
> strings for matching uniqueIDs.
> The problem is I need to join that SP with some other tables.
> The SP I have reads something like:
> mySPName
> @.myUserID int
> SELECT myUniqueID, dbo.fn_myFunctionName(UniqueID) As myRunningString
> FROM myTEMPTableName
> GROUP BY myUniqueID
> WHERE myTEMPTableName.UserID = @.myUserID
> I need to join that result with myTableName on myUniqueID such as:
> Select myTableName.myField1, myTableName.myField2,
> mySPName.myRunningString
> From ...
> -- joining myTableName.myUniqueID = mySPName.myUniqueID
> Can this be done?
> The reason I don't just do it with a View instead of an SP is that I
> have that parameter that must be passed to filter the records in
> myTEMPTableName.
> Any help is appreciated.
> lq
> oh...
> the UDF looks like:
> Create Function dbo.fn_myFunctionName(@.myUniqueID as int) returns
> nvarchar(500)
> AS
> BEGIN
> DECLARE @.ret_value nvarchar(500)
> SET @.ret_value=''
> SELECT @.ret_value=@.ret_value + ';' + myString
> FROM myTEMPTableName
> WHERE
> myUniqueID =@.myUniqueID
> RETURN RIGHT(@.ret_value,Len(@.ret_value)-2)
> END

There are some options described here:

http://www.sommarskog.se/share_data.html

From your description, rewriting the stored procedure as a
table-valued UDF sounds like it should be possible.

Simon

Monday, March 19, 2012

Nested Repeater Query

Hello Everyone,

I am trying to create a query for the purpose of a nested repeater relation. The information needs to be pulled from one table. I have shortened the columns to the ones that are required.

table - Pages

ID
PageName
ParentPageID

So, take the following example:

ID 14, PageName - Service A, ParentPage ID = 6
ID 15, PageName - Service B, ParentPage ID = 6
ID 36 PageName - Client 1, ParentPage ID = 14
ID 37 PageName - Client 2, ParentPage ID = 14
ID 38 PageName - Client 3, ParentPage ID = 15
ID 39 PageName - Client 4, ParentPage ID = 15

So, I want to create a query that will get my nested repeater to display as follows:

Service A
Client 1
Client 2
Service B
Client 3
Client 4

What I have come up with so far is:

SELECT * from tbl_Pages WHERE ParentPageID IN (Select ID From tbl_Pages)

SELECT p.ParentPageID, p.PageName, p.ID FROM tbl_Pages p


The relation would be based off ParentPageID. I keep getting errors that either there is no unique value or the relation is null. What am I am missing here?

Replace

SELECT * from tbl_Pages WHERE ParentPageID IN (Select ID From tbl_Pages)

SELECT p.ParentPageID, p.PageName, p.ID FROM tbl_Pages p

with

SELECT * from tbl_Pages Parent inner join tbl_Pages Child on Parent.Id = Child.ParentPageID

Inform me if this works for u

|||

Thank you, the query worked great! I am now trying to make it work with a nested repeater however, and I am getting the following error:

System.IndexOutOfRangeException: Cannot find table 1.


I am assuming this is because this query places everything in 1 table, right? Here is my codebehind (I haven't placed the query in a stored procedure yet)

public partial class Controls_RightSideBarNavTree : BaseControl
{
protected void Page_Load(object sender, EventArgs e)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["dsn1"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connString);
SqlCommand MyCommand = new SqlCommand("SELECT * from tbl_Pages Parent inner join tbl_Pages Child on Parent.Id = Child.ParentPageID", myConnection);
SqlDataAdapter ad = new SqlDataAdapter(MyCommand);
DataSet ds = new DataSet();
ad.Fill(ds);

ds.Relations.Add(new DataRelation("NavigationRelation", ds.Tables[0].Columns["ParentPageID"], ds.Tables[1].Columns["ParentPageID"]));
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
}

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView drv = e.Item.DataItem as DataRowView;
Repeater childRepeater = e.Item.FindControl("childRepeater") as Repeater;
childRepeater.DataSource = drv.CreateChildView("NavigationRelation");
childRepeater.DataBind();
}
}

|||

try this

u have to add next to the repeater in an item template a label with the Eval("Id"), and lets give it an id="label1"

protected void Page_Load(object sender, EventArgs e)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["dsn1"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connString);
SqlCommand MyCommand = new SqlCommand("SELECT * from tbl_Pages Parent inner join tbl_Pages Child on Parent.Id = Child.ParentPageID", myConnection);
SqlDataAdapter ad = new SqlDataAdapter(MyCommand);
DataSet ds = new DataSet();
ad.Fill(ds);

Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
}
}

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{

string connString = System.Configuration.ConfigurationManager.ConnectionStrings["dsn1"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connString);
SqlCommand MyCommand = new SqlCommand("SELECT * from tbl_Pages Parent inner join tbl_Pages Child on Parent.Id = Child.ParentPageID where Parent.Id = @.ID",myConnection);
Label lbl = (Label)e.Item.FindControl("Label1");

MyCommand.Parameters.Add("@.Id",lbl.Text);
SqlDataAdapter ad = new SqlDataAdapter(MyCommand);
DataSet ds = new DataSet();
ad.Fill(ds);
Repeater childRepeater = e.Item.FindControl("childRepeater") as Repeater;
childRepeater.DataSource = ds;
childRepeater.DataBind();
}
}

Inform me if this works for u.

|||

Thank you very much for your help. I see that you are from Lebanon. I am originally from Damascus :)

Anyway, I had to make a change to the first query in order to get the correct results for the first query...
SELECT * from tbl_Pages WHERE ParentPageID = @.ID (@.ID is taken from a class BasicPageInfo.ID)

However, when using the 2nd query for the childrepeater, I get the following error:
System.NullReferenceException: Object reference not set to an instance of an object. on Line 47 childRepeater.DataSource = ds;

When I run the query in SQL Server Management Studio I get the correct result when I pass, for example, 32 for the @.ID.

This error was happening when i used the first query the way you had indicated as well. Seems to me, that for some reason, the dataset is not being populated correctly.

This is what I intend on doing. Before trying to use a nested repeater, these were my functions to create a navigation menu. This worked just fine, but I couldn't figure out how to insert a nested repeater into the below code (I took this over from another developer).

public partial class Controls_RightSideBarNavTree : BaseControl
{
protected void Page_Load(object sender, EventArgs e)
{
PagesDB sdb1 = new PagesDB();
BasicPageData[] dataArray1 = null;
int num1 = -1;

if (base.BasicPageInfo != null)
{
dataArray1 = sdb1.Page_GetAllWithParentID(base.BasicPageInfo.ID);
num1 = base.BasicPageInfo.ID;
if (dataArray1.Length == 0)
{
dataArray1 = sdb1.Page_GetAllWithParentID(base.BasicPageInfo.ParentPageID);
num1 = base.BasicPageInfo.ParentPageID;
}

/* Checks to see if page is a content page. If so, applied Header Text */
if (num1 > 0)
{
BasicPageData data1 = sdb1.Page_GetItemByID(num1);
this.lblHeader.Text = data1.PageName;
this.Repeater1.DataSource = dataArray1;
this.Repeater1.DataBind();
}
else
{
this.lblHeader.Text = "";
}
}

}

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
BasicPageData data1 = (BasicPageData)e.Item.DataItem;
HyperLink link1 = (HyperLink)e.Item.FindControl("hypItem");
HtmlGenericControl control1 = (HtmlGenericControl)e.Item.FindControl("liItem");
if (null == data1) return;
link1.Text = data1.PageName;
link1.NavigateUrl = GlobalData.TRCBaseURL + "content/default.aspx?" + BaseControl.PAGE_GUID + "=" + data1.GUID.ToString() + "&" + BaseControl.TOOLBARPAGE_GUID + "=" + base.ToolbarPageGUID.ToString();
if (base.PageGUID.Equals(data1.GUID))
{
control1.Attributes.Add("class", "subOn");
}
}
}

}

|||

Hi adarwich,

From the context, I didn't see the control childRepeater has been initialized. Please check if childRepeater is a valid control on the page and it has a valid reference to an object.

Wednesday, March 7, 2012

Need Web Client for doing Adhoc querry

Does anyone know of a good Web Client where I can expose SQL tables to
selected users for the purpose of doing AdHoc query?
ThanksSeveral listed here, not all web-based though, you'll have to fish for them
yourself, but it's better than sending you to Google. :-)
http://www.aspfaq.com/2442
"Brian Kitt" <BrianKitt@.discussions.microsoft.com> wrote in message
news:5BEB54E4-A8A5-4684-AF09-2AD801229F1C@.microsoft.com...
> Does anyone know of a good Web Client where I can expose SQL tables to
> selected users for the purpose of doing AdHoc query?
> Thanks