Link to home
Start Free TrialLog in
Avatar of jasont09
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
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);

        
			}

Open in new window


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;
}

Open in new window


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 :) )
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

You need to pass the parameter to the function 'CaptureRedirectInformation' and return the value into a parameter string.
Change this line
objUtils.CaptureRedirectInformation();

Open in new window

to
string redirectionInfo = objUtils.CaptureRedirectInformation(strProductID);

Open in new window

So if you want to pass this returned value as query string
modify this line
Response.Redirect("/supplier/index.asp?id=" + id + "&hprc=" + hprc + "&redi=" + redirectID);

Open in new window

to
Response.Redirect("/supplier/index.asp?id=" + id + "&hprc=" + hprc + "&redi=" + redirectionInfo);

Open in new window


       
                  }
No need of this line
string strProductID = Request.QueryString["id"];

Open in new window


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);
    }

Open in new window

Avatar of jasont09
jasont09

ASKER

Hi RajkumarGS

thanks for this... looking at the code for the page you sent... it doesnt send the productid to objUtils.CaptureRedirectInformation 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"];
ASKER CERTIFIED SOLUTION
Avatar of Rajkumar Gs
Rajkumar Gs
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok I get that now...

so this part

objUtils.CaptureRedirectInformation(id)

sends the id (which is the productid)

and this part

public string CaptureRedirectInformation(string strProductID)

recieves it.

is that correct?
This was a great help and clearly communicated, thanks.
...is that correct?
Yes it is correct.

Glad I could help you
Raj