我正在编写一个必须具备以下功能的应用程序:
我用谷歌搜索了很长时间。
我在普通的C#windows窗体应用程序中编写了一个类似的应用程序,我使用OLEdb来做所有事情并且工作正常,它不仅限于一个.mdb文件。
在C#Windows窗体应用程序中,我使用了Gridview控件。 由于Web应用程序没有Gridview,我使用了数据网格。
到目前为止,这是代码:
public partial class _Default : System.Web.UI.Page
{
string databasePath = "____________.mdb";
DataTable userTable = new DataTable();
OleDbDataAdapter adapter;
OleDbConnection connection;
OleDbCommand command;
OleDbCommandBuilder builder;
DataSet ds;
DataSet tempDs;
public string DatabaseName = null;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
protected void Page_Load(object sender, EventArgs e)
{
tbDatabasePath.Text = databasePath;
}
protected void btnLoadData_Click(object sender, EventArgs e)
{
ReadRecords();
}
protected void btnGetTables_Click(object sender, EventArgs e)
{
try
{
ddTables.Items.Clear();
ddTables.Text = null;
getTables();
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not retrieve the Tables');", true);
}
}
private void getTables()
{
string tempstring = "";
string getTConnection = connectionString + databasePath; ;
OleDbConnection myConnection = new OleDbConnection(getTConnection);
myConnection.Open();
DataTable datT = myConnection.GetSchema("Tables");
for (int i = 0; i < datT.Rows.Count; i++)
{
tempstring = datT.Rows[i][2].ToString();
if (tempstring.Contains("MSys"))
{
}
else
{
ddTables.Items.Add(datT.Rows[i][2].ToString());
}
}
myConnection.Close();
}
private void ReadRecords()
{
datagrid.DataSource = null;
OleDbDataReader reader = null;
try
{
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + databasePath);
OleDbCommand cmd = new OleDbCommand("Select * FROM " + ddTables.Text, connection);
adapter = new OleDbDataAdapter(cmd);
builder = new OleDbCommandBuilder(adapter);
ds = new DataSet("MainDataSet");
tempDs = new DataSet("TempDataSet");
connection.Open();
adapter.Fill(tempDs);
tempDs.Clear();
tempDs.Dispose();
adapter.Fill(ds, ddTables.Text);
userTable = ds.Tables[ddTables.Text];
}
catch
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not load the data');", true);
}
finally
{
if (reader != null) reader.Close();
//if (connection != null) connection.Close();
datagrid.ShowFooter = true;
}
CreateTempTable(0, 10);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('DONE');", true);
}
private void ReadRecords2()
{
// datagrid.DataSource = null;
OleDbDataReader reader = null;
try
{
connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + databasePath);
OleDbCommand cmd = new OleDbCommand("Select * FROM " + ddTables.Text, connection);
adapter = new OleDbDataAdapter(cmd);
builder = new OleDbCommandBuilder(adapter);
ds = new DataSet("MainDataSet");
tempDs = new DataSet("TempDataSet");
connection.Open();
adapter.Fill(tempDs);
tempDs.Clear();
tempDs.Dispose();
adapter.Fill(ds, ddTables.Text);
userTable = ds.Tables[ddTables.Text];
}
catch
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not load the data');", true);
}
finally
{
if (reader != null) reader.Close();
//if (connection != null) connection.Close();
}
CreateTempTable(0, 10);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('DONE');", true);
}
private void CreateTempTable(int startRecord, int noOfRecords)
{
try
{
userTable.Rows.Clear();
adapter.Fill(ds, "");
userTable = ds.Tables[""];
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Could not load the data in CreateTempTable');", true);
}
datagrid.DataSource = userTable.DefaultView;
datagrid.DataBind();
}
protected void datagrid_RowEditing(object sender, GridViewEditEventArgs e)
{
datagrid.EditIndex = e.NewEditIndex;
ReadRecords();
}
protected void datagrid_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void datagrid_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
adapter.Update(userTable);
}
protected void datagrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
adapter.Update(userTable);
ReadRecords();
}
protected void datagrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
datagrid.EditIndex = -1;
ReadRecords();
}
protected void datagrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowToDelete = e.RowIndex;
datagrid.DeleteRow(rowToDelete);
adapter.Update(userTable);
ReadRecords();
}
}
}
它可以:
现在,当我单击编辑按钮时,它会使所选行可编辑。 但是,当我单击更新按钮时,我得到“对象引用未设置为对象的实例”。 代码在Forms应用程序中工作..
有没有任何方法适配器.Update(userTable);将能够在表单应用程序中使用它吗?
任何人都可以帮我看看我的问题。如果我只能将更改保存回.mdb文件,我会非常高兴。
答案 0 :(得分:0)
私有变量的行为在Web应用程序与WinForms中是不同的。
简而言之:
您应该在adapter
方法中初始化变量(特别是Page_Load
)。然后它将不为null,您可以使用它。
注意:您还需要存储数据集中所做的更改... 我建议再多阅读一下它是如何工作的...... 例如,您可以使用Session对象,或者更好的是,请查看ViewState
了解ASP.NET视图状态: http://msdn.microsoft.com/en-us/library/ms972976.aspx
的IsPostBack: http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx