[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

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 :) )
0
jasont09
Asked:
jasont09
  • 5
  • 3
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
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

0
 
Rajkumar GsSoftware EngineerCommented:
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


       
                  }
0
 
Rajkumar GsSoftware EngineerCommented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jasont09Author Commented:
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"];
0
 
Rajkumar GsSoftware EngineerCommented:
The lines
string id = Request.QueryString["id"];

Open in new window

and
string strProductID = Request.QueryString["id"];

Open in new window

are getting the same querystring.

That's why I removed one line. No need to again declare another variable for the same querystring.


 protected void Page_Load(object sender, EventArgs e)
    {

      //  string id = Request.QueryString["id"];  // Repeatation
       string strProductID = Request.QueryString["id"]; 


        string hprc = Request.QueryString["hprc"];

        clsUtils objUtils = new clsUtils();
        string redirectID = objUtils.CaptureRedirectInformation(id);

        Response.Redirect("/supplier/index.asp?id=" + strProductID  + "&hprc=" + hprc + "&redi=" + redirectID);
    }

Open in new window

0
 
jasont09Author Commented:
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?
0
 
jasont09Author Commented:
This was a great help and clearly communicated, thanks.
0
 
Rajkumar GsSoftware EngineerCommented:
...is that correct?
Yes it is correct.

Glad I could help you
Raj
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now