Код: Выделить всё
Default.aspx
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string connStr =
@"Data Source=db011;" +
"Initial Catalog=***DB;" +
"User id=***;" +
"Password=***;";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGrid();
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [dbo].[Modus_FTE]", con);
DataTable dt = new DataTable();
da.Fill(dt);
CustomersGridView.DataSource = dt;
CustomersGridView.DataBind();
}
protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
CustomersGridView.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
CustomersGridView.EditIndex = -1;
BindGrid();
}
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(CustomersGridView.DataKeys[e.RowIndex].Value);
string name = ((TextBox)CustomersGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string loc = ((TextBox)CustomersGridView.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string hired = ((TextBox)CustomersGridView.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
SqlConnection con = new SqlConnection(connStr);
string query = "UPDATE Users SET Name=@Name, Loc=@Loc, Hired=@Hired WHERE FteID=@id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Loc", loc);
cmd.Parameters.AddWithValue("@Hired", hired);
con.Open();
cmd.ExecuteNonQuery();
CustomersGridView.EditIndex = -1;
BindGrid();
}
protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(CustomersGridView.DataKeys[e.RowIndex].Value);
using (SqlConnection con = new SqlConnection(connStr))
{
string query = "DELETE FROM Users WHERE FteID=@id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@FteID", id);
con.Open();
cmd.ExecuteNonQuery();
}
BindGrid();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(connStr))
{
string query = "INSERT INTO [dbo].[Modus_FTE] (FteID, Name, Loc, Hired) VALUES (@FteID, @Name, @Loc, @Hired)";
SqlCommand cmd = new SqlCommand(query, con);
var randOne = new Random((int)DateTime.UtcNow.Ticks); //random number based on ticks in this instance of utc time for testing purpose
DateTime thisDay = DateTime.Today;
cmd.Parameters.AddWithValue("@FteID", randOne);
cmd.Parameters.AddWithValue("@Name", "New User" + randOne.ToString());
cmd.Parameters.AddWithValue("@Loc", "new"+randOne.ToString()+"@example.com");
cmd.Parameters.AddWithValue("@Hired", thisDay.ToString("g"));
con.Open();
cmd.ExecuteNonQuery();
}
BindGrid();
}
}
Код: Выделить всё
SELECT TOP (1000) [IsReviewItem]
,[FteID]
,[Name]
,[Loc]
,[Hired]
,[Termed]
,[Department]
,[Roles]
,[Review]
,[RefId]
,[PersonTitle]
,[IsFteWhole]
,[IsActive]
,[ProdYear]
FROM [ModusDB_Dev].[dbo].[Modus_FTE]
Ошибка:
Имя «CustomersGridView» не существует в текущем контексте в Default.aspx.cs
(везде в Default.aspx.cs я использую CustomersGridView)