AccessDataSource to Sql server -object referance error

Posted by Dn2010 under ASP.NET on 2/7/2011 | Points: 10 | Views : 1996 | Status : [Member] | Replies : 3
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




Responses

Posted by: Karthikanbarasan on: 2/8/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

In which point you getting the object reference error?

Thanks
Karthik
www.f5Debug.net

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 2/9/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Also please post the error log.

Thanks

Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Madhu.b.rokkam on: 2/10/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

While opening the connection you must be getting an error

OpenConnection(con); wont be suffice and u need to either use the ref keyword while passing the con object or else as per your code you need to change the code to capture the created connection in the above method.

Regards
Madhu



Thanks and Regards
Madhu

Dn2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response