[aspx.cs]
[WebMethod]
public static object GetTreeGridData()
{
List<TaskData> list = new List<TaskData>();
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);//connectionString
con.Open();
using (con)
{
using (var command = con.CreateCommand())
{
command.CommandText = "SELECT Id,Name,StartDate,Duration,ParentId FROM treedata";
using (var reader = command.ExecuteReader())
{
var indexOfCol1 = reader.GetOrdinal("Id");
var indexOfCol2 = reader.GetOrdinal("Name");
var indexOfCol3 = reader.GetOrdinal("StartDate");
var indexOfCol4 = reader.GetOrdinal("Duration");
var indexOfCol5 = reader.GetOrdinal("ParentId");
while (reader.Read())
{
TaskData obj = new TaskData();
obj.Id = reader.GetValue(indexOfCol1).ToString();
obj.Name = reader.GetValue(indexOfCol2).ToString();
obj.StartDate = reader.GetValue(indexOfCol3).ToString();
obj.Duration = reader.GetValue(indexOfCol4).ToString();
obj.ParentId = (reader.GetValue(indexOfCol5).ToString() == "") ? null : reader.GetValue(indexOfCol5).ToString();
list.Add(obj);
}
reader.Close();
}
}
con.Close();
}
return list;
}
[WebMethod]
public void Update(TaskData Task)
{
string IDNumber = Task.Id;
string cmdString = "UPDATE treedata SET Id=@Id, Name=@Name,StartDate=@StartDate,Duration=@Duration WHERE Id = '" + IDNumber + "'";
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);//connectionString
con.Open();
using (SqlCommand sqlCommand = new SqlCommand(cmdString, con))
{
sqlCommand.Parameters.AddWithValue("@Name", Task.Name);
sqlCommand.Parameters.AddWithValue("@Id", Task.Id);
sqlCommand.Parameters.AddWithValue("@StartDate", Task.StartDate);
sqlCommand.Parameters.AddWithValue("@Duration", Task.Duration);
sqlCommand.ExecuteNonQuery();
}
con.Close();
}
public void Add(TaskData Task)
{
string cmdString = "INSERT INTO treedata ([Name],[Id],[StartDate],[ParentId],[Duration])" + "VALUES(@Name,@Id,@StartDate,@ParentId,@Duration)";
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);//connectionString
con.Open();
using (SqlCommand sqlCommand = new SqlCommand(cmdString, con))
{
sqlCommand.Parameters.AddWithValue("@Name", Task.Name);
sqlCommand.Parameters.AddWithValue("@StartDate",Task.StartDate);
sqlCommand.Parameters.AddWithValue("@Duration", Task.Duration);
sqlCommand.Parameters.AddWithValue("@Id", Task.Id);
if (Task.ParentId == null)
{
sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId).Value = "";
}
else
{
sqlCommand.Parameters.AddWithValue("@ParentId", Task.ParentId);
}
int test = sqlCommand.ExecuteNonQuery();
}
con.Close();
}
public void Delete(TaskData Task)
{
string IDNumber = Task.Id;
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);//connectionString
con.Open();
SqlCommand cmd = new SqlCommand("delete from treedata where Id = '" + IDNumber + "'", con);
int result = cmd.ExecuteNonQuery();
con.Close();
} |