Showing posts with label working. Show all posts
Showing posts with label working. Show all posts

Saturday, March 24, 2012

Pass variable into a user control

Please help, I've been working on this for hours and can't seem to find an answer online.

I have a main page (index.aspx) that uses a usercontrol (DropDownSearch.ascx). Inside the usercontrol are 3 dropdownlists with a code behind file that fills them with data from a query. The query is based on a variable which is sent by the main (index.aspx) file. Inside the code behind for the main page (index.aspx) I try to send the variable over to the codebehind of the user control, but it keeps failing.

Here is the user control info in index.aspx:
<%@dotnet.itags.org.Register TagPrefix="MyControl" Tagname="DropDownSearch" src="http://pics.10026.com/?src=dropdownsearch.ascx"%>
<MyControl:DropDownSearch ID="SearchMenus" Runat="server"></MyControl:DropDownSearch
Here is the code for the index.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MainPage
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton Functional;
protected System.Web.UI.WebControls.RadioButton Pwb;
protected System.Web.UI.WebControls.RadioButton Decorative;
protected System.Web.UI.WebControls.RadioButton Microelectronics;
public string SqlCat1, SqlCat2;
protected SearchMenus DropDownSearch;

private void Page_Load(object sender, System.EventArgs e)
{

}

protected void CategorySelected(object sender, System.EventArgs e)
{

if (Functional.Checked)
{
SearchMenus.SqlCat1 = "1";
SearchMenus.SqlCat2 = "1";
}

if (Decorative.Checked)
{
SearchMenus.SqlCat1 = "2";
SearchMenus.SqlCat2 = "2";
}

if (Pwb.Checked)
{
SearchMenus.SqlCat1 = "3";
SearchMenus.SqlCat2 = "3";
}

if (Microelectronics.Checked)
{
SearchMenus.SqlCat1 = "4";
SearchMenus.SqlCat2 = "4";
}

}
}
}

Here is the user control code behind (dropdownsearch.ascx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Enthone
{
/// <summary>
/// Summary description for tier.
/// </summary>
public class dropdownsearch : System.Web.UI.UserControl
{
SqlConnection SearchConnection;
protected DataSet MyDataSet = new DataSet();
public DropDownList TradeNames, Applications, Processes;
public string SqlCat1, SqlCat2;

private void Page_Load(object sender, System.EventArgs e)
{
SearchConnection = new SqlConnection("Server=localhost;uid=sa;password=;database=Enthone");
string MySqlStatement = "sp_DropDownsSql " +SqlCat1+", "+SqlCat2;
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySqlStatement, SearchConnection);
MyAdapter.Fill(MyDataSet);

TradeNames.DataSource = MyDataSet.Tables["Table"];
TradeNames.DataBind();

Applications.DataSource = MyDataSet.Tables["Table1"];
Applications.DataBind();

Processes.DataSource = MyDataSet.Tables["Table2"];
Processes.DataBind();

SearchConnection.Close();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}
}

ThanksHow is it failing? Do you get an exception? (If so, what is the complete exception text?) Incorrect results? What's going wrong?

Don
When I go to build it from vs.net I get this error in the index.aspx.cs file, "c:\inetpub\wwwroot\MyApp\index.aspx.cs(25): The type or namespace name 'SearchMenus' could not be found (are you missing a using directive or an assembly reference?)
"
Okay, that means that it can't find the SearchMenus namespace. Where is that located? In another project? Is it open in your VS.NET solution file? You'll need to add a reference to it in Solution Explorer, by right-clicking on References in the project with Index.aspx and adding a reference to the project or DLL.

There are lots of ways to configure these things in VS.NET though, so explain where SearchMenus is located and we should be able to figure out what's wrong.

Don
O.K. I got the whole thing working, but would definitly appreciatea look at my code to make sure I'm handling it the right way...

User Control Info on index.aspx:


<%@.Register TagPrefix="MyControl" Tagname="DropDownSearch" src="http://pics.10026.com/?src=dropdownsearch.ascx"%
<MyControl:DropDownSearch ID="SearchMenus" Runat="server"></MyControl:DropDownSearch>

Code Behind of index.aspx:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton Functional;
protected System.Web.UI.WebControls.RadioButton Pwb;
protected System.Web.UI.WebControls.RadioButton Decorative;
protected System.Web.UI.WebControls.RadioButton Microelectronics;
public string SqlCat1, SqlCat2;
protected MyNamespace.dropdownsearch SearchMenus;

private void InitializeComponent()
{

}

private void Page_Load(object sender, System.EventArgs e)
{
}

protected void CategorySelected(object sender, System.EventArgs e)
{

if (Functional.Checked)
{
SearchMenus.SqlCat1 = "1";
SearchMenus.SqlCat2 = "1";
SearchMenus.DbCall();
}

if (Decorative.Checked)
{
SearchMenus.SqlCat1 = "2";
SearchMenus.SqlCat2 = "2";
SearchMenus.DbCall();
}

if (Pwb.Checked)
{
SearchMenus.SqlCat1 = "3";
SearchMenus.SqlCat2 = "3";
SearchMenus.DbCall();
}

if (Microelectronics.Checked)
{
SearchMenus.SqlCat1 = "4";
SearchMenus.SqlCat2 = "4";
SearchMenus.DbCall();
}

}
}
}


Code behind of User Control:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for tier.
/// </summary>
public class dropdownsearch : System.Web.UI.UserControl
{
SqlConnection SearchConnection;
protected DataSet MyDataSet = new DataSet();
public DropDownList TradeNames, Applications, Processes;
public Label MyLabel;
private string _SqlCat1;
private string _SqlCat2;

public string SqlCat1
{

get
{
return _SqlCat1;
}
set
{
_SqlCat1 = value;
}
}

public string SqlCat2
{

get
{
return _SqlCat2;
}
set
{
_SqlCat2 = value;
}
}

private void Page_Init(object sender, System.EventArgs e){

}

private void Page_Load(object sender, System.EventArgs e)
{
}

public void DbCall()
{
SearchConnection = new SqlConnection("Server=localhost;uid=sa;password=;database=db");
string MySqlStatement = "sp_DropDownsSql " +SqlCat1+", "+SqlCat2;
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySqlStatement, SearchConnection);
MyAdapter.Fill(MyDataSet);

TradeNames.DataSource = MyDataSet.Tables["Table"];
TradeNames.DataBind();

Applications.DataSource = MyDataSet.Tables["Table1"];
Applications.DataBind();

Processes.DataSource = MyDataSet.Tables["Table2"];
Processes.DataBind();

SearchConnection.Close();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}
}

Thanks

Wednesday, March 21, 2012

Passing A Session Variable Form Page to Page ~ No Working

Hi All,

I am trying to passing a session variable from Page to Page but its not working!

the user basicaly logins in on the login.aspx and when their credentials are verifed by the MSSAccess database they are tehn redirected to the next page...heres a snip of code

~~~~~~~~~~~~~~
session("ID") = dslogin.tables("UserInfo").rows(0).item("ID")

response.redirect("./success.aspx")
~~~~~~~~~~~~~~~~~~

Heres the code for the page I am trying to access after beigng verifed

~~~~~~~~~~
sub page_load(byval sender as object, byval e as eventargs)

if Len(Session("ID")) = 0 then

response.redirect("./login.aspx")

end if
~~~~~~~~~~~~~~~

wots happeniong is even if you use the correct userid and password the code on the second page still sends you back to the login.aspx???

Can anyone please shine a light on this as I am going around in circles...

many thanks

Tony

end subHi Tony,
Any chance you are redirecting from https to http?
Hi & Thanks for your response

its all HTTP:
are you debugging to verify that your DataBase value is actually being inserted into Session upon logging in?
no ...but i would love to learn how......

Please can you tell me how to do this plase

I have this name sapce in

<%@. Page Language="VB" Debug="true" %
rgs

Tony
Hi,

Thanks for your help so far..

I think it is passing but now I am gettimng this error and I dont know what it means!!

Compiler Error Message: BC30545: Property access must assign to the property or use its value.

Source Error:

Line 7: sub page_load(byval sender as object, byval e as eventargs)
Line 8:
Line 9: request.querystring("UserID")
Line 10:
Line 11: if Len(Session("UserID")) = 0 then

any help would be most appreciated

rgs

Tony
You have to assign a variable to store the QueryString data, like:

dim foo as string = request.querystring("bar")

Are you using VS.NET and developing this app locally? If so you can just add a breakpoint to a line of code by pressing f9 and when that line is met, you can step through the code and hover you mouse over variables to see what they contain, or add them to the watch list.

Friday, March 16, 2012

Passing a value to a JavaScript function from the database

Hi Steven,

Thanks for following up. I believe I have it working now, using the prior
suggestion.

On the C# side, my code looks roughly like this:

String scriptString = "<script language=JavaScript> ";
scriptString += "var ... ";
scriptString += "function FunctionName() {... document.Form1.strValue1.value
...}";
scriptString += "</script> ";

RegisterHiddenField("strValue1", "...");
RegisterHiddenField("strValue2", "...");

RegisterStartupScript("startup", scriptString);

It tests Ok, but I haven't put it into production yet.

Regards,
Scott

"Steven Cheng[MSFT]" <v-schang@dotnet.itags.org.online.microsoft.com> wrote in message
news:qPlTs2UoEHA.2640@dotnet.itags.org.cpmsftngxa06.phx.gbl...
> Hi Scott,
> Have you had a chance to check bruce's suggestions or have you got any
> further ideas on this issue? If you need any further assistance, please
> feel free to post here.Thanks.
>
> Regards,
> Steven Cheng
> Microsoft Online Support
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)Thanks for your followup Scott!

Anyway, if you have any problems in the future, please feel free to post
here. Thanks.
Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Passing a Variable

Hi All,

Please can you help...my code is not working when I try and pass my variable from page to another

Can anyone spot wots wrong here...i tHinks its to do with my error message...

many thanks

Tony

dim lota as integer

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)

lota = Request.QueryString("lota")

If Not Page.IsPostBack Then
'if Not IsPostBack Then
Dim DBConn as OleDbConnection
Dim DBCommand As OleDbDataAdapter
Dim DSPageData as New DataSet
DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& Server.MapPath("/PWSC/" _
& "auction.mdb;"))
DBCommand = New OleDbDataAdapter _
("Select LotNo, Date, title, txt " _
& "From Table1 " _
& "Where LotNo = &lota "_
& "Order By LotNo ", DBConn)
DBCommand.Fill(DSPageData, _
"LotDescription")
dg.DataSource = _
DSPageData.Tables("LotDescription").DefaultView
dg.DataBind()
dbconn.close
End If
End Sub

Exactly what is the problem? What is the error you are getting?

One possible problem is fixed here:


DBCommand = New OleDbDataAdapter _
("Select LotNo, Date, title, txt " _
& "From Table1 " _
& "Where LotNo = " & lota.ToString() _
& "Order By LotNo ", DBConn)

Declare variable as protected and see if you can access it.
Hello U have to pass this parameter &loto u don't give any value to that !

Try this

DbCommand.SelectCommand.CommandText = ("Select LotNo, Date, title, txt " _

& "From Table1 " _

& "Where LotNo = &lota "_

& "Order By LotNo ")

and Now add this Paramater
DbCommand.SelectCommand.Paramters.Add("&lota",lota )
and then fill and bind it should works I also recommend u to use ? instead & for mdb Database parameters !

I hope it helps !

passing a variable from page to page

I am working on a web app that takes a selection from a dropdown and uses the selection name to pass to the next page. I would like to know how to do this. When the selection is made, the user would press a button to pass the var and display the next page.

I need to know what to do on the first page and the how to accept the var on the second page.In the button's onclick event

onClick="document.location.href='pagename.aspx?selval=' + document.getElementById('ddl1').options[document.getElementById('ddl1').selectedIndex].value"
My next question is how do I define the var in the next page to use it.
What would the code look like?
To send value from one form to another:

Response.Redirect("Page2.aspx?abc="+dropDownList1.SelectedItem.Text );

to Receive the value in another page:

string sValue= Convert.ToString(Request.QueryString["abc"]);

Best of Luck.

-Rajib
Thanks, that worked out great!!!
Request.QueryString will return a string.