I am tryiny to display a column from SQL Server Databse in Sharepoint Environment. I am using SPGridView for Consistent look and feel.
There is stored procedure called "sp_GetMyApplications" which gives all the records in the table.
I do not know, what I am missing. I cannot attach worker process to debug because it says the breakpoint cannot be hit.
Can anyone help me, What is wrong with this code and where should be the correction. This is how the webpart renders in the site when used.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Text;
namespace Application
{
[Guid("c51a50e8-661b-4711-bd5d-c73531c893f3")]
public class Application : System.Web.UI.WebControls.WebParts.WebPart
{
SqlCommand cmd;
SqlConnection con;
SPGridView spGrid = new SPGridView();
public Application()
{
}
protected override void CreateChildControls()
{
}
protected override void Render(HtmlTextWriter writer)
{
con = new SqlConnection("Data Source=.;Initial Catalog=MyProject;Integrated Security=True");
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "sp_GetMyApplications";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
SPBoundField spFName = new SPBoundField();
spFName.DataField = "FirstName";
spGrid.DataSource = ds.Tables[0];
spGrid.AutoGenerateColumns = false;
spGrid.Columns.Add(spFName);
spGrid.DataBind();
writer.Write(spGrid);
}
}
}
Update:
I have modified my code as per the comments provided by the below users and my code works great.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Text;
namespace EditData
{
[Guid("3707b6d7-d757-4508-8bf4-edb35a53764f")]
public class EditData : System.Web.UI.WebControls.WebParts.WebPart
{
public EditData()
{
}
SqlCommand cmd;
SqlConnection con;
DataSet ds;
SqlDataAdapter da;
SPGridView spGrid = new SPGridView();
protected override void CreateChildControls()
{
con = new SqlConnection("Data Source=.;Initial Catalog=MyProject;Integrated Security=True");
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "sp_GetMyApplications";
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter();
ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
spGrid.AutoGenerateColumns = false;
spGrid.AllowSorting = true;
spGrid.Sorting += new GridViewSortEventHandler(oGrid_Sorting);
//Firstname
SPBoundField spFName = new SPBoundField();
spFName.DataField = "FirstName";
spFName.SortExpression = "FirstName";
spFName.HeaderText = "First Name";
//Lastname
SPBoundField spLName = new SPBoundField();
spLName.DataField = "LastName";
spLName.SortExpression = "LastName";
spLName.HeaderText = "LastName";
//ManagerName
//Lastname
SPBoundField spMName = new SPBoundField();
spMName.DataField = "ManagerName";
spMName.SortExpression = "ManagerName";
spMName.HeaderText = "Manager Name";
spGrid.DataSource = ds.Tables[0];
spGrid.AutoGenerateColumns = false;
spGrid.Columns.Add(spFName);
spGrid.Columns.Add(spMName);
spGrid.Columns.Add(spLName);
spGrid.DataBind();
this.Controls.Add(spGrid);
}
void oGrid_Sorting(object sender, GridViewSortEventArgs e)
{
string lastExpression = "";
if (ViewState["SortExpression"] != null)
lastExpression = ViewState["SortExpression"].ToString();
string lastDirection = "asc";
if (ViewState["SortDirection"] != null)
lastDirection = ViewState["SortDirection"].ToString();
string newDirection = "asc";
if (e.SortExpression == lastExpression)
newDirection = (lastDirection == "asc") ? "desc" : "asc";
ViewState["SortExpression"] = e.SortExpression;
ViewState["SortDirection"] = newDirection;
spGrid.DataBind();
}
}
}
This is how it looks.. I have undescore for sorting, when clicked on them does not work. I know I am missing some thing.
I want the sorting and Editing bit. How do I do that? Thank you