Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
hi my name is vishal i was wondering how to find how many times a same value(combobox item/text) has been used in same table from c# windows forms with sql server2008.

I have a combobox named:cboDialyzerID in my form named:frmCleaning.
Given below is structure of my table named:reprocessor in sql server2008
ColumnName DataType AllowNulls
reprocessor_id Int No(since auto-increment primary key)
start_date datetime Yes
end_date datetime Yes
row_upd_date datetime Yes
dialysis_date datetime Yes
bundle_vol Int Yes
disinfectant Int Yes
technician_id Int Yes
user_id Int Yes
dialyzer_id nvarchar(30) Yes
prime_volume Int Yes

Given below is my c# code of form:frmCleaning from which i insert values into table reprocessor in sql server2008.
C#
private void btnCreate_Click(object sender, EventArgs e)
        {
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            int autoGenId = -1;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Insert into reprocessor(start_date,end_date,dialyzer_id,dialysis_date,prime_volume,bundle_vol,disinfectant,technician_id,row_upd_date,user_id,errorCode)" + "Values(@start_date,@end_date,@dialyzer_id,@dialysis_date,@prime_volume,@bundle_vol,@disinfectant,@technician_id,GetDate(),@user_id,@errorCode); Select @autoGenId=SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@start_date", dtSTime.Value);
            cmd.Parameters.AddWithValue("@end_date", dtETime.Value);
            cmd.Parameters.AddWithValue("@dialyzer_id", cboDialyzerID.Text);
            cmd.Parameters.AddWithValue("@dialysis_date", dtDDate.Value);
            cmd.Parameters.AddWithValue("@prime_volume", txtPrimeVol.Text);
            cmd.Parameters.AddWithValue("@bundle_vol", txtBVol.Text);
            cmd.Parameters.AddWithValue("@disinfectant", txtDisInfectant.Text);
            cmd.Parameters.AddWithValue("@technician_id",cboTechnician.SelectedValue);
            cmd.Parameters.AddWithValue("@user_id", pUserID);
            cmd.Parameters.AddWithValue("@errorCode", 0);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 7,cboDialyzerID.Text.ToString());
MessageBox.Show("Reprocessing data was successfully added", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
}

I populate my combobox named:cboDialyzerID in frmCleaning through following code in c#:
C#
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 frmCleaning : Form
    {
public frmCleaning()
        {
InitializeComponent();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
string DialyzerPull = ("Select dialyserID from dialyser where deleted_status=0 and closed_status=0");
            SqlCommand dcd = new SqlCommand(DialyzerPull);
            dcd.Connection = conn;
            dcd.CommandType = CommandType.Text;
            SqlDataReader cr = dcd.ExecuteReader();
            while (cr.Read())
            {
                DialyzerPull = cr[0].ToString();
                cboDialyzerID.Items.Add(DialyzerPull);
            }
            cr.Close();
}


What i want is to find and know how many times each value in field dialyzer_id of data-type:nvarchar(30) in table:reprocessor in sql server2008 has been used in same table:reprocessor from c# windows forms with sql server2008. Can anyone help me please? Any help/guidance in solving of this problem would be greatly appreciated.! Can anybody help please!
Posted
Comments
Prasad Avunoori 23-May-14 6:08am    
I didn't get your question? Make it simple.
Prasad Avunoori 23-May-14 6:11am    
If i guess correctly you may use:
SELECT dialyzer_id, COUNT(dialyzer_id) FROM reprocessor GROUP BY dialyzer_id
Prasad Khandekar 23-May-14 6:16am    
Hello,

You can use query similar to one shown below.

SELECT dialyzer_id,
COUNT(1) FROM reprocessor
GROUP BY dialyzer_id;

SQL
SELECT dialyzer_id, COUNT(dialyzer_id) FROM reprocessor GROUP BY dialyzer_id
 
Share this answer
 
Comments
Member 10248768 23-May-14 7:16am    
Yes! Your query worked perfectly! But my last question is how to bind result of sql select query to int of string variable in c# windows forms with sql server2008? Reply please?
You can write a SQL Query that will do this.

Use a SQL aggregation function called 'count', and the 'Group By' clause.

http://msdn.microsoft.com/en-us/library/ms177673.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900