Solved

Caling a subrouting in asp.net c#

Posted on 2010-09-15
11
369 Views
Last Modified: 2012-05-10
Experts,

Newbie question.

I want to call a subroutine in a asp.net c# webform.

I get a error saying:
"Compiler Error Message: CS1501: No overload for method 'AppendSQL' takes 0 arguments"
See code below.
Thanks
protected void googButton1_Click(object sender, ImageClickEventArgs e)
    {
        string gogetit = "http://www.google.com/search?source=ig&hl=en&q=" + TextBox1.Text;
        AppendSQL();
        Response.Redirect(gogetit);
    }



    protected void AppendSQL(object s, EventArgs e)
    {
        string strConn = ConfigurationManager.ConnectionStrings["CS1"].ToString();
        string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
        using (SqlConnection con = new SqlConnection(strConn))
        {
            // 1. Instantiate a new command
            SqlCommand cmd = new SqlCommand(@"INSERT INTO gogetit (term,url,IP,createdate) Values ('"
               + TextBox1.Text + "','"
               + gogetit + "','"
               + clientIPAddress
               + "', getdate())");
            // 2. Set the Connection property
            cmd.Connection = con;
            // 3. Open connection
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                // 4. Call ExecuteNonQuery to send command
                cmd.ExecuteNonQuery();
            }
            catch (SqlException se)
            {
                Response.Write("Result: " + se.Message);
            }
            cmd.Dispose();
            con.Close();
        }
    }

Open in new window

0
Comment
Question by:AndyPandy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33684383
you have to pass some parameters, you can use the follow:
 AppendSQL(null, null);
0
 
LVL 9

Expert Comment

by:wellhole
ID: 33684385
Your subroutine requires 2 parameters as shown in your function declaration "protected void AppendSQL(object s, EventArgs e)". However, your call to this in googButton1_Click does not pass it any parameters.
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33684397
you can pass null values as parameter because it not are used inside the function
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:AndyPandy
ID: 33684440
I passed it two parameters (null,null),  but now it can't find the variable "gogetit"  that was set in the calling routine.
What do I do to have AppendSQL to see the variable  "gogetit"?

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 33684480
AppendSql appears to be intended to be used as an event handler. The reason you can't get at "gogetit" is because it is scoped to the method where it is defined. If you want it to be available to the method you either need to scope it at the class level, or alter the signature of the AppendSql() method to accept an extra parameter.
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33684492
enter in Debug mode an analyse the callstack to see where the variable has been setted
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 33684793
Here is the code for the solution mentioned by carl_tawn:


protected void googButton1_Click(object sender, ImageClickEventArgs e)
    {
        string gogetit = "http://www.google.com/search?source=ig&hl=en&q=" + TextBox1.Text;
        AppendSQL(null, null, gogetit);
        Response.Redirect(gogetit);
    }



    protected void AppendSQL(object s, EventArgs e, string gogetit)
    {
        string strConn = ConfigurationManager.ConnectionStrings["CS1"].ToString();
        string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
        using (SqlConnection con = new SqlConnection(strConn))
        {
            // 1. Instantiate a new command
            SqlCommand cmd = new SqlCommand(@"INSERT INTO gogetit (term,url,IP,createdate) Values ('"
               + TextBox1.Text + "','"
               + gogetit + "','"
               + clientIPAddress
               + "', getdate())");
            // 2. Set the Connection property
            cmd.Connection = con;
            // 3. Open connection
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                // 4. Call ExecuteNonQuery to send command
                cmd.ExecuteNonQuery();
            }
            catch (SqlException se)
            {
                Response.Write("Result: " + se.Message);
            }
            cmd.Dispose();
            con.Close();
        }
    }

Open in new window

0
 
LVL 9

Accepted Solution

by:
puru1981 earned 300 total points
ID: 33685108
Two changes
1. declare variable gogetit  Class level
2. AppendSQL(null, null);

string gogetit= string.Empty;
protected void googButton1_Click(object sender, ImageClickEventArgs e)
    {
        gogetit = "http://www.google.com/search?source=ig&hl=en&q=" + TextBox1.Text;
        AppendSQL(null,null);
        Response.Redirect(gogetit);
    }



    protected void AppendSQL(object s, EventArgs e)
    {
        string strConn = ConfigurationManager.ConnectionStrings["CS1"].ToString();
        string strHostName = System.Net.Dns.GetHostName();
        string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
        using (SqlConnection con = new SqlConnection(strConn))
        {
            // 1. Instantiate a new command
            SqlCommand cmd = new SqlCommand(@"INSERT INTO gogetit (term,url,IP,createdate) Values ('"
               + TextBox1.Text + "','"
               + gogetit + "','"
               + clientIPAddress
               + "', getdate())");
            // 2. Set the Connection property
            cmd.Connection = con;
            // 3. Open connection
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                // 4. Call ExecuteNonQuery to send command
                cmd.ExecuteNonQuery();
            }
            catch (SqlException se)
            {
                Response.Write("Result: " + se.Message);
            }
            cmd.Dispose();
            con.Close();
        }
    }

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33685578
Although, mrichmons code will do the, it will cause you a problem if AppendSql() is actually being used as an event handler as it will no longer match the required signature for the EventHandler delegate, in which case you'll just have to increase the scope of the "gogetit" variable.
0
 

Author Closing Comment

by:AndyPandy
ID: 33686126
The 'trick' I missed was to declare the variable in the
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33686528
You're webform is a class that derives (inherits in OO speak) from the Page class. AppendSql() is referred to as a "method" of the class.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question