faramarz_s
چهارشنبه 02 مهر 1382, 10:00 صبح
برای نمایش داده های قابل آپدیت درون خود دیتا گرید مایکروسافت مثال زیر را پیشنهاد داده:
<script runat="server">
SqlConnection myConnection;
protected void Page_Load(Object Src, EventArgs E)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
myConnection = new SqlConnection ("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Determine whether the page is a postback. If it is not a
// postback, call BindGrid.
if (!IsPostBack) BindGrid();
}
// Create an index to the DataGrid row that is clicked and
// call BindGrid.
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
// Cancel resets the index to the row's previous settings.
public void MyDataGrid_Cancel(Object sender,
DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
// When the Update link is clicked, build a SQL UPDATE command,
// connect to the database, update the row's information in the
// database, and rebind the DataGrid to show the updated information.
public void MyDataGrid_Update(Object sender,
DataGridCommandEventArgs E)
{
String updateCmd = "UPDATE Authors SET au_id = @Id," +
" au_lname = @LName, au_fname = @FName, phone = @Phone," +
" address = @Address, city = @City, state = @State," +
" zip = @Zip, contract = @Contract WHERE au_id = @Id;";
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@LName",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@FName",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Phone",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Address",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@City",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@State",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Zip",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Contract",
SqlDbType.VarChar));
// Initialize the SqlCommand "@Id" parameter to the ID of the
// row that must be clicked.
myCommand.Parameters["@Id"].Value =
MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
// Create an array of column names.
String[] cols = {"@Id","@LName","@FName","@Phone",
"@Address", "@City","@State","@Zip","@Contract"};
// Iterate through the columns, checking for empty values.
// Skip the first, second, and last columns. If an empty value
// is found, display an error message box. Also initialize the
// SqlCommand parameter values.
int numCols = E.Item.Cells.Count;
for (int i=2; i<numCols-1; i++)
{
String colvalue = ((TextBox)E.Item.Cells[i].Controls[0]).Text;
if (i<6 && colvalue == "")
{
Message.InnerHtml = "ERROR: Null values not allowed for" +
" Author ID, Name or Phone";
Message.Style["color"] = "red";
return;
}
myCommand.Parameters[cols[i-1]].Value = colvalue;
}
// Append the last field, converting true/false values to 0/1.
if (String.Compare(((TextBox)E.Item.Cells
[numCols-1].Controls[0]).Text, "true", true)==0)
myCommand.Parameters["@Contract"].Value = "1";
else
myCommand.Parameters["@Contract"].Value = "0";
// Connect to the database and update the information.
myCommand.Connection.Open();
// Test whether the data was updated and display the
//appropriate message to the user.
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated.</b><br>";
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists" +
" with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record," +
" please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}
// Close the connection.
myCommand.Connection.Close();
// Show the updated information.
BindGrid();
}
// The BindGrid procedure connects to the database and implements
// a SQL SELECT query to get all data in the "Authors" table.
public void BindGrid()
{
SqlConnection myConnection = new SqlConnection(
"server=localhost;database=pubs;Trusted_Connection= Yes");
SqlDataAdapter myCommand = new SqlDataAdapter(
"SELECT * FROM authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();
}
نکته ای که هست عدم نمایش دیتا گرید بدون هیچ پیغام خطا!
کجای کار ایراد دارد؟ :?:
<script runat="server">
SqlConnection myConnection;
protected void Page_Load(Object Src, EventArgs E)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
myConnection = new SqlConnection ("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Determine whether the page is a postback. If it is not a
// postback, call BindGrid.
if (!IsPostBack) BindGrid();
}
// Create an index to the DataGrid row that is clicked and
// call BindGrid.
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
// Cancel resets the index to the row's previous settings.
public void MyDataGrid_Cancel(Object sender,
DataGridCommandEventArgs E)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
// When the Update link is clicked, build a SQL UPDATE command,
// connect to the database, update the row's information in the
// database, and rebind the DataGrid to show the updated information.
public void MyDataGrid_Update(Object sender,
DataGridCommandEventArgs E)
{
String updateCmd = "UPDATE Authors SET au_id = @Id," +
" au_lname = @LName, au_fname = @FName, phone = @Phone," +
" address = @Address, city = @City, state = @State," +
" zip = @Zip, contract = @Contract WHERE au_id = @Id;";
SqlCommand myCommand = new SqlCommand(updateCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@LName",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@FName",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Phone",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Address",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@City",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@State",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Zip",
SqlDbType.VarChar));
myCommand.Parameters.Add(new SqlParameter("@Contract",
SqlDbType.VarChar));
// Initialize the SqlCommand "@Id" parameter to the ID of the
// row that must be clicked.
myCommand.Parameters["@Id"].Value =
MyDataGrid.DataKeys[(int)E.Item.ItemIndex];
// Create an array of column names.
String[] cols = {"@Id","@LName","@FName","@Phone",
"@Address", "@City","@State","@Zip","@Contract"};
// Iterate through the columns, checking for empty values.
// Skip the first, second, and last columns. If an empty value
// is found, display an error message box. Also initialize the
// SqlCommand parameter values.
int numCols = E.Item.Cells.Count;
for (int i=2; i<numCols-1; i++)
{
String colvalue = ((TextBox)E.Item.Cells[i].Controls[0]).Text;
if (i<6 && colvalue == "")
{
Message.InnerHtml = "ERROR: Null values not allowed for" +
" Author ID, Name or Phone";
Message.Style["color"] = "red";
return;
}
myCommand.Parameters[cols[i-1]].Value = colvalue;
}
// Append the last field, converting true/false values to 0/1.
if (String.Compare(((TextBox)E.Item.Cells
[numCols-1].Controls[0]).Text, "true", true)==0)
myCommand.Parameters["@Contract"].Value = "1";
else
myCommand.Parameters["@Contract"].Value = "0";
// Connect to the database and update the information.
myCommand.Connection.Open();
// Test whether the data was updated and display the
//appropriate message to the user.
try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated.</b><br>";
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists" +
" with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record," +
" please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}
// Close the connection.
myCommand.Connection.Close();
// Show the updated information.
BindGrid();
}
// The BindGrid procedure connects to the database and implements
// a SQL SELECT query to get all data in the "Authors" table.
public void BindGrid()
{
SqlConnection myConnection = new SqlConnection(
"server=localhost;database=pubs;Trusted_Connection= Yes");
SqlDataAdapter myCommand = new SqlDataAdapter(
"SELECT * FROM authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();
}
نکته ای که هست عدم نمایش دیتا گرید بدون هیچ پیغام خطا!
کجای کار ایراد دارد؟ :?: