jasont09
asked on
C# Passing Variable to Method and Returning Variable
Hi
I need some help to learn the basics of how to pass a variable from a querystring to a method in a class, and then return some data.
I have 2 pages, the 1st page works on Page_Load and recieves the querystrings into strings and also calls the method or class in the other page.
but I need to send the productid to the method/class and then I need to return the redirectID
my 1st page code is
and my 2nd page looks like this
so the redirectID gets sent back to the 1st page so I can append it the end of the redirect url
This may seem very simple to most but its confusing the hell out of me... I have C# for dummies open in front of me ( so I am trying :) )
I need some help to learn the basics of how to pass a variable from a querystring to a method in a class, and then return some data.
I have 2 pages, the 1st page works on Page_Load and recieves the querystrings into strings and also calls the method or class in the other page.
but I need to send the productid to the method/class and then I need to return the redirectID
my 1st page code is
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string hprc = Request.QueryString["hprc"];
string strProductID = Request.QueryString["id"];
clsUtils objUtils = new clsUtils();
objUtils.CaptureRedirectInformation();
Response.Redirect("/supplier/index.asp?id=" + id + "&hprc=" + hprc + "&redi=" + redirectID);
}
and my 2nd page looks like this
public string CaptureRedirectInformation(string strProductID)
{
//capture information in session and on page
string strRefType = "";
string strRefProductId = "";
string strRefDomain = "";
string strRefSearchTerm = "";
string strRefURL = "1234";
string strRefToURL = "";
string strRefURLQueryString = "";
string strRedirectPageURL = "";
string strSiteEntryDateTime = "";
string strSiteExitDateTime = "";
string redirectID = "";
if (HttpContext.Current.Session["RefType"] != null)
strRefType = HttpContext.Current.Session["RefType"].ToString();
if (HttpContext.Current.Session["RefProductID"] != null)
strRefProductId = HttpContext.Current.Session["RefProductID"].ToString();
if (HttpContext.Current.Session["RefDomain"] != null)
strRefDomain = HttpContext.Current.Session["RefDomain"].ToString();
if (HttpContext.Current.Session["RefSearchTerm"] != null)
strRefSearchTerm = HttpContext.Current.Session["RefSearchTerm"].ToString();
if (HttpContext.Current.Session["ReferringURL"] != null)
strRefURL = HttpContext.Current.Session["ReferringURL"].ToString();
if (HttpContext.Current.Session["ReferredToURL"] != null)
strRefToURL = HttpContext.Current.Session["ReferredToURL"].ToString();
if (HttpContext.Current.Session["RefURLQueryString"] != null)
strRefURLQueryString = HttpContext.Current.Session["RefURLQueryString"].ToString();
if (HttpContext.Current.Session["RefEntryTime"] != null)
strSiteEntryDateTime = HttpContext.Current.Session["RefEntryTime"].ToString();
//strTargetURL = strProductID;
strSiteExitDateTime = DateTime.Now.ToString();
strRedirectPageURL = HttpContext.Current.Request.Url.OriginalString;
//save information to the DB
if (strRefURL.Trim() != "")
{
string insertSQL = "";
insertSQL = " SET NOCOUNT ON";
insertSQL += " insert into tbl_Redirects (Referring_URL, Referring_URL_QS, Referring_Domain, Referred_To_URL, ";
insertSQL += " Referral_Type, Referral_Search_Term, Site_Entry_Time, Site_Exit_Time, Redirect_Page_URL)";
insertSQL += " values ('" + strRefURL.Replace("'", "''") + "','" + strRefURLQueryString.Replace("'", "''") + "','" + strRefDomain.Replace("'", "''") + "','" + strRefToURL.Replace("'", "''") + "',";
insertSQL += "'" + strRefType.Replace("'", "''") + "','" + strRefSearchTerm.Replace("'", "''") + "',";
insertSQL += "'" + strSiteEntryDateTime + "','" + strSiteExitDateTime + "','" + strRedirectPageURL.Replace("'", "''") + "') ";
insertSQL += " SET NOCOUNT OFF";
insertSQL += " SELECT SCOPE_IDENTITY()";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connStr);
conn.Open();
if (conn.State == ConnectionState.Open)
{
System.Data.OleDb.OleDbCommand insertCommand = new System.Data.OleDb.OleDbCommand(insertSQL, conn);
object obj = insertCommand.ExecuteScalar();
redirectID = obj.ToString();
conn.Close();
}
}
return redirectID;
}
so the redirectID gets sent back to the 1st page so I can append it the end of the redirect url
This may seem very simple to most but its confusing the hell out of me... I have C# for dummies open in front of me ( so I am trying :) )
So if you want to pass this returned value as query string
modify this line
}
modify this line
Response.Redirect("/supplier/index.asp?id=" + id + "&hprc=" + hprc + "&redi=" + redirectID);
toResponse.Redirect("/supplier/index.asp?id=" + id + "&hprc=" + hprc + "&redi=" + redirectionInfo);
}
No need of this line
Here is modified Page_Load code of first page
string strProductID = Request.QueryString["id"];
Here is modified Page_Load code of first page
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string hprc = Request.QueryString["hprc"];
clsUtils objUtils = new clsUtils();
string redirectID = objUtils.CaptureRedirectInformation(id);
Response.Redirect("/supplier/index.asp?id=" + id + "&hprc=" + hprc + "&redi=" + redirectID);
}
ASKER
Hi RajkumarGS
thanks for this... looking at the code for the page you sent... it doesnt send the productid to objUtils.CaptureRedirectIn formation which I need to do, because I am going to store it into the db
can you show me how to do send the productid
the productid is
Request.QueryString["id"];
thanks for this... looking at the code for the page you sent... it doesnt send the productid to objUtils.CaptureRedirectIn
can you show me how to do send the productid
the productid is
Request.QueryString["id"];
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok I get that now...
so this part
objUtils.CaptureRedirectIn formation( id)
sends the id (which is the productid)
and this part
public string CaptureRedirectInformation (string strProductID)
recieves it.
is that correct?
so this part
objUtils.CaptureRedirectIn
sends the id (which is the productid)
and this part
public string CaptureRedirectInformation
recieves it.
is that correct?
ASKER
This was a great help and clearly communicated, thanks.
...is that correct?
Yes it is correct.
Glad I could help you
Raj
Yes it is correct.
Glad I could help you
Raj
Change this line
Open in new window
toOpen in new window