Hi my name is Vishal. For the past 2 days, I have been breaking my head on how to have a trail of four zeros proceeding a field name from a table in display member property of combobox in c# windows forms with sql server 2008?
I have a form named:
frmDialyzer and in it I populate my combobox named:
cboPatientID.
Given below is c# code on how I populate my combobox(
cboPatientID) from tables named:
patient_id and
patient_name in sql server2008.:
namespace DRRS_CSharp
{
public partial class frmDialyzer : Form
{
int pUserID;
public frmDialyzer()
{
InitializeComponent();
string Sql = "Select p.patient_id as patient_id,(n.patient_first_name+' '+n.patient_last_name+'| '+convert(varchar,n.patient_id)) as Name from patient_id p,patient_name n where n.patient_id=p.patient_id and n.status=1 and not exists(Select * from dialyser where dialyser.deleted_status=0 and dialyser.closed_status=0 and dialyser.patient_id=p.patient_id)";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(Sql, conn);
dt.Load(cmd.ExecuteReader());
cboPatientID.DataSource = dt;
cboPatientID.ValueMember = "patient_id";
cboPatientID.DisplayMember = "Name";
cboPatientID.SelectedValue = 0;
}
Since i am using
SelectedValue property of
cboPatientID in terms of selecting item in combobox(cboPatientID) in
frmDialyzer. I have also enabled
DropDownStyle property of
cboPatientID to
DropDownList in
frmDialyzer.
Given below is structure of tables in sql server2008:
table name:
patient_id
ColumnName DataType AllowNulls
patient_id(primary key) Int No(since auto-increment)
patient_sex nvarchar(15) Yes
patient_dob date Yes
row_upd_date datetime Yes
table name:
patient_name
ColumnName DataType AllowNulls
patient_id int Yes
patient_first_name nvarchar(50) Yes
patient_middle_name nvarchar(50) Yes
patient_last_name nvarchar(50) Yes
status bit Yes
row_upd_date datetime Yes
As you can see in below c# code of
frmDialyzer:
namespace DRRS_CSharp
{
public partial class frmDialyzer : Form
{
int pUserID;
public frmDialyzer()
{
InitializeComponent();
string Sql = "Select p.patient_id as patient_id,(n.patient_first_name+' '+n.patient_last_name+'| '+convert(varchar,n.patient_id)) as Name from patient_id p,patient_name n where n.patient_id=p.patient_id and n.status=1 and not exists(Select * from dialyser where dialyser.deleted_status=0 and dialyser.closed_status=0 and dialyser.patient_id=p.patient_id)";
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(Sql, conn);
dt.Load(cmd.ExecuteReader());
cboPatientID.DataSource = dt;
cboPatientID.ValueMember = "patient_id";
cboPatientID.DisplayMember = "Name";
cboPatientID.SelectedValue = 0;
}
In above code, I am able to display
patient first ,last name and patient_id as
Name in
DisplayMember property of
cboPatientID
Suppose i want to display a
trail of four zeros proceeding patient_id(such as if patient_id=2 then i want patient_id to be displayed as
00002) along with patient first,last name and patient_id as
Name in
DisplayMember property of
cboPatientID.
So I am getting patient_id along with patient first,last name in
DisplayMember property of
cboPatientID.
But what i want is my
patient_id must have
trail of four zeros proceeding my
patient_id
for example: my patient_id:
3 then i want my patient_id to be displayed as
00003.
Can anyone help me please on how to display trail of four zeros proceeding my patient_id along with patient first,last name as
Name in
DisplayMember property of
cboPatientID. Tell me what should i need to do to achieve my required result.?
Can anyone help me please?! Any help/guidance in solving of this problem would be greatly appreciated!