how to collect multiple dataset data into one dataset? - c#

I need to show multiple records together in a gridview in c# with SQL.
i have an array of Id's using those id's i need to get all details correspond to those id's.
i have an array ID[] Dataset ds
for(int i=0; i<=ID.Length-1; i++)
{
dss = fill_DS("select Client_id, F_name, L_name, Ph, Alt_ph, Time_date, Plan_duration, Brand, Email from Client_detail where Client_id='" + ID[i]+ "'");
}
Second method
public static DataSet fill_DS(string query)
{
DataSet ds=null;
try
{
SqlDataAdapter da = new SqlDataAdapter(query, con);
ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
}
return ds;
}
It only returns me the result of the last row only or for last id only. i need to combine the rows for all id's and show them in a gridview.
How to do this?

Related

how to populate combo box from database and insert new item on index zero in C#

This code below works for me, but i have not been able to insert ---Select--- to the for first index with value zero, i have tried:
cboGrade.Items.Insert(0,"---Select---)
but it is not working for me.
Also i want to able to retrieve and display the displaymember and displayvalue on the combo box while retrieving from database without items not being repeated:
private void LoadGrade()
{
using (SQLiteConnection conn = new SQLiteConnection(connstring))
{
try
{
string query = "select GradeCode, GradeName from Grade";
SQLiteDataAdapter da = new SQLiteDataAdapter(query, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "OrgGrade");
cboGrade.DisplayMember = "GradeName";
cboGrade.ValueMember = "GradeCode";
cboGrade.DataSource = ds.Tables["OrgGrade"];
}
catch (Exception ex)
{
MessageBox.Show("Error occured loading grade!");
}
}
}
Items only works if you have an unbound combo hence you either need to build the items array OR use binding.
What you need to do is insert into the DataTable you are binding like this:
DataTable dt = ds.Tables["OrgGrade"]
// generate the data you want to insert
DataRow newRow= dt.NewRow();
newRow.GradeName = "-- Select --";
newRow.GradeCode = 0;
// insert in the desired place
dt.Rows.InsertAt(newRow, 0);
Then bind using:
cboGrade.DataSource = dt;
Similar to what you have.

display data to listbox C#

Listbox does not show data. Verified data is in database and I am not getting an
error. Not sure where/what is wrong. Thanks in advance. My code is attached.
private void UpDateList()
{
// add data connection and fill data set.
SqlConnection conn = new SqlConnection(dataSource);
DataTable dt = new DataTable();
string sqlString = "select * from Suppliers";
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
cmd.Connection = conn;
conn.Open();
cmd.CommandText = sqlString;
da.Fill(ds);
conn.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
foreach(DataRow dRow in ds.Tables[0].Rows)
{
ArrayList values = new ArrayList();
foreach(object value in dRow.ItemArray)
{
values.Add(value);
_Suppliers.Add(values);
}
}
lstSuppliers.DataSource = _Suppliers;
lstSuppliers.Update();
}
It's kinda pointless to enumerate one bindable data collection and transfer data into another bindable collection so it can be bound. Just have your list use the default view of the datatable that already holds the data (allows sorting, filtering etc)
E.g.
LstSuppliers.DataSource = ds.Tables[0].DefaultView;
LstSuppliers.DisplayMember = "column name goes here of what to show eg SupplierName";
LstSuppliers.ValueMember = "column whose value to use for lstSuppliers.SelectedValue e.g. supplierId";
And then for example, not required but an example possibility:
ds.Tables[0].DefaultView.Sort = "[SupplierName] ASC";

How to get datarow column values by using c#

Iam using a datatable to fetch table values from mysql DB.
My codes
public static DataTable mydealdetails()
{
try
{
string connString = "Server=localhost;database=mytable;uid=root;password=Mytable";
string query = "SELECT * FROM `mytable`.`mydealdetails`";
MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
DataSet DS = new DataSet();
ma.Fill(DS);
return DS.Tables[0];
}
catch (MySqlException e)
{
throw new Exception(e.Message);
}
}
And iam also using this methode
var expression = "MovieMasterId = '"+mouvieid+"'";
DataRow[] foundRows = allmydeals.Select(expression);
I can get two rows count here,but i want to know how can i get each column values by using this methode

Select from dataset where data in dataset matches user input

I am trying to query or match user input against a dataset using a DataTable:
I am populating the dataset from a stored procedure which selects only a single column from a single table: Example: UserID Column. **I am not selecting the entire content of the table.*
public static DataSet LoadProfile()
{
SqlCommand cmdSQL = new SqlCommand("usp_LoadProfile", ConnectDatabase);
cmdSQL.CommandType = CommandType.StoredProcedure;
SqlDataAdapter daSQL = new SqlDataAdapter(cmdSQL);
DataSet ds = new DataSet();
daSQL.Fill(ds);
try
{
ConnectDatabase.Open();
cmdSQL.ExecuteNonQuery();
}
catch(Exception)
{
StatusMsg = ex.Message;
}
finally
{
ConnectDatabase.Close();
cmdSQL.Parameters.Clear();
cmdSQL.Dispose();
}
return ds;
}
I have the following method called in the form load event: I need to populate the dataset on from load.
public static DataTable LoadData()
{
DataSet dsView = new DataSet();
dsView = LoadProfile();
DataTable tblExample = dsView.Tables["Example"];
return tblExample;
}
Finally what I would like to do is match the user entry from the DataTable.
I have this in button event:
DataRow[] results;
results = LoadData().Select(txtExample.Text);
Beyond this point, I could use a for loop but there is only one record for each person.
I am trying to match the user entry with the dataset via the datatable.
The last line should be
DataRow[] results;
results = LoadData().Select("UserID = '" + txtExample.Text +"'");
Supposing that UserID is a field of text type. If instead is of numeric type then remove the quotes
results = LoadData().Select("UserID = " + txtExample.Text);
However I should point that the code in LoadProfile following the daSQL.Fill(ds); call is not needed and you can remove it (just return the DataSet though)
Use the following simple query on dataset:
DataRow[] dRow = dataSetName.Tables[0].Select("fieldToMatch = '" + userInput.ToString() + "' ");

Modified Data Table

For example I have this table
EmployeeName EmpoyeeID
John Mark 60001
Bent Ting 60002
Don Park 60003
How I can show the EmployeeID to have a leading asterisk in data table?
Sample: *60001 *60002 *60003
public DataTable ListOfEmployee()
{
DataSet ds = null;
SqlDataAdapter adapter;
try
{
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("Select * Employee", myDatabaseConnection))
{
ds = new DataSet();
adapter = new SqlDataAdapter(mySqlCommand);
adapter.Fill(ds, "Users");
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0];
}
I need to show the dataTable in the crystal report with a leading asterisk in the employee ID
public void Employees()
{
ReportDocument rptDoc = new ReportDocument();
Employees ds = new Employees(); // .xsd file name
DataTable dt = new DataTable();
// Just set the name of data table
dt.TableName = "Employees";
dt = ListOfEmployee(); //This function is located below this function
ds.Tables[0].Merge(dt);
string strReportName = "Employees.rpt";
string strPath = Application.StartupPath + "\\Reports\\" + strReportName;
// Your .rpt file path will be below
rptDoc.Load(strPath);
//set dataset to the report viewer.
rptDoc.SetDataSource(ds);
ReportViewer newReportViewer = new ReportViewer();
newReportViewer.setReport(rptDoc);
newReportViewer.Show();
}
the easiest not the best way to do is to get the data in that format, if and only if it is not meant to be used anywhere else. You can do this in your query
Select '*'+id,col1,col2,col3 from employee
Though the best way is to modify the column value when you consume it. But obviously that is more headache than adding simply in the query.
Add it into the Crystal report itself.
Something like -
"*" & {tblTable.FieldName}
(Although I can't remember the syntax for Crystal reports, sorry!)
Not tested, but replacing the return statement at the end of your function with the following code should work:
DataTable table = ds.Tables[0];
DataTable clonedTable = table.Clone();
clonedTable.Columns["EmployeeID"].DataType = typeof(String);
foreach (DataRow row in table.Rows)
{
clonedTable.ImportRow(row);
}
foreach (DataRow row in clonedTable.Rows)
{
row["EmployeeID"] = "*" + row["EmployeeID"].ToString();
}
return clonedTable;
However, as others have said, I would recommend adding the asterick somewhere down the line when the data is read rather than to the table itself.

Categories