private void bind1()
{
con.Open();
da = new SqlDataAdapter("Select NationalityID,NationalityName from Nationality", con);
dt = new DataTable();
da.Fill(dt);
comboBox2.DisplayMember = "NationalityName";
comboBox2.ValueMember = "NationalityID";
comboBox2.DataSource = dt;
comboBox2.Refresh();
con.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
bind1();
}
this code above work fine and retrieve data in combobox by load event but i need to work in selected index changed event combobox
Why not retrieve data in selected index changed event of combobox
and how i do that
ahmed saPosted Feb 11, 2014, 1:14 PM
ahmed saPosted Feb 11, 2014, 1:09 PM
1-retrieve value of every row in combobox done from below code success
2- then display all items in combobox in selectedindexchanged event(not done)
3-then select item i need to edit or update (not done)
Drive1view is as following
SELECT dbo.Nationality.NationalityName, dbo.Driver.DriverName, dbo.Driver.DriverID, dbo.Driver.Nationality, dbo.Driver.Address, dbo.Driver.NationalityID,
dbo.Driver.ValueCompany
FROM dbo.Driver INNER JOIN
dbo.Nationality ON dbo.Driver.NationalityID = dbo.Nationality.NationalityID
code as bellow
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FleetManagment
{
public partial class Driver : Form
{
string value1;
string value2;
string value3;
string value4;
private int currrecord=0;
private int totalrecord=0;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection con = new SqlConnection();
public Driver(string str1,string str2,string str3,string str4)
{
InitializeComponent();
value1 = str1;
value2 = str2;
value3 = str3;
value4 = str4;
}
private void fillcontrol()
{
textBox1.Text = dt.Rows[currrecord]["DriverID"].ToString();
textBox2.Text = dt.Rows[currrecord]["DriverName"].ToString();
textBox3.Text = dt.Rows[currrecord]["Nationality"].ToString();
textBox4.Text = dt.Rows[currrecord]["Address"].ToString();
comboBox1.Text = dt.Rows[currrecord]["NationalityName"].ToString();
}
private void Driver_Load(object sender, EventArgs e)
{
string constr = "Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "";
con = new SqlConnection(constr);
string comdstr = "select * from Driver1view";
da = new SqlDataAdapter(comdstr, constr);
ds = new DataSet();
da.Fill(ds, "prog");
dt = new DataTable();
dt = ds.Tables["prog"];
totalrecord = dt.Rows.Count;
if (ds != null)
{
dt = ds.Tables[0];
currrecord = 0;
}
fillcontrol();
}
combobox1_selectindexchanged event()
{
bind1();
then update
}
private void Next_Click(object sender, EventArgs e)
{
if (currrecord != totalrecord - 1)
{
currrecord++;
fillcontrol();
}
else
{
//MessageBox.Show("No more");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
FleetManagment.Fleet fleat = new FleetManagment.Fleet();
string max = fleat.Max_Driver("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "");
textBox1.Text = max;
FleetManagment.Fleet fleat1 = new FleetManagment.Fleet();
fleat1.InsertDriver("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "", Convert.ToInt32(max));
fillcontrol();
string constr = "Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "";
con = new SqlConnection(constr);
string comdstr = "select * from Driver1view";
da = new SqlDataAdapter(comdstr, constr);
ds = new DataSet();
da.Fill(ds, "prog");
dt = new DataTable();
dt = ds.Tables["prog"];
if (ds != null)
{
dt = ds.Tables[0];
totalrecord = dt.Rows.Count;
currrecord = totalrecord - 1;
}
fillcontrol();
}
}
}
}
My problem after i get the record value into combobox1
VulpesPosted Feb 11, 2014, 10:31 AM
A couple of points on this:
1. I'm surprised that the code works at all as you're creating the connection object after you've filled the datatable and assigning it the local variable 'con'. I imagine that you must have another variable called 'con' declared at form level which is initialized somewhere else.
2. If you rebind to the datatable in the SelectedIndexChanged eventhandler, then it may cause the handler to be called again and so on 'ad infinitum' until the stack overflows. It's not therefore a good place to put your bind method.
Can you explain what you're trying to achieve here?
ahmed saPosted Feb 11, 2014, 9:21 AM
ahmed saPosted Feb 11, 2014, 9:20 AM
Hardik BhavsarPosted Feb 11, 2014, 8:27 AM
{
if(!ispostback)
{
bind1();
}
}
private void bind1()
{
con.Open();
da = new SqlDataAdapter("Select NationalityID,NationalityName from Nationality", con);
dt = new DataTable();
da.Fill(dt);
SqlConnection con = new SqlConnection("Data Source=" + value1 + ";Initial Catalog=" + value2 + ";User ID=" + value3 + ";Password=" + value4 + "");// DataRow dr;
dr = dt.NewRow();
dr.ItemArray = new object[] { 0, "---Select Nationality---" };
dt.Rows.InsertAt(dr, 0);comboBox2.DisplayMember = "NationalityName";
comboBox2.ValueMember = "NationalityID";
comboBox2.DataSource = dt;
comboBox2.Refresh();
con.Close();
}
Accept Answer is True