Fabio ha
Fabio ha

Reputation: 583

Getting data from sql table inside a GridView (C#)

I've got a GridView which I made in ASP.NET. Now I want it to fill itself with this method. But it doesn't work. the return() doesn't work and the GetData inside the Page_load is also underlined red(Compiler Error CS1501).

 public partial class Pages_GridView1 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GetData();
    }


    public string GetData(Chart chart)
    {

        string connStr = ConfigurationManager.ConnectionStrings["CRM_SQL"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand query = new SqlCommand(chart.Sql, conn);
        SqlDataReader rst = query.ExecuteReader();


        gridView1.DataSource = rst;
        gridView1.DataBind();
        return gridView1();

    }
}

Upvotes: 0

Views: 957

Answers (4)

Vishal prabhu lawande
Vishal prabhu lawande

Reputation: 231

I don't think there's a need of returning a string if it is not used. Also returngridView1(); doesn't return a string. It will return GridView. You could either return Gridview i.e. Change the function definition to

public Gridview GetData()

OR you can do this; the following code should do the job.

public void GetData()
{

    string connStr = ConfigurationManager.ConnectionStrings["CRM_SQL"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    conn.Open();
    SqlCommand query = new SqlCommand(chart.Sql, conn);
    SqlDataReader rst = query.ExecuteReader();


    gridView1.DataSource = rst;
    gridView1.DataBind();

}

Upvotes: 0

Midhun
Midhun

Reputation: 23

i think you need to change method return type to GridView class, because you are returning gridview1 object rite.. and check the method signature you are using is correct or not while calling the method..

Upvotes: 0

Ubaid Ashraf
Ubaid Ashraf

Reputation: 885

You are calling GetData() method without any parameters and then in method definition using GetData(Chart chart). Plus you are returning string in method definition!!

Method call : GetData()
Method definition : public string GetData(Chart chart)

Method call and definition should match

Now you should do something like this :

protected void Page_Load(object sender, EventArgs e)
{
    GetData();
}


public void GetData()
{
    Chart chart = new Chart();
    string connStr = ConfigurationManager.ConnectionStrings["CRM_SQL"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    conn.Open();
    SqlCommand query = new SqlCommand(chart.Sql, conn);
    SqlDataReader rst = query.ExecuteReader();


    gridView1.DataSource = rst;
    gridView1.DataBind();


}

Note : I am assuming you are setting sql query in Chart class

Upvotes: 2

Amit
Amit

Reputation: 890

You need to specify commandtype if you are using stored procedure.

SqlCommand query = new SqlCommand(chart.Sql, conn);
query.CommandType = CommandType.StoredProcedure;
SqlDataReader rst = query.ExecuteReader();

Your error is clearly visible.

protected void Page_Load(object sender, EventArgs e)
    {
        GetData();
    }

And signature for GetData is different.

 public string GetData(Chart chart)

Upvotes: 0

Related Questions