Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

how to show a group box along with it's datas based on a value of integer variable in c# windows forms with sql server 2008?

$
0
0

hi my name is vishal for past 7 i have been breaking on how to show each group box along with it's controls based on value in c# windows forms with sql server 2008.

i have 5 group boxes.

1.name:GrpStep1,enabled:true,visible:true(contains 8 labels,2combobox,3textboxes and 3 datetimepickers).

2.name:GrpBarCode,enabled:true,visible:true(contains GrpDEntry along with 1 label and 1 textbox).

3.name:GrpDEntry,enabled:true,visible:true(contains 2 radio buttons).

4.name:GrpStep2,enabled:true,visible:true(contains 1 textbox,1button and StrokeScribe control).

5.name:GrpStep3,enabled:true,visible:true(contains 1 combobox,2buttons,2 picture boxes and StrokeScribe control).

So GrpStep1 is the base group box,GrpBarCode is a group box is put on or embed on grpStep1,GrpDEntry in put on or embed on GrpBarCode,GrpStep2 in put on or embed on GrpBarCode and GrpDEntry and GrpStep3 is a group box which is visible to the designer at design-time.

Given is my code below in c# of what i have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DRRS_CSharp
{
    public partial class frmDialyzer : Form
    {
        int dStepIndex;
        int pUserID;
        public frmDialyzer()
        {
            InitializeComponent();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string PatientPull = ("Select p.patient_id as patient_id,n.patient_first_name as patient_fname,n.patient_last_name as patient_lname from patient_name n,patient_id p where n.patient_id=p.patient_id and n.status=1 and p.patient_id not in (Select patient_id from dialyser where deleted_status=0)");
            SqlCommand cmd = new SqlCommand(PatientPull);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                PatientPull = dr[0].ToString() + dr[1].ToString() + dr[2].ToString();
                cboPatientID.Items.Add(PatientPull);
            }
            dr.Close();
            string ManufacturerPull = ("Select distinct(ManufacturerName) from EquipmentData");
            SqlCommand mcd = new SqlCommand(ManufacturerPull);
            mcd.Connection = conn;
            mcd.CommandType = CommandType.Text;
            SqlDataReader cdr = mcd.ExecuteReader();
            while (cdr.Read())
            {
                ManufacturerPull = cdr[0].ToString();
                cboManufacturer.Items.Add(ManufacturerPull);
            }
            cdr.Close();
}
private void frmDialyzer_Load(object sender, EventArgs e)
        {
            dtExpDate.MinDate = dtMFRDate.Value;
            dStepIndex = 0;
            GrpStep1.Visible = true;
            GrpStep2.Visible = false;
            GrpStep3.Visible = false;
            dtStartDate.MinDate =dtStartDate.Value;
        }
 private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (GrpDEntry.Visible == true)
            {
                GrpDEntry.Visible = false;
                GrpStep1.Text = "Step 2";
                if (radioButton1.Checked == false)
                {
                    GrpBarCode.Visible = false;
                }
                else
                {
                    if (dStepIndex == 0)
                    {
                        string dFieldName = "";
                        Boolean vEmptyB = false;
                        if (cboManufacturer.Text == "")
                        {
                            vEmptyB = true;
                            dFieldName = "Please select a Manufacturer";
                        }
                        else if (cboequipmentType.Text == "")
                        {
                            vEmptyB = true;
                            dFieldName = "Please select size of the dialyzer";
                        }
                        // else if(txtMFRRefNo.Text.ToString().Trim()=="")
                        //{
                        // vEmptyB=true;
                        //dFieldName="Please enter Reference number of the dialyzer";
                        // }
                        else if (txtMFRLotNo.Text.ToString().Trim() == "")
                        {
                            vEmptyB = true;
                            dFieldName = "Please enter Lot No of the dialyzer";
                        }
                        else if (txtPVol.Text.ToString().Trim() == "")
                        {
                            vEmptyB = true;
                            dFieldName = "Please enter packed volume of the dialyzer";
                        }
                        if (vEmptyB == true)
                        {
                            MessageBox.Show(dFieldName + "should not be empty");
                            return;
                        }
                        string dDID;
                        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();
                        cmd.Connection = conn;
                        cmd.CommandType = CommandType.Text;
                        DataTable dt = new DataTable();
                        int autoGenId = -1;
                        cmd = new SqlCommand("Select max(agn) from dialyser;Select @autoGenId = SCOPE_IDENTITY();", conn);
                        cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
                        cmd.ExecuteNonQuery();
                        autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
                        if (autoGenId == 0)
                        {
                            dDID = "1";
                        }
                        else
                        {
                            dDID = autoGenId.ToString() + 1;
                        }
                        txtDID.Text = txtDID.Text.PadRight(5) + dDID;
                        conn.Close();
                        GrpStep1.Visible = false;
                        GrpStep2.Visible = true;
                        GrpStep3.Visible = false;
                        dStepIndex = 2;
                        btnNext.Enabled =false;
                        button1.Enabled = false;
                        txtDID.Focus();
                    }
 else if (dStepIndex == 2)
                    {
                        GrpStep1.Visible = false;
                        GrpStep2.Visible = false;
                        GrpStep3.Visible = true;
                        dStepIndex = 3;
                        btnNext.Visible = false;
                    }
}
}
}
 private void cboManufacturer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboManufacturer.SelectedIndex == -1)
            {
                return;
            }
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string ManufacturerSize = ("Select distinct(volume) from EquipmentData where ManufacturerName='" + cboManufacturer.Text.ToString() + "'");
            SqlCommand cmd = new SqlCommand(ManufacturerSize);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ManufacturerSize = dr[0].ToString();
                cboequipmentType.Items.Add(ManufacturerSize);
            }
            dr.Close();
}
 private void cboequipmentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboequipmentType.SelectedIndex == -1)
            {
                return;
            }
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string EquipmentVol = ("Select packed_volume from EquipmentData where ManufacturerName='" + cboManufacturer.Text.ToString() + "'and volume='" + cboequipmentType.Text + "'");
            SqlCommand cmd = new SqlCommand(EquipmentVol);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                EquipmentVol = dr[0].ToString();
                txtPVol.Text = EquipmentVol;
            }
            dr.Close();
}
 private void cboPatientID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboPatientID.SelectedIndex != -1)
            {
                btnSearch.Enabled = true;
                btnAssign.Enabled = true;
                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();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd = new SqlCommand("Select * from patient_name where status=1 and patient_id=" + cboPatientID.GetItemText(cboPatientID.SelectedIndex) + "'", conn);
                if (txtDID.Text.Length > 5)
                {
                    txtDID.Text = txtDID.Text.Substring(5);
                }
                DataTable dt = new DataTable();
                if (dt.Rows.Count == -1)
                {
                    if ((cmd.Parameters.AddWithValue("@virology", SqlDbType.Int).Value.GetHashCode() == 0))
                    {
                        txtDID.Text = txtDID.Text + 1;
                    }
                    else
                    {
                        txtDID.Text = txtDID.Text + 0;
                    }
                }
            }
        }
        private void btnAssign_Click(object sender, EventArgs e)
        {
            string dFieldName = "";
            Boolean vEmptyB = false;
            if (cboManufacturer.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please select a Manufacturer";
            }
            else if (cboequipmentType.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please select size of dialyzer";
            }
            else if (txtMFRRefNo.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please enter reference number of the dialyzer";
            }
            else if (txtMFRLotNo.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please enter lot number of the dialyzer";
            }
            else if (txtPVol.Text.ToString().Trim() == "")
            {
                vEmptyB = true;
                dFieldName = "Please enter packed volume of the dialyzer";
            }
            else if (cboPatientID.SelectedIndex == -1)
            {
                vEmptyB = true;
                dFieldName = "Please select Patient ID";
            }
            if (vEmptyB == true)
            {
                MessageBox.Show(dFieldName + "should not be empty");
                return;
            }
            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();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            int autoGenId = -1;
            cmd = new SqlCommand("Insert into dialyser(dialyserID,manufacturer,mfr_ref_number,mfr_lot_number,mfr_date,exp_date,start_date,packed_volume,dialyzer_size,patient_id,row_upd_date,user_id)" + "Values(@dialyserID,@manufacturer,@mfr_ref_number,@mfr_lot_number,@mfr_date,@exp_date,@start_date,@packed_volume,@dialyzer_size,@patient_id,GetDate(),@user_id);Select @autoGenId = SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@dialyserID", txtDID.Text.ToString());
            cmd.Parameters.AddWithValue("@manufacturer", cboManufacturer.Text.ToString());
            cmd.Parameters.AddWithValue("@mfr_ref_number", txtMFRRefNo.Text.ToString());
            cmd.Parameters.AddWithValue("@mfr_lot_number", txtMFRLotNo.Text.ToString());
            cmd.Parameters.AddWithValue("@mfr_date", dtMFRDate.Value);
            cmd.Parameters.AddWithValue("@exp_date", dtExpDate.Value);
            cmd.Parameters.AddWithValue("@start_date", dtStartDate.Value);
            cmd.Parameters.AddWithValue("@packed_volume", txtPVol.Text.ToString());
            cmd.Parameters.AddWithValue("@dialyzer_size", cboequipmentType.Text.ToString());
            cmd.Parameters.AddWithValue("@patient_id", cboPatientID.GetItemText(cboPatientID.SelectedIndex));
            cmd.Parameters.AddWithValue("@user_id", pUserID);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 4, txtDID.Text.ToString() + "Dialyzer detail was successfully assigned to the patient-(" + cboPatientID.GetItemText(cboPatientID.SelectedIndex) + ")");
 private void button1_Click(object sender, EventArgs e)
        {
            if (txtDID.Tag.ToString() == "0")
            {
                btnNext.Enabled = true;
                button1.Enabled = false;
                txtDID.Enabled = false;
                barCDialyzer.Alphabet = STROKESCRIBELib.enumAlphabet.AZTEC;
                barCDialyzer.CtlText = txtDID.Text + (char)9;
                barCOrg.Alphabet = STROKESCRIBELib.enumAlphabet.AZTEC;
                barCOrg.CtlText = txtDID.Text + (char)9;
                int rc = 0;
                rc = barCOrg.SavePicture("D:\\Workarea\vishal\\DRRS in CSharp\\DRRS CSharp\\DRRS CSharp\bar1.bmp", STROKESCRIBELib.enumFormats.BMP, barCOrg.BitmapW, barCOrg.BitmapH);
                if (rc > 0)
                {
                    MessageBox.Show(barCOrg.ErrorDescription);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string dPatientID;
            dPatientID = cboPatientID.GetItemText(cboPatientID.SelectedIndex);
            frmPatient p = new frmPatient();
            p.loadPatient(dPatientID);
            p.Show();
        }

        private void dtMFRDate_ValueChanged(object sender, EventArgs e)
        {
            dtStartDate.MinDate =dtStartDate.Value;
        }

        private void txtPVol_TextChanged(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", txtPVol.Text))
            {
                MessageBox.Show("Packed volume should be numeric.");
                txtPVol.Text.Remove(txtPVol.Text.Length - 1);
            }
        }

where frmDialyzer is name of my form.

As you can see all my combobox functions works perfectly according to my expectations. But here is the problem the code works till it comes or shows GrpStep1 and after that the code does not perform any process.

Given below is code the same thing which i have done in vb6 with ms access with adodb and it works.

Option Explicit
Dim dStepIndex As Integer
Dim inst As String
Private Sub loadPatientID()
    On Error GoTo errh
    Dim vSQLStr As String
    Dim LPatientID As String
    vSQLStr = "select p.patient_id as patient_id,n.patient_first_name as patient_fname, n.patient_last_name as patient_lname from patient_name n,patient_id p where n.patient_id=p.patient_id and n.status = true and p.patient_id not in (select patient_id from dialyser where deleted_status=false);"
    Dim oRS As New ADODB.Recordset
    If (adoDatabase.State = 0) Then
        adoDatabase.Open
    End If
    oRS.Open vSQLStr, adoDatabase, adOpenForwardOnly, adLockReadOnly
    cboPatientID.Clear
    Do While Not oRS.EOF
         '// Do something with the data'
         LPatientID = oRS.Fields("patient_id").Value
         LPatientID = Replace(Space(6 - Len(LPatientID)) & LPatientID, " ", "0")
         cboPatientID.AddItem oRS.Fields("patient_fname").Value & " " & oRS.Fields("patient_lname").Value & "|" & LPatientID
         cboPatientID.ItemData(cboPatientID.NewIndex) = oRS.Fields("patient_id").Value
         oRS.MoveNext
    Loop
    oRS.Close
    Exit Sub
errh:
    MsgBox Err.Description, vbCritical
End Sub
Private Sub loadManufacurer()
    On Error GoTo errh
    Dim vSQLStr As String
    vSQLStr = "select distinct(ManufacturerName) from EquipmentData"
    Dim oRS As New ADODB.Recordset
    oRS.Open vSQLStr, adoDatabase, adOpenForwardOnly, adLockReadOnly
    cboManufacturer.Clear
    Do While Not oRS.EOF
         '// Do something with the data'
         cboManufacturer.AddItem oRS.Fields("ManufacturerName").Value
         oRS.MoveNext
    Loop
    oRS.Close
    Exit Sub
errh:
    MsgBox Err.Description, vbCritical
End Sub
Private Sub cboequipmentType_Click()
    On Error GoTo errh
    If cboequipmentType.ListIndex = -1 Then Exit Sub
    Dim vSQLStr As String
    vSQLStr = "select * from EquipmentData where Manufacturername='" & cboManufacturer.Text & "' and volume='" & cboequipmentType.Text & "' and status=true"
    Dim oRS As New ADODB.Recordset
    oRS.Open vSQLStr, adoDatabase, adOpenForwardOnly, adLockReadOnly
    If oRS.EOF = False Then
         '// Do something with the data'
        If IsNull(oRS.Fields("packed_volume").Value) = False Then
            TxtPVol.Text = oRS.Fields("packed_volume").Value
        End If
         oRS.MoveNext
    End If
    oRS.Close
'    oRS.Open "select * from application_preferences where status=true", adoDatabase, adOpenForwardOnly, adLockReadOnly
'
'    If (oRS.EOF = False) Then
'        TxtPVol.Text = oRS.Fields("bundle_volume").Value
'    End If
'    oRS.Close
    Exit Sub
errh:
    MsgBox Err.Description, vbCritical
End Sub
Private Sub cboManufacturer_Click()
    On Error GoTo errh
    If cboManufacturer.ListIndex = -1 Then Exit Sub
    Dim vSQLStr As String
    vSQLStr = "select distinct(volume) from EquipmentData where Manufacturername='" & cboManufacturer.Text & "'"
    Dim oRS As New ADODB.Recordset
    oRS.Open vSQLStr, adoDatabase, adOpenForwardOnly, adLockReadOnly
    cboequipmentType.Clear
    Do While Not oRS.EOF
         '// Do something with the data'
         cboequipmentType.AddItem oRS.Fields("volume").Value
         oRS.MoveNext
    Loop
    oRS.Close
    Exit Sub
errh:
    MsgBox Err.Description, vbCritical
End Sub
Private Sub cboPatientID_Click()
    On Error GoTo errh
    If (cboPatientID.ListIndex <> -1) Then
        cmdSearch.Enabled = True
        Command5.Enabled = True
        Dim rs1 As ADODB.Recordset
        Dim vFieldNameStr As String
        Set rs1 = New ADODB.Recordset
        If (adoDatabase.State = 0) Then
            adoDatabase.Open
        End If
        rs1.Open "select * from patient_name where status=true and patient_id=" & cboPatientID.ItemData(cboPatientID.ListIndex), adoDatabase, adOpenForwardOnly, adLockReadOnly
        If Len(TxtDID.Text) > 5 Then
            TxtDID.Text = Left(TxtDID.Text, 5)
        End If
        If (rs1.EOF = False) Then
            If (rs1.Fields("virology ").Value = 0) Then
                TxtDID.Text = TxtDID.Text & 1
            Else
                TxtDID.Text = TxtDID.Text & 0
            End If
        Else
            TxtDID.Text = TxtDID.Text & 0
        End If
        lblPID.Caption = Left(cboPatientID.Text, InStr(1, cboPatientID.Text, "|", vbTextCompare) - 1) & " - " & Mid(cboPatientID.Text, InStr(1, cboPatientID.Text, "|", vbTextCompare) + 1)
        barCDialyzer.Alphabet = CODE128
        barCDialyzer.Text = TxtDID.Text & Chr(9)
        barCOrg.Alphabet = CODE128
        barCOrg.Text = TxtDID.Text & Chr(9)
        Dim bar_w As Long
        Dim rc As Long
        bar_w = barCOrg.BitmapW
        rc = barCOrg.SavePicture(App.Path & "\barcode.bmp", BMP, bar_w * 2, bar_w)
        If rc > 0 Then
          MsgBox barCOrg.ErrorDescription
        End If
    Else
        cmdSearch.Enabled = False
        Command5.Enabled = False
    End If
    Exit Sub
errh:
    MsgBox Err.Description, vbCritical
End Sub
Private Sub cmdNext_Click()
    On Error GoTo errh
    If (fmeDEntry.Visible = True) Then
        fmeDEntry.Visible = False
        fmeStep1.Caption = "Step 2"
        If (optDEntry1.Value = False) Then
            fmeBarCode.Visible = False
        End If
    Else
        If dStepIndex = 0 Then
            Dim vEmptyB As Boolean
            Dim vFieldNameStr As String
            If cboManufacturer.Text = "" Then
                vEmptyB = True
                vFieldNameStr = "Manufacturer"
            ElseIf Trim(cboequipmentType.Text) = "" Then
                vEmptyB = True
                vFieldNameStr = "Size"
'            ElseIf Trim(TxtMFRRefNo.Text) = "" Then
'                vEmptyB = True
'                vFieldNameStr = "MFR Ref No"
            ElseIf Trim(TxtMFRLotNo.Text) = "" Then
                vEmptyB = True
                vFieldNameStr = "MFR Lot No"
            ElseIf Trim(TxtPVol.Text) = "" Then
                vEmptyB = True
                vFieldNameStr = "Packed Volume"
            End If
            If vEmptyB = True Then
                MsgBox vFieldNameStr & " should not be empty", vbCritical
                Exit Sub
            End If
            Dim vSQLStr As String
            Dim dDID As String
            vSQLStr = "select max(agn) from dialyser"
            Dim oRS As New ADODB.Recordset
            oRS.Open vSQLStr, adoDatabase, adOpenForwardOnly, adLockReadOnly
            If Not oRS.EOF Then
                If (IsNull(oRS.Fields(0).Value)) Then
                    dDID = "1"
                Else
                    dDID = oRS.Fields(0).Value + 1
                End If
                TxtDID.Text = Replace(Space(5 - Len(dDID)) & dDID, " ", "0")
            Else
                TxtDID.Text = Replace(Space(5 - Len("1")) & "1", " ", "0")
            End If
            oRS.Close
            fmeStep1.Visible = False
            fmeStep2.Visible = True
            fmeStep3.Visible = False
            dStepIndex = 2
            cmdNext.Enabled = False
            Command1.Enabled = False
            TxtDID.SetFocus
        ElseIf dStepIndex = 2 Then
            fmeStep1.Visible = False
            fmeStep2.Visible = False
            fmeStep3.Visible = True
            dStepIndex = 3
            cmdNext.Visible = False
        End If
    End If
    Exit Sub
errh:
    MsgBox Err.Description, vbCritical
End Sub
Private Sub Form_Load()
    dtMFRExpDate.MinDate = Now
    dtMFRDate.MaxDate = Now
    loadPatientID
    loadManufacurer
    dStepIndex = 0
    fmeStep1.Visible = True
    fmeStep2.Visible = False
    fmeStep3.Visible = False
    dtMFRStartDate.MinDate = Now
End Sub

what i want is in my c# code with sql server 2008 after processing till GrpStep1 upon clicking on next button in my frmDialyzer form i want to go to GrpStep2 and GrpStep3.

You can in my vb6 code the processing of each frame is done through value of dStepIndex value in my form in vb6.

I know i have to do some modifications in my c# code but i dont know how to do it can anyone help me please.! If anyone wants any details regarding controls in each  group box please ask me in order to help me please ask.! Any help or guidance in solving this problem would be greatly appreciated.


vishal


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>