retrieving values from multiple columns in asp drop down - c#

I've got a dropdown whose values are retrieved from a database. I am retrieving ID and name from the database.
public void GetDepartment_temp()
{
try
{
DataTable dt = new DataTable();
listBoxDepartment.ClearSelection();
Get_Department objDAL = new Get_Department();
dt = objDAL.Get_Hospital_Department();
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
listBoxDepartment.Items.Add(new ListItem(row["department_name"].ToString(), row["department_id"].ToString()));
}
}
}
catch (Exception) { }
}
I've got to show the number of employees of each department in the text box. Suppose a user selects human department, then the text box should display the number of employees in that department.
For the ListBox, only two values from the database can be retrieved. How can I show the number of employee in this condition?
public DataTable Get_Hospital_Department()
{
try
{
DataTable dt = new DataTable();
dt = DbAccessHelper.ExecuteDataSet("p_get_hospital_department", true).Tables[0];
return dt;
}
catch (Exception) { return null; }
}
CREATE PROCEDURE [dbo].[p_get_hospital_department]
AS
BEGIN
SET NOCOUNT ON;
SELECT department_id
,department_name
FROM [dbo].[tbl_hospital_department];
END

The statement For the ListBox, only two values from the database can be retrieved. is not correct. You can populate the datatable with as many fields as you want. However, you can set only the Value and text attributes of the Listbox item as you have done.
Change the stored procedure code to fetch the employee count also.
Mark your datatable dt as static and public.
Fetch the datable and you can play with the data as you want. You can fetch the employee count in the textbox on listview selected index changed as shown below:
public static DataTable dt = new DataTable();
public void GetDepartment_temp()
{
try
{
string connString = ConfigurationManager.ConnectionStrings["SOConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand command =
new SqlCommand(
"select Department.DepartmentID, Department.[Department Name], count( Department.DepartmentID) as empCount from Department join Employee on Department.DepartmentID = Employee.DepartmentID group by Department.DepartmentID, Department.[Department Name]",
connection);
command.Connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
dt.PrimaryKey = new DataColumn[] {dt.Columns["DepartmentID"]};
ListBox1.ClearSelection();
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
ListBox1.Items.Add(new ListItem(row["Department Name"].ToString(),
row["DepartmentID"].ToString()));
}
}
}
catch (Exception ex)
{
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRow dr = dt.Rows.Find(Convert.ToInt32(ListBox1.SelectedItem.Value));
TextBox5.Text = dr["empCount"].ToString();
}

Related

comboBox.SelectedValue to fetch id on sql server

Im having trouble fetching ID of selected value from combobox, cna i get some help? the program should get the id of the selected string in the combobox from database.
private void GetValues_Click(object sender, RoutedEventArgs e)
{
ReportesAsistenciaTableAdapter ada = new ReportesAsistenciaTableAdapter();
DataTable dt = ada.GetDataBy((int)selectClass.SelectedItem, dateBox.Text);
if (dt.Rows.Count > 0)
{
DataTable dtNew = ada.GetDataBy((int)selectClass.SelectedItem, dateBox.Text);
EstudiantesList.ItemsSource = dtNew.ToString();
}
else
{
Estudiantes1TableAdapter studentsAdapter = new Estudiantes1TableAdapter();
DataTable dtStudents = studentsAdapter.GetDataByClaseID((int)selectClass.SelectedItem);
foreach (DataRow row in dtStudents.Rows)
{
ada.InsertQuery((int)row[0], (int)selectClass.SelectedItem, dateBox.Text, "", row[1].ToString(), selectClass.Text) ;
}
DataTable dtNew = ada.GetDataBy((int)selectClass.SelectedItem, dateBox.Text);
EstudiantesList.ItemsSource = dtNew.ToString();
}

Add row from datagridview1 to datagridview2

I have two datagridviews in one from. I need to get data from database to datagridview1 (using Select *from database...) then I want to add data from datagriwview to datagridview2 using Selected Rows.
First I wanted to solve this problem to get Selected Row's ID, when I select row in datagridview it shows in datagridview2, but when I select another row, it is updating in datagridview, it does not add as new row. I tried several ways but did not solve this problem, Is there anyone help me to solve this problem? Thanks
private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
{
int id = Convert.ToInt32
(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value);//3
try
{
MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand command = start.CreateCommand();
command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler WHERE id = '" + id.ToString() + "'";
conn.Open();
MySqlDataAdapter oxu = new MySqlDataAdapter(command);
DataTable dt = new DataTable();
oxu.Fill(dt);
dataGridView2.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Is quite simple the explanation: everytime that you make a double click to a datagridview1's cell you replace the old datatable with a new one. If you want append the result you can do something like this:
MySqlConnection conn = new MySqlConnection(connection);
MySqlCommand command = start.CreateCommand();
command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler WHERE id = '" + id.ToString() + "'";
conn.Open();
MySqlDataAdapter oxu = new MySqlDataAdapter(command);
DataTable dt = new DataTable();
oxu.Fill(dt);
if(dataGridView2.DataSource != null) {
DataTable pr = dataGridView2.DataSource as DataTable;
pr.Merge(dt);
dataGridView2.DataSource = pr;
}
else
dataGridView2.DataSource = dt;
Since you have all information in datagridview1 you should just copy the contents of the selected row into a new row for datagridrow2.
The DataGridView is based on a DataSet which contains DataTables.
The DataTable contains rows.
You cannot move a row from one table to annother.
Instead you have to create a new row and insert into the DataTable of DataGridView2
private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentRow.Cells["Id"].Value != null)
{
int Id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Id"].Value);
MySqlConnection start = new MySqlConnection(baglanti);
MySqlCommand command = start.CreateCommand();
command.CommandText = "SELECT id, muayine_adi, sabit_qiymet FROM tibbi_xidmetler WHERE id = '" + Id + "'";
start.Open();
MySqlDataAdapter oxu = new MySqlDataAdapter(command);
DataTable dt = new DataTable();
oxu.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
int idx = dataGridView2.Rows.Count - 1;
dataGridView2.Rows.Add(dt.Rows.Count);
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
int rVal = (idx + i) + 1;
dataGridView2.Rows[rVal].Cells["id"].Value = dt.Rows[i]["id"].ToString();
dataGridView2.Rows[rVal].Cells["muayine_adi"].Value = dt.Rows[i]["muayine_adi"].ToString();
dataGridView2.Rows[rVal].Cells["sabit_qiymet"].Value = dt.Rows[i]["sabit_qiymet"].ToString();
}
}
start.Close();
}
}

How to get a specific column value from a DataTable in c#

How to get a specific column value from a DataTable in c#
I have a problem with my code C#.
I need read a specific column value from a DataTable.
I've tried using this solution without success, because the output is the name of column selected in the query (File) and not the value of field database.
I would greatly appreciate any help you can give me in working this problem.
Here is my code:
public DataTable GridViewBind()
{
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
sql1 = " SELECT FILE FROM tbl_A WHERE Id = 1; ";
using (OdbcDataAdapter command =
new OdbcDataAdapter(sql1, cn))
{
try
{
cn.Open();
dset = new DataSet();
dset.Clear();
command.Fill(dset);
DataTable dt = dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write(dt.Columns[0].ToString());
return dt;
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
if (command != null)
{
command.Dispose();
}
if (cn != null)
{
cn.Close();
cn.Dispose();
}
}
}
}
}
Edit #1
Now I have error:
System.IndexOutOfRangeException: There is no row at position 0.
public DataTable GridViewBind()
{
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
sql1 = " SELECT FILE FROM tbl_A WHERE Id = 1; ";
using (OdbcDataAdapter command =
new OdbcDataAdapter(sql1, cn))
{
try
{
cn.Open();
dset = new DataSet();
dset.Clear();
command.Fill(dset);
DataTable dt = dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
string file = dt.Rows[0].Field<string>(0);
Response.Write(file.ToString());
return dt;
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
if (command != null)
{
command.Dispose();
}
if (cn != null)
{
cn.Close();
cn.Dispose();
}
}
}
}
}
The table normally contains multiple rows. Use a loop and use row.Field<string>(0) to access the value of each row.
foreach(DataRow row in dt.Rows)
{
string file = row.Field<string>("File");
}
You can also access it via index:
foreach(DataRow row in dt.Rows)
{
string file = row.Field<string>(0);
}
If you expect only one row, you can also use the indexer of DataRowCollection:
string file = dt.Rows[0].Field<string>(0);
Since this fails if the table is empty, use dt.Rows.Count to check if there is a row:
if(dt.Rows.Count > 0)
file = dt.Rows[0].Field<string>(0);
This is pretty simple to achieve. From the DataTable, first we can apply the Select function to get only the row that is associated with Id=1.
After that, we can do the CopyToDataTable() function so as to get it in the DataTable format.
Since, we are expecting that there is only row for Id=1, we can directly apply Rows[0] and then give the column name, convert to string and assign to file variable.
string file = dt.Select("Id=1").CopyToDataTable().Rows[0]["FILE"].ToString()
Note that if you are selecting string value, you need to apply single quotes. For eg, if Id is of string type, then you need to apply like below.
Id='1'

Populate dropdownlist inside a gridview

I have a Dropdownlist in a Gridview and i have to show the records associated with every id.And the ID contains more than 10 records so how can i show them??
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con.Open();
var ddl = (DropDownList)e.Row.FindControl("DropDownList1");
//int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
SqlCommand cmd = new SqlCommand("select LastName from Profile_Master", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "LastName";
ddl.DataBind();
}
}
FillSelect(myDropDownList, "--select--", "0", true);
public static void FillSelect(DropDownList DropDown, string SelectItemText, string SelectItemValue, bool includeselectitem)
{
List<PhoneContact> obj_PhoneContactlist = getAll();
if (obj_PhoneContactlist != null && obj_PhoneContactlist.Count > 0)
{
DropDown.DataTextField = "PhoneContactName";
DropDown.DataValueField = "id";
DropDown.DataSource = obj_PhoneContactlist.OrderBy(o => o.PhoneContactName);//linq statement
DropDown.DataBind();
if (includeselectitem)
DropDown.Items.Insert(0, new ListItem(SelectItemText, SelectItemValue));
}
}
public static List<PhoneContact> getAll()
{
obj_PhoneContactlist = new List<PhoneContact>();
string QueryString;
QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["Admin_raghuConnectionString1"].ToString();
obj_SqlConnection = new SqlConnection(QueryString);
obj_SqlCommand = new SqlCommand("spS_GetMyContacts");
obj_SqlCommand.CommandType = CommandType.StoredProcedure;
obj_SqlConnection.Open();
obj_SqlCommand.Connection = obj_SqlConnection;
SqlDataReader obj_result = null;
obj_SqlCommand.CommandText = "spS_GetMyContacts";
obj_result = obj_SqlCommand.ExecuteReader();
//here read the individual objects first and append them to the listobject so this we get all the rows in one list object
using (obj_result)
{
while (obj_result.Read())
{
obj_PhoneContact = new PhoneContact();
obj_PhoneContact.PhoneContactName = Convert.ToString(obj_result["PhoneContactName"]).TrimEnd();
obj_PhoneContact.PhoneContactNumber = Convert.ToInt64(obj_result["PhoneContactNumber"]);
obj_PhoneContact.id = Convert.ToInt64(obj_result["id"]);
obj_PhoneContactlist.Add(obj_PhoneContact);
}
}
return obj_PhoneContactlist;
}
I have done this to get my phonecontacts which are in the data base into dropdown you can change the stored procedures and the values according to your need.
Hope this helps:D
We just ran into this issue where I work. Our way around this problem was to first get the DropDownLists UniqueID. This is basically a Client ID. Inside of that ID is a reference to the row of the GridView that it was selected from. THE ONLY PROBLEM is that it seems to add 2 to the row count. So if you select Row 1's DropdownList, the Unique ID will bring you a reference to the 3rd row. So:
Get the unique ID > Split it however you need to to get the row > use the row number to get the values you need.

Datatable-Access Database Updating Concurrency violation

I'm working on a project which uses an Access database. First of all I copy the information that i need from db and put it in a DataTable (with combining 2 tables). Over this datatable I search items; update, delete, add rows using a GridView and all of that happens in an ajax update panel.
Problem occurs when i try to apply the changes back to the db. What i want to do is get certain rows of the DataTable and update/insert/delete them to certain rows of certain tables. Important thing is database table has to contain the same information as the DataTable.
İ actually don't know if I can use the OledbDataAdapter.Update this way.
Here is the exeption that i get : Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
My Code:
void load_dtTable()
{
OleDbConnection bag = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Profiles\\StjBugraO\\Desktop\\data.accdb");
DataTable dtResult = new DataTable();
if (Session["dtResult"] != null)
{
dtResult = (DataTable)Session["dtResult"];
}
else
{
bag.Open();
try
{
OleDbDataAdapter adtr = new OleDbDataAdapter("select gt.NodeID, gt.unit_num,gt.unit_name_text,gt2.ip_id ,gt2.server_name_text ,gt.call_flow_type From gt inner join gt2 on gt.cm_id=gt2.cm_id", bag);
dtResult.Clear();
adtr.Fill(dtResult);
adtr.Dispose();
dtResult.Columns[0].ColumnName = "NodeID";
dtResult.Columns[1].ColumnName = "Şube Kodu";
dtResult.Columns[2].ColumnName = "Şube ADI";
dtResult.Columns[3].ColumnName = "IP Adresi";
dtResult.Columns[4].ColumnName = "Call Manager";
dtResult.Columns[5].ColumnName = "Santral Tipi";
foreach (DataRow dr in dtResult.Rows)
{
if ((string)dr[5] == "P")
{
dr[5] = "PRI";
}
else if ((string)dr[5] == "A") dr[5] = "ANALOG";
}
}
catch (Exception e)
{
}
Session["dtResult"] = dtResult as DataTable;
bag.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
DataTable dtResult = Session["dtResult"] as DataTable;
//changing the names of the columns (that i need) to be the same with the db
dtResult.Columns[0].ColumnName = "NodeID";
dtResult.Columns[1].ColumnName = "unit_num";
dtResult.Columns[2].ColumnName = "unit_name_text";
dtResult.Columns[3].ColumnName = "ip_id";
dtResult.Columns[5].ColumnName = "call_flow_type";
OleDbConnection bag = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\Profiles\\StjBugraO\\Desktop\\data.accdb");
OleDbDataAdapter adtr2 = new OleDbDataAdapter("select NodeID ,unit_num ,unit_name_text , ip_id , call_flow_type from gt", bag);
OleDbCommandBuilder Ocmd = new OleDbCommandBuilder(adtr2);
adtr2.DeleteCommand=Ocmd.GetDeleteCommand();
adtr2.InsertCommand=Ocmd.GetInsertCommand();
adtr2.UpdateCommand=Ocmd.GetUpdateCommand();
DataTable dt = dtResult.GetChanges();
int updates = 0;
try
{
if(dt != null)
updates=adtr2.Update(dtResult);
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
//changing them back
dtResult.Columns[0].ColumnName = "NodeID";
dtResult.Columns[1].ColumnName = "Şube Kodu";
dtResult.Columns[2].ColumnName = "Şube ADI";
dtResult.Columns[3].ColumnName = "IP Adresi";
dtResult.Columns[4].ColumnName = "Call Manager";
dtResult.Columns[5].ColumnName = "Santral Tipi";
Label2.Text = updates + "changes applied.";
adtr2.Dispose();
bag.Close();
}

Categories

Resources