database query populate combo box

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 19790 Views - Last Post: 30 July 2009 - 12:22 PM Rate Topic: -----

#16 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 29 July 2009 - 11:54 AM

View Posteclipsed4utoo, on 29 Jul, 2009 - 10:44 AM, said:

let's try...

using System.Web.UI.WebControls;




that got it but unfortuinately i'm back to where i started with a blank combo box... is there a way to tell if the data is getting into the dataset?
Was This Post Helpful? 0
  • +
  • -

#17 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: database query populate combo box

Posted 29 July 2009 - 11:58 AM

You can see if anything being returned by using

MessageBox.Show("Returned Rows: " + myDataset.Tables["FunctionalGroupControl_tb"].Rows.Count.ToString());



Right after you fill the DataSet.
Was This Post Helpful? 0
  • +
  • -

#18 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 29 July 2009 - 12:07 PM

now on the line cmbopartner.Items.Add(li); I am getting NullReferenceException was unhandlded object reference not set to an instance of an object...

This post has been edited by codeman0013: 29 July 2009 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#19 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 30 July 2009 - 06:40 AM

View Postcodeman0013, on 29 Jul, 2009 - 11:07 AM, said:

now on the line cmbopartner.Items.Add(li); I am getting NullReferenceException was unhandlded object reference not set to an instance of an object...



any suggestions?
Was This Post Helpful? 0
  • +
  • -

#20 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: database query populate combo box

Posted 30 July 2009 - 11:27 AM

put a breakpoint at that line, then hover over "li" and see if it's null. If it is, check to make sure "row["partnerkey"]" is a valid column in the datatable. You can use the Immediate window to check this. Just copy and paste "row["partnerkey"]" into the Immediate Window and hit enter(once your breakpoint has been hit).

If you don't have an Immediate window showing, go to the Command Window and type "immed" then hit enter.
Was This Post Helpful? 0
  • +
  • -

#21 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 30 July 2009 - 11:37 AM

View Posteclipsed4utoo, on 30 Jul, 2009 - 10:27 AM, said:

put a breakpoint at that line, then hover over "li" and see if it's null. If it is, check to make sure "row["partnerkey"]" is a valid column in the datatable. You can use the Immediate window to check this. Just copy and paste "row["partnerkey"]" into the Immediate Window and hit enter(once your breakpoint has been hit).

If you don't have an Immediate window showing, go to the Command Window and type "immed" then hit enter.



li has info in it on that line cmbopartner says null when i hover over it though?
Was This Post Helpful? 0
  • +
  • -

#22 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: database query populate combo box

Posted 30 July 2009 - 11:43 AM

so your combobox is null? thats weird.

did you post all of the code that you have?

This post has been edited by eclipsed4utoo: 30 July 2009 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#23 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 30 July 2009 - 11:46 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.Web;
using System.Web.UI.WebControls; 

namespace EDI_Updater
{
		
	public partial class frmmain : Form
	{
		public OleDbConnection myConn;
		public OleDbDataAdapter myDataAdapter;
		public string strpartner;
	  
	   
	
		public frmmain() 
		{
			CreateSqlConnection();

		}

		private void CreateSqlConnection()
		{
		  
		try 
			{
				//open the connection to the database
				myConn = new OleDbConnection();
				myConn.ConnectionString = "Persist Security Info=False;Integrated Security=SSPI;Provider = SQLOLEDB;database=gentranNT5;server=decsql02;Connect Timeout=30";
				myConn.Open();			
			}
			catch(OleDbException ex) 
			{
				MessageBox.Show("Error in connection ..."+ex.Message);
			}

		   string sqlStr = "select distinct PartnerKEY from FunctionalGroupControl_tb";
			
			//Instantiate a DataAdapter by passing the sqlStr and Connection.
			//now data in raw form
			myDataAdapter = new OleDbDataAdapter(sqlStr,myConn);

			//Instantiate a DataSet
			DataSet myDataset = new DataSet();
			
			// Populate the data table "student"
			myDataset.Clear();
			myDataAdapter.Fill(myDataset,"FunctionalGroupControl_tb");
			//MessageBox.Show("Returned Rows: " + myDataset.Tables["FunctionalGroupControl_tb"].Rows.Count.ToString());


			
			foreach (DataRow row in myDataset.Tables["FunctionalGroupControl_tb"].Rows)
			{
				
				ListItem li = new ListItem(row["PartnerKEY"].ToString());
				cmbopartner.Items.Add(li);	 
			  
			}
			  
			// Close the connection to the database.
			myConn.Close();

		}///////////////////////////////////////////////
	

		private void btnexit_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void frmmain_Load(object sender, EventArgs e)
		{
			// TODO: This line of code loads data into the 'gentranNT5DataSet.FunctionalGroupControl_tb' table. You can move, or remove it, as needed.
			this.functionalGroupControl_tbTableAdapter.Fill(this.gentranNT5DataSet.FunctionalGroupControl_tb);

		}

	   
	}
}



thats literally a select all and copy paste into here its really baffeling its there but it says its null and wont let me redefine or anything..
Was This Post Helpful? 0
  • +
  • -

#24 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: database query populate combo box

Posted 30 July 2009 - 11:49 AM

ok....now that code is different from what you posted just a few posts up. Your missing the "InitializeComponents" method that is normally in the default constructor. This method is what defines all of the controls on the form.

So you need to do this...

public frmmain() 
{
      InitializeComponent();
      CreateSqlConnection();
}


Was This Post Helpful? 0
  • +
  • -

#25 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 30 July 2009 - 11:53 AM

WOW you got it!!! 2 days of me staring at code becuase of one stupid line lol...

now on to the hard part... requerying the db based on the selected item lol

thanks again and if you have any suggestions on how to change the text box with a value let me know :-)
Was This Post Helpful? 0
  • +
  • -

#26 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: database query populate combo box

Posted 30 July 2009 - 11:57 AM

View Postcodeman0013, on 30 Jul, 2009 - 02:53 PM, said:

WOW you got it!!! 2 days of me staring at code becuase of one stupid line lol...

now on to the hard part... requerying the db based on the selected item lol

thanks again and if you have any suggestions on how to change the text box with a value let me know :-)


I've got burned by that one line before...and it has stuck in my head to always check that when modifying/creating constructors for a form.

as for going to the database when the selection changes, you can use the SelectedIndexChanged event of the combo box. Then use the "Text" property of the combo box to get what they selected. Then query with that.
Was This Post Helpful? 0
  • +
  • -

#27 codeman0013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 28-July 09

Re: database query populate combo box

Posted 30 July 2009 - 12:22 PM

View Posteclipsed4utoo, on 30 Jul, 2009 - 10:57 AM, said:

View Postcodeman0013, on 30 Jul, 2009 - 02:53 PM, said:

WOW you got it!!! 2 days of me staring at code becuase of one stupid line lol...

now on to the hard part... requerying the db based on the selected item lol

thanks again and if you have any suggestions on how to change the text box with a value let me know :-)


I've got burned by that one line before...and it has stuck in my head to always check that when modifying/creating constructors for a form.

as for going to the database when the selection changes, you can use the SelectedIndexChanged event of the combo box. Then use the "Text" property of the combo box to get what they selected. Then query with that.



ok becuase i'm usign the same table i just need to take another field and put it in the combo box when they select it
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2