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.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Order : Form
    {

        public int count = 0;
        public string Conn = WindowsFormsApplication1.conn.getConnection();
        public string getid;
        public SqlDataAdapter dAdapter, dAdapter1;
        public DataSet dSet, dSet1;
        public SqlDataReader reader, reader1, reader2, myReader;
        public int id, i;
        public string mode;

        public Order()
        {
            InitializeComponent();
        }

         private void Order_Load(object sender, EventArgs e)
        {
            SqlConnection myConnection_Company = new SqlConnection(Conn);
            myConnection_Company.Open();
            string load_company = "SELECT Customer_ID, Company_Name, Email FROM tbl_customer";
            SqlCommand cmd_company = new SqlCommand(load_company, myConnection_Company);
            SqlDataAdapter adapter_company = new SqlDataAdapter();
            DataSet ds_company = new DataSet();
            adapter_company.SelectCommand = cmd_company;
            adapter_company.Fill(ds_company);
            tb_Company.DataSource = ds_company.Tables[0];
            tb_Company.ValueMember = "Customer_ID";
            tb_Company.DisplayMember = "Company_Name";
            tb_email.Text = ds_company.Tables["tbl_Customer"].Rows[0]["Email"].ToString();
            reader = cmd_company.ExecuteReader();
            myConnection_Company.Close();
        }

        private void tb_Company_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection myConnection_Company = new SqlConnection(Conn);
            myConnection_Company.Open();
            SqlDataAdapter adapter_company = new SqlDataAdapter("select * from tbl_Customer where Customer_ID='" + tb_Company.SelectedItem.ToString() + "'", myConnection_Company);
            DataSet ds_company = new DataSet();
            adapter_company.Fill(ds_company);

            if (ds_company.Tables["tbl_Customer"].Rows.Count > 0)
            {
                tb_email.Text = ds_company.Tables["tbl_Customer"].Rows[0]["Email"].ToString();
            }
            else
            {
                tb_email.Text = "";
            }
            myConnection_Company.Close();
        }
    }
}

im also having this error message
---- object reference is not set to an instance
when i run this code

any help is very much appreciated :) thanks guys...

Recommended Answers

All 4 Replies

try textbox1.text=combobox.selecteditem.text

sorry had little brainfrt, it should be textBox1.Text = comboBox1.SelectedItem.ToString();

object reference is not set to an instance

I think this is referring to reader = cmd_company.ExecuteReader();, you probably have to initialize it with the new keyword.

as tinstaafl said object reference is not set to an instance may occur if you create any instance with out new keyword. but this error normally comes when we get null value and assign it to some variable ,object etc. It is always better to convert null into empty string. or you can use

if(ds_company.Tables["tbl_Customer"].Rows[0] != null)
{
//your code --- 
}

Regards

or you can do textBox1.Text = comboBox1.SelectedText; if your comboBox displays strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.