Ab

Ab

  • NA
  • 1
  • 0

Please Help - problem with SQL connection

Apr 6 2008 1:40 PM

Hi,

I'm still new to C# coding and so I'm having some trouble even maybe with simple things, I have the following code, and I'm getting this particular error :

System.NullReferenceException: Object reference not set to an instance of an object.

Line 26: 	    cmd.CommandType = CommandType.StoredProcedure;
Line 27: 	    cmd.CommandText = "ShowTag";
Line 28:         cmd.Connection.Open();
Line 29:         //con.Open();
Line 30:         DbDataReader rdr = cmd.ExecuteReader();
The following is the code I'm using

using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class sp_test : System.Web.UI.Page

{

SqlConnection con;

SqlCommand cmd = new SqlCommand();

protected void GridShow_Click(object sender, EventArgs e)

{

//ConnectionStringSettings pubs = ConfigurationManager.ConnectionStrings["test"];

//DbConnection connection = new SqlConnection(pubs.ConnectionString);

//DbCommand cmd = connection.CreateCommand();

con = new SqlConnection("Data Source=databaseGeneral;Initial Catalog = test; Integrated Security=True;");

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "ShowTag";

con.Open();

DbDataReader rdr = cmd.ExecuteReader();

DataTable html = new DataTable();

html.Load(rdr, LoadOption.Upsert);

con.Close();

//Label lbl = GetLabel(320, 100);

//lbl.Text = "Command Created";

GridView gv = GetGridView(320, 120);

gv.DataSource = html;

gv.DataBind();

}

private Label GetLabel(int left, int top)

{

Label lbl = new Label();

lbl.Style.Add("position", "absolute");

lbl.Style.Add("left", left.ToString() + "px");

lbl.Style.Add("top", top.ToString() + "px");

lbl.EnableViewState = false;

form1.Controls.Add(lbl);

return lbl;

}

private GridView GetGridView(int left, int top)

{

GridView gv = new GridView();

gv.Style.Add("position", "absolute");

gv.Style.Add("left", left.ToString() + "px" );

gv.Style.Add("top", top.ToString() + "px" );

gv.EnableViewState = false;

form1.Controls.Add(gv);

return gv;

}

}

 

Any help is appreciated

Thanks



Answers (1)