Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
GeneralRe: Fairly easy task (I think), but difficult to explain (plus I'm sort of a newbie) Pin
ThisIsMyUserName215-Jun-06 12:12
ThisIsMyUserName215-Jun-06 12:12 
GeneralRe: Fairly easy task (I think), but difficult to explain (plus I'm sort of a newbie) Pin
Alexander Wiseman15-Jun-06 12:22
Alexander Wiseman15-Jun-06 12:22 
GeneralRe: Fairly easy task (I think), but difficult to explain (plus I'm sort of a newbie) Pin
ThisIsMyUserName215-Jun-06 12:39
ThisIsMyUserName215-Jun-06 12:39 
GeneralRe: Fairly easy task (I think), but difficult to explain (plus I'm sort of a newbie) Pin
Alexander Wiseman15-Jun-06 12:47
Alexander Wiseman15-Jun-06 12:47 
GeneralRe: Fairly easy task (I think), but difficult to explain (plus I'm sort of a newbie) Pin
ThisIsMyUserName215-Jun-06 13:21
ThisIsMyUserName215-Jun-06 13:21 
QuestionUsing RPC (with IDL) from C# Pin
BambooMoon15-Jun-06 9:55
BambooMoon15-Jun-06 9:55 
QuestionDataGridView custom Column Pin
Mike0915-Jun-06 8:56
Mike0915-Jun-06 8:56 
Questiondatagrid and databinding (long due to code) Pin
leckey15-Jun-06 8:51
leckey15-Jun-06 8:51 
I'm new to datagrids so if this seems easy my apologies. In my project the user enters a part number in a text box and then clicks a button. Based on what the user inputs the datagrid fills. I first got this to work with some code found from the net and help from users here on CodeProject:
namespace WebBasedPartsDB<br />
{<br />
	/// <summary><br />
	/// Summary description for WebForm2.<br />
	/// </summary><br />
	public class WebForm2 : System.Web.UI.Page<br />
	{<br />
		protected System.Web.UI.WebControls.TextBox txtSearchPart2;<br />
		protected System.Web.UI.WebControls.DataGrid DataGrid2;<br />
		protected System.Web.UI.WebControls.Button btnSearch2;<br />
		protected string strPartNumberInput2;<br />
<br />
		//Get the SQL connection string from the web.config file<br />
		public String strConnectSQL = (ConfigurationSettings.AppSettings["ConnectionString"]);<br />
	<br />
		private void Page_Load(object sender, System.EventArgs e)<br />
		{<br />
			if (!(Page.IsPostBack))<br />
			{<br />
			}<br />
		}<br />
<br />
		#region Web Form Designer generated code<br />
		override protected void OnInit(EventArgs e)<br />
		{<br />
			//<br />
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.<br />
			//<br />
			InitializeComponent();<br />
			base.OnInit(e);<br />
		}<br />
		<br />
		/// <summary><br />
		/// Required method for Designer support - do not modify<br />
		/// the contents of this method with the code editor.<br />
		/// </summary><br />
		private void InitializeComponent()<br />
		{    <br />
			this.btnSearch2.Click += new System.EventHandler(this.btnSearch2_Click);<br />
			this.DataGrid2.SelectedIndexChanged += new System.EventHandler(this.DataGrid2_SelectedIndexChanged);<br />
			this.Load += new System.EventHandler(this.Page_Load);<br />
<br />
		}<br />
		#endregion<br />
<br />
		private void btnSearch2_Click(object sender, System.EventArgs e)<br />
		{<br />
			strPartNumberInput2 = txtSearchPart2.Text;<br />
			string SQLString = "SELECT c.cost, pt.description, dd.DrawingNumber, dd.DrawingRevision, dd.DwgPath FROM costs c INNER JOIN Parts p ON c.PartID = p.Id INNER JOIN PartTypes pt on pt.ID = p.PartTypeID LEFT JOIN DraftingData dd on dd.PartID = p.ID WHERE p.PartNumber = @PartID";<br />
			SqlCommand cmd = new SqlCommand();<br />
			cmd.Connection = new SqlConnection(strConnectSQL);<br />
			cmd.CommandText = SQLString;<br />
			cmd.Parameters.Add ("@PartID", strPartNumberInput2);<br />
			BindGrid(cmd, DataGrid2);<br />
		}<br />
<br />
	<br />
		//**********************************************************<br />
		//BindGrid()<br />
		//**********************************************************<br />
		private void BindGrid (SqlCommand command, System.Web.UI.WebControls.DataGrid DGrid)<br />
		{<br />
			SqlDataAdapter adapter = new SqlDataAdapter(command);<br />
			DataSet ds = new DataSet();<br />
			adapter.Fill(ds);<br />
			DGrid.DataSource = ds;<br />
			DGrid.DataBind();<br />
<br />
		}<br />
<br />
		private void DataGrid2_SelectedIndexChanged(object sender, System.EventArgs e)<br />
		{<br />
		<br />
		}<br />
		public void DataGrid2_edit (object sender, DataGridCommandEventArgs e)<br />
		{<br />
			DataGrid2.EditItemIndex = e.Item.ItemIndex;<br />
			DataGrid2.DataBind();<br />
		}<br />
			<br />
	}<br />
}


Question 1: Since using this "resuable" BindGrid I have to pass two parameters to it. So when I write the DataGrid2_edit (and will also have cancel and update) how do I write these so the BindGrid works properly? Would I have to declare the adapter and command in every function?

Okay, second part...started playing around with a BindData function found here on CodeProject that does not take on any parameters:
using System;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Web;<br />
using System.Web.SessionState;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.HtmlControls;<br />
using System.Data.SqlClient;<br />
using System.Configuration;<br />
<br />
namespace WebBasedPartsDB<br />
{<br />
	/// <summary><br />
	/// Summary description for AddUpdatePart.<br />
	/// </summary><br />
	public class AddUpdatePart : System.Web.UI.Page<br />
	{<br />
		protected System.Web.UI.WebControls.Button btnGetPart;<br />
		protected System.Web.UI.WebControls.DropDownList ddPartType;<br />
		protected System.Web.UI.WebControls.DataGrid dgParts;<br />
		protected string strPartNumberInputReference;<br />
		protected System.Web.UI.WebControls.TextBox txtPartNumberInput;<br />
		public String strConnectSQL = (ConfigurationSettings.AppSettings["ConnectionString"]);<br />
		protected System.Data.SqlClient.SqlConnection sqlConnection1;<br />
	<br />
		private void Page_Load(object sender, System.EventArgs e)<br />
		{<br />
			// Put user code to initialize the page here<br />
		}<br />
		private void BindData()<br />
		{<br />
			strPartNumberInputReference = txtPartNumberInput.Text;<br />
			SqlCommand sqlCmd = new SqlCommand("SELECT c.cost, pt.description, dd.DrawingNumber, dd.DrawingRevision, dd.DwgPath FROM costs c INNER JOIN Parts p ON c.PartID = p.Id INNER JOIN PartTypes pt on pt.ID = p.PartTypeID LEFT JOIN DraftingData dd on dd.PartID = p.ID WHERE p.PartNumber = @PartID", sqlConnection1);<br />
			sqlCmd.CommandType = CommandType.Text;<br />
			sqlCmd.Parameters.Add ("@PartID", strPartNumberInputReference);<br />
<br />
			sqlConnection1.Open();<br />
			dgParts.DataSource = sqlCmd.ExecuteReader();<br />
			dgParts.DataBind();<br />
			sqlConnection1.Close();<br />
			<br />
		}<br />
<br />
		#region Web Form Designer generated code<br />
		override protected void OnInit(EventArgs e)<br />
		{<br />
			//<br />
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.<br />
			//<br />
			InitializeComponent();<br />
			base.OnInit(e);<br />
		}<br />
		<br />
		/// <summary><br />
		/// Required method for Designer support - do not modify<br />
		/// the contents of this method with the code editor.<br />
		/// </summary><br />
		private void InitializeComponent()<br />
		{    <br />
			this.btnGetPart.Click += new System.EventHandler(this.btnGetPart_Click);<br />
			this.Load += new System.EventHandler(this.Page_Load);<br />
<br />
		}<br />
		#endregion<br />
<br />
		private void btnGetPart_Click(object sender, System.EventArgs e)<br />
		{<br />
			<br />
			BindData();<br />
		}<br />
	}<br />
}

But I get the error:System.NullReferenceException: Object reference not set to an instance of an object

So how do I fix one or the other? Is one approach better than the other?
Thanks again everyone for all your help!
AnswerRe: datagrid and databinding (long due to code) Pin
Josh Smith15-Jun-06 9:18
Josh Smith15-Jun-06 9:18 
QuestionSending info to command line Pin
ddpn4215-Jun-06 8:47
ddpn4215-Jun-06 8:47 
AnswerRe: Sending info to command line Pin
Alexander Wiseman15-Jun-06 8:54
Alexander Wiseman15-Jun-06 8:54 
GeneralRe: Sending info to command line Pin
Josh Smith15-Jun-06 9:02
Josh Smith15-Jun-06 9:02 
GeneralRe: Sending info to command line [modified] Pin
ddpn4215-Jun-06 9:21
ddpn4215-Jun-06 9:21 
GeneralRe: Sending info to command line Pin
Alexander Wiseman15-Jun-06 9:25
Alexander Wiseman15-Jun-06 9:25 
GeneralRe: Sending info to command line Pin
ddpn4215-Jun-06 10:02
ddpn4215-Jun-06 10:02 
GeneralRe: Sending info to command line Pin
Dustin Metzgar15-Jun-06 10:15
Dustin Metzgar15-Jun-06 10:15 
AnswerRe: Sending info to command line [modified] Pin
Alexander Wiseman15-Jun-06 10:23
Alexander Wiseman15-Jun-06 10:23 
AnswerRe: Sending info to command line Pin
Dustin Metzgar15-Jun-06 9:01
Dustin Metzgar15-Jun-06 9:01 
GeneralRe: Sending info to command line Pin
Dustin Metzgar15-Jun-06 9:05
Dustin Metzgar15-Jun-06 9:05 
AnswerRe: Sending info to command line Pin
Eric Dahlvang15-Jun-06 10:14
Eric Dahlvang15-Jun-06 10:14 
GeneralRe: Sending info to command line Pin
ddpn4215-Jun-06 10:53
ddpn4215-Jun-06 10:53 
AnswerRe: Sending info to command line [modified] Pin
Eric Dahlvang15-Jun-06 11:16
Eric Dahlvang15-Jun-06 11:16 
GeneralRe: Sending info to command line [modified] Pin
ddpn4216-Jun-06 4:31
ddpn4216-Jun-06 4:31 
QuestionCLR Integration. Pin
PrashantJ15-Jun-06 8:26
PrashantJ15-Jun-06 8:26 
AnswerRe: CLR Integration. Pin
Josh Smith15-Jun-06 8:33
Josh Smith15-Jun-06 8:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.