0

Here is my cs file:

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";

        }

I'm new to .net. I created student form using msaccess. And I insert the date into database. Now after submit the input details, I need to display in gridview after submission.

For that i used above code(from online), now it shows,

Selectcommand.connection property not initialized

In the line da.Fill(ds); . I don't know sql server, so i m started using msacces.

May i know, how can i fix this issue?

Thanks,

pcs
  • 1,864
  • 4
  • 25
  • 49

1 Answers1

0

Your code rectified with some other inputs as below

    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";

    }
}