hi friends,
I convert the code in using accessdatasource To sqlserver ADO.net
Access datasource application successfully running.
But
using server side code is got Object Reference error.
I pasted the Both codes,
please find the whats the error in this codes.
/* Access DataSource code */
Dim ds As New Data.DataSet
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0 ;data source=" & Server.MapPath("..\SimpleQSet.mdb") & ";")
Dim dap As New Data.OleDb.OleDbDataAdapter("Select * from Categories", con)
Dim daps As Data.OleDb.OleDbDataAdapter
Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
AccessDataSource1.InsertParameters(0).DefaultValue = TextBox2.Text
AccessDataSource1.InsertParameters(1).DefaultValue = TextBox2.Text
AccessDataSource1.InsertParameters(2).DefaultValue = TextBox3.Text
AccessDataSource1.Insert()
TextBox2.Text = ""
TextBox3.Text = ""
DropDownList2.DataBind()
DropDownList3.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("User") <> Nothing Then
ds.Clear()
dap.Fill(ds)
AccessDataSource1.DataBind()
Else
Response.Redirect("AdminLogin.aspx?err=Login Required")
End If
End Sub
/* Sqlserver ado.net code */
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter dap, daps = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlDataReader dr;
System.Data.DataSet ds = new System.Data.DataSet();
public SqlConnection OpenConnection(SqlConnection c)
{
c.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
if (c.State == ConnectionState.Closed)
{
c.Open();
}
return c;
}
protected void btnadd_Click(object sender, EventArgs e)
{
OpenConnection(con);
cmd.Connection = con;
cmd.CommandText ="insert into Categories values('" + TextBox2.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
cmd.ExecuteNonQuery();
con.Close();
TextBox2.Text = "";
TextBox3.Text = "";
DropDownList2.DataBind();
DropDownList3.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["User"] != null)
{
ds.Clear();
dap.Fill(ds);
//AccessDataSource1.DataBind();
OpenConnection(con);
cmd.Connection = con;
cmd.CommandText = "select * from Categories where cid=' " + DropDownList2.SelectedValue + " '";
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList2.SelectedValue = dr[0].ToString();
}
con.Close();
OpenConnection(con);
cmd.Connection = con;
cmd.CommandText = "select * from Categories where cid=' " + DropDownList3.SelectedValue + " '";
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList3.SelectedValue = dr[0].ToString();
}
con.Close();
}
else
{
Response.Redirect("AdminLogin.aspx?err=Login Required");
}
}
Thanks