在c#中使用msaccess提交后,在gridview中显示数据时出现问题

本文关键字:数据 显示 问题 gridview msaccess 提交 | 更新日期: 2023-09-27 18:19:32

这是我的cs文件:

protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Visible = false;
            OleDbCommand com = new OleDbCommand(connectionstr);
            com.Parameters.AddWithValue("@Action", HiddenField2.Value).ToString();
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(com);
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        protected void btnsub_Click(object sender, EventArgs e) {
            OleDbConnection con =Connection.DBconnection();
            OleDbCommand com =new OleDbCommand("Insert into registration(username,class,section,address)values(@username,@class,@section,@address)",con);
            com.Parameters.AddWithValue("@Action", HiddenField1.Value).ToString();
            com.Parameters.AddWithValue("@username",Textusername.Text.Trim());
            com.Parameters.AddWithValue("@pwd", Textclass.Text.Trim());
            com.Parameters.AddWithValue("@email",Textsection.Text.Trim());
            com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
            com.ExecuteNonQuery();
            Label1.Visible = true;
            Label1.Text = "Records are Submmited Successfully";
        }

我是.net的新手。我使用msaccess创建了学生表格。然后我把日期插入数据库。现在,在提交了输入的详细信息后,我需要在提交后显示在gridview中。

为此,我使用了上面的代码(来自在线),现在它显示,

Selectcommand.connection属性未初始化

在CCD_ 1。我不知道sql server,所以我开始使用msacces。

我可以知道,我该如何解决这个问题吗?

谢谢,

在c#中使用msaccess提交后,在gridview中显示数据时出现问题

您的代码使用以下中的一些其他输入进行了更正

    String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='c:''temp''Database11.mdb'";
    protected void Page_Load(object sender, EventArgs e)
    {
        OleDbConnection Conn = new OleDbConnection(strConn);
        Label1.Visible = false;
        OleDbCommand com = new OleDbCommand("Select [username],[class],[section],[address] from registration", Conn);
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(com);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void btnsub_Click(object sender, EventArgs e)
    {
        OleDbConnection Conn = new OleDbConnection(strConn);
        OleDbCommand com = new OleDbCommand("Insert into registration([username],[class],[section],[address])values(@username,@class,@section,@address)", Conn);
        com.Parameters.AddWithValue("@username", Textusername.Text.Trim());
        com.Parameters.AddWithValue("@class", Textclass.Text.Trim());
        com.Parameters.AddWithValue("@section", Textsection.Text.Trim());
        com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
        Conn.Open();
        com.ExecuteNonQuery();
        Conn.Close();
        Label1.Visible = true;
        Label1.Text = "Records are Submmited Successfully";
    }
}