I have wrote an application which is connected to an SQL server. There is a function which list out one of the table's rows in a DataGridView. This DataGridView is bound to a BindingSource which is connected to the table directly via a DataSet. It has been clicked via GUI.
My problem is, when something changes in the table, the DataSource is not refreshing. Not even I edit something via my application or just delete a row it still stands there even if the SQL table does not contains anymore.
I have wrote a refresh procedure which have been called every time I click on the table view menu or use the refresh button, it looks like that:
public void SrvMgmtRefresh()
{
string select = "select * from ServersInput";
srvMgmtDataGridView.DataSource = null;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(select, con);
DataSet DS = new DataSet();
sqlDataAdapter.Fill(DS, "ServersInput");
srvMgmtDataGridView.DataSource = DS.Tables[0];
}
Does anyone have any idea what I have did wrong? I see in the application that it reload the content of the table but it still not refresh.
Thanks in advance.
Try this :
DataSet ds = new DataSet();
using (SqlDataAdapter sda = new SqlDataAdapter("ProcedureName", _context.Database.GetConnectionString()))
{
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.SelectCommand.Parameters.Add(new
SqlParameter("#isMedicationPerscriber", SqlDbType.Bit)).Value =
filteringParams.IsMedicationPerscriber;
sda.SelectCommand.Parameters.Add(new
SqlParameter("#isInpersonSession", SqlDbType.Bit)).Value =
filteringParams.InpersonSession;
sda.Fill(ds);
}
int totalRecords = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
var items = ds.Tables[0].AsEnumerable()
.Select(dataRow => new User
{
ZipCode = dataRow.Field<string>("ZipCode"),
IsDeleted = dataRow.Field<bool>("IsDeleted"),
IsConnected = dataRow.Field<bool>("IsConnected"),
Latitude = dataRow.Field<double?>("Latitude"),
Longitude = dataRow.Field<double?>("Longitude"),
}).Where(x => x.UserId != Guid.Parse(filteringParams.UserId)).ToList();
Related
The following C# code runs a DAX statement and retrieves a DataTable. This works fine, but now I need to retrieve from the database up to N rows. Is there a way to limit the number of rows returned by the Fill function? If not, how can I retrieve the top N rows? Note that I need to keep this generic for any DAX statement, so you shouldn't change the DAX itself. Also, I don't want to retrieve all the data and then take the first N rows as the data may be too large.
public static DataTable runDaxStatement(int maxRows) {
var con = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
AdomdConnection conn = new AdomdConnection(con);
DataSet ds = new DataSet();
ds.EnforceConstraints = false;
AdomdCommand cmd = new AdomdCommand("evaluate customers", conn);
AdomdDataAdapter da = new AdomdDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
Came across the following TOPN function in the documentation.
This can be used to return the top N rows of the specified table.
For example
public static DataTable runDaxStatement(int maxRows) {
var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
using(AdomdConnection connection = new AdomdConnection(connectionString)) {
string commandText = $"EVALUATE TOPN({maxRows}, customers, <orderBy_expression_here>)";
AdomdCommand command = connection.CreateCommand();
command.CommandText = commandText;
DataSet dataSet = new DataSet(){
EnforceConstraints = false
}
AdomdDataAdapter adapter = new AdomdDataAdapter(command);
adapter.Fill(dataSet);
return dataSet.Tables[0];
}
}
I have a datatable which currently displays all the records from a database table. I am using asp.net and ado.net. The table is populated by a stored procedure which is simply select * from table. When my view loads the table gets populated with all records. I want the table to display only related records at a time until those records in the table have been authorised (accepted or rejected and then records are inserted to other tables) or a 'Next' button is clicked. These records would be grouped by ID. What ID that shows up first when the view loads just depends on what the first record ID is.
To give an example of data and how I want it displayed:
All records:
PageID AID Name
1 60 Book1
2 60 Book7
3 50 Book7
4 60 Book8
5 70 Book12
6 70 Book7
The first ID is 60(AID) so I would want it to loop through the column, find all that are AID60, display them in the view, once all have been actioned, it would move to AID50 etc etc...
PageID AID Name
1 60 Book1
2 60 Book7
4 60 Book8
What I have currently in the Controller to get the records is:
public ActionResult toAction()
{
SqlConnection con = new SqlConnection();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstringpath"].ToString());
List<ActionModel> AList = new List<ActionModel>();
SqlCommand com = new SqlCommand("Action", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
foreach (DataRow dtRow in dt.Rows)
{
string CurrentAID = dtRow["AID"].ToString();
if (dtRow["AID"].ToString().Equals(CurrentAID))
{
AList = (from DataRow dr in dt.Rows
select new ActionModel()
{
PageID = Convert.ToInt32(dr["PageID"]),
AID = Convert.ToInt32(dr["AID"]),
Name = Convert.ToString(dr["Name"])
}).ToList();
}
}
return View(AList);
}
I am aware the the above code is incorrect as the foreach loop does not work. The idea would be to
Get the AID of the first record in the table, loop through the rest of the column looking for the same AID and add all records to the list to display to the view. Once actioned in the view the next AID will populate. (I have the Accept/Reject/Next buttons already)
I just need to get the foreach working correctly to only show out one AID at a time. I will not be passing an AID as a parameter so the likes of Select * from Action where AID = #AID will not work.
public ActionModel toAction()
{
SqlConnection con = new SqlConnection();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstringpath"].ToString());
SqlCommand com = new SqlCommand("Action", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
return dt.Select("AID = 60").ToList<ActionModel>();
}
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.
I want to display data into the gridview from the database
But Currently there is no data into the table but it still showing the records from the table.
Below is my code:-
protected void DisplayGrid()
{
OracleCommand cmd = new OracleCommand("Select * from XXACL_PN_FLAT_STATUS_HIS", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}
and on page_load
DisplayGrid();
I tried from here its related to Session but I dont have any session right now.
Also I cross checked the database name, Table name and connection string
UPDATE
I tried with Open and close like below but still it didn't worked
try
{
ObjPriCon.Open();
OracleCommand cmd = new OracleCommand("Select * from XXACL_PN_FLAT_STATUS_HIS", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}
catch (Exception ex)
{
}
finally
{
ObjPriCon.Close();
}
Finally after a research of many things, I manage to got the solution.
Actually in Toad.
After executing the select query in the Oracle, the next step what I thought of was COMMITING after I executed my select query.
So when I checked after commiting, It worked for me.
I have a DataGridView that I'm trying to pull data to from an SQLite database.
The structure of the DataGridView is:
TaskName : String
TaskDueDate : String
Status : ComboBox
Code:
String SelectQuery = "Select TaskName,TaskDue,Status from TASKS Order BY Status";
SQLiteConnection slite = new SQLiteConnection("data source = Dash.sqlite");
slite.Open();
SQLiteDataAdapter data = new SQLiteDataAdapter(SelectQuery, slite);
SQLiteCommandBuilder Command = new SQLiteCommandBuilder(data);
DataTable table = new DataTable();
data.Fill(table);
MyTasksGrid.AutoGenerateColumns = false;
MyTasksGrid.DataSource = table;
I've spent the past few hours on here looking at several question including this one,
however it was producing the exact same results for me as the code above.
This question also suggests my code is correct from the accepted answer?
I get the correct amount of rows showing up in the grid, but all the cells are blank. If I remove the AutoGeneratColumns line it adds the data but in new columns, which I don't want. (I want it to be added in the already defined columns.)
Could anyone give me a hand to see where I am going wrong?
String SelectQuery = "Select ID,TaskName,TaskDue,Status from TASKS Order BY Status";
SQLiteConnection slite = new SQLiteConnection("data source = SupportDash.sqlite");
slite.Open();
SQLiteDataAdapter data = new SQLiteDataAdapter(SelectQuery, slite);
SQLiteCommandBuilder Command = new SQLiteCommandBuilder(data);
var Bind = new BindingSource();
DataTable table = new DataTable();
Bind.DataSource = table;
MyTasksGrid.AutoGenerateColumns = false;
MyTasksGrid.DataSource = table;
MyTasksGrid.DataSource = Bind;
MyTasksGrid.Refresh();
data.Fill(table);
I also needed to add the 'DataPropertyName' in the control properties.