The background is I have a custom control that is a asp:Menu that is linked to an xmldatasource. The xmldatasource is created dynamically depending on the user privies. Here is the load event for the custom control:
protected void Page_Load(object sender, EventArgs e)
{
string userId = (string)Session["userId"];
if (userId != null)
{
DataSet ds = dal.RetrieveApplications(userId);
ds.DataSetName = "Menus";
ds.Tables[0].TableName = "Menu";
DataRelation relation = new DataRelation
("ParentChild",
ds.Tables["Menu"].Columns["Folder_Id"],
ds.Tables["Menu"].Columns["Parent_Folder_ID"], true);
relation.Nested = true;
ds.Relations.Add(relation);
xmlDataSource1.Data = ds.GetXml();
}
}
This works perfectly for the first user that uses it. But it seems that every subsequent user is actually getting the first user's menu. I have walked it through and verified that my dataset is getting created fine and when I examine the XMLDatasource.data at the end of the load the xml is correct.
I am really stuck.
I found the answer and though I would leave it out here for others who might search for this:
But I had to set the "ENABLECACHING" to false on the xmldatasource.
Related
Goal:
My goal is to format 1 column to password characters *.
Resources I have tried:
I have used an example from here :
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.cellformatting?view=netframework-4.8
and tried this answer here:
https://stackoverflow.com/a/38074239/12485722
This is my current code to load DataGrid View:
private void Form3_Load(object sender, EventArgs e)
{
RefreshGrid();
}
//Refresh Data Grid from external Forms
public void RefreshGrid()
{
// MySQL connection string
using (var conn = new MySqlConnection(ConnectionString()))
{
using (var mySqlDataAdapter = new MySqlDataAdapter("select * from user", conn))
{
using (var dataSet = new DataSet())
{
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);
dataGridView1.DataSource = DS.Tables[0];
}
}
}
}
This simply loads up the DataGridView with Usernames and passwords.
Now I need to format 2nd column to password characters, so the passwords do not show as text but formatted as *.
I have tried adding this piece of code from the 2nd resource mentioned above:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1 && e.Value != null)
{
e.Value = new String('*', e.Value.ToString().Length);
}
}
This does not work - no errors show up, but I have noticed this references is set to 0.
(Note: this is what it should be)
Question:
I am confused where it is going wrong.. can somebody assist me to achieve this task?
Update (1) - Events and cell formatting not showing:
Your method is not wired to an event of GridView (thats why it has 0 references, because nothing is using your function).
The solution in the mentioned link is correct, but you should wire your method to an event in designer (right click on grid view > properties > events) and choose an event to fire up in CellFormatting event. Also dont forget to handle EditingControlShowing, but everything what you need is already written in a solution thread that you mentioned.
Why don't you modify your query to just say the below:
select username, repeat('*', length(password)) from users
I have two pages. On the first one I have two drop down lists and button like this:
Code for this button is:
protected void btnIzabraniProizvodi_Click(object sender, EventArgs e)
{
Session["Id_Dobavljaca"] = ddlDobavljaci.SelectedValue;
Session["Id_Kategorija"] = ddlKategorija.SelectedValue;
Response.Redirect("IzabraniProizvodi.aspx");
}
When I click on this button the secont page opens.
This two sessions are input parameters for the SQL query. Here is the code on the second page:
protected void Page_Load(object sender, EventArgs e)
{
string idDobavljaca = Session["Id_Dobavljaca"].ToString();
string idKategorija = Session["Id_Kategorija"].ToString();
string konekcioniString = ConfigurationManager.ConnectionStrings["moja_konekcija"].ConnectionString;
using (SqlConnection sqlKonekcija = new SqlConnection(konekcioniString))
{
SqlDataAdapter sqlDA = new SqlDataAdapter("spVratiIzabraneProizvode", sqlKonekcija);
sqlDA.SelectCommand.Parameters.AddWithValue("#Id_dobavljaca", idDobavljaca);
sqlDA.SelectCommand.Parameters.AddWithValue("#Id_kategorija", idKategorija);
sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
sqlDA.Fill(ds);
ds.Tables[0].TableName = "IzabraniProizvodi";
gridView.DataSource = ds.Tables["IzabraniProizvodi"];
gridView.DataBind();
}
}
My question is, when this dataSet is empty how can I get some message on the first page below the button: "No information for this values, try again with different values"? Any idea?
There is no way to do this normally and you have two not good ways:
You can use child and parent page. The second page will be the child of first page and data will send from child to parent by javascript. but the problem is that this does not work in chrome for security reasons.
The second way is to check automatically from first page by ajax method in periods of times.
setInterval(function(){ AJAX-CHECK }, 5000)
If you want each one of those senarios i will more explain.
I am new to using the telerik tools for winforms and I was wondering if anyone could help me to find the best way so that the client can add a new row on the radgrid and for it to show this change on the data source.
so far I have the radgrid set up so that the client can add a new row. I just need to bind it to the data source.
private void radGridView1_Click(object sender, EventArgs e)
{
this.radGridView1.AllowEditRow = false;
this.radGridView1.AllowAddNewRow = true;
this.radGridView1.AllowDeleteRow = false;
this.radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;
this.radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
radGridView1.EnableAlternatingRowColor = true;
}
Have a look at the UserAddedRow event for RadGridView. This is fired after the user added a new row to the grid. You could then add the new entries to a source list or data table.
List<Foo> _lSource = new List<Foo>();
DataTable _tSource = new DataTable();
private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
{
Foo foo = new Foo();
foo.Col1 = e.Row.Cells["col1"].Value.ToString();
foo.Col2 = e.Row.Cells["col2"].Value.ToString();
_lSource.Add(foo);
_tSource.Rows.Add(e.Row.Cells["col1"].Value.ToString(), e.Row.Cells["col2"].Value.ToString());
}
I added both possibilities in the same code snippet. Just choose one (list or table). I just created a random class to show you an example. You can create your own classes and name the properties as you want. Just note that the column (col1 and col2 in my example) must exist, otherwise you'll get a NullReferenceException. If you're using DataTable you have to add the columns once before adding rows.
_tSource.Columns.Add("col1");
_tSource.Columns.Add("col2");
I also recommend not to update the RadGridView properties on a click event of RadGridView. Because it is enough to set those properties once. Now you set them every time you click in your grid view. Either move them to the Load event of your form or set it directly in designer properties.
private void Form_Load(object sender, EventArgs e)
{
radGridView1.AllowEditRow = false;
radGridView1.AllowAddNewRow = true;
radGridView1.AllowDeleteRow = false;
radGridView1.AddNewRowPosition = Telerik.WinControls.UI.SystemRowPosition.Top;
radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
radGridView1.EnableAlternatingRowColor = true;
}
RadGridView supports data binding to variety of data sources and CRUD operation will be handled for you automatically. Here you can find information on the supported data sources: Telerik UI for WinForms Documentation
And here is how to bind to a DataTable, where all CRUD operations work out of the box
RadGridView radGridView1 = new RadGridView();
this.Controls.Add(radGridView1);
radGridView1.Dock = DockStyle.Fill;
DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Rows.Add("value1", "value1");
table.Rows.Add("value2", "value2");
radGridView1.DataSource = table;
Here is also a design time tutorial.
I cant populate grid view with the data sent to api. tell me where is the flaw. I have no idea.
protected void btnGo_Click(object sender, EventArgs e)
{
WSmyWebAPI.myWeb wsAPI = new WSmyWebAPI.myWeb();
WSmyWebAPI.OrderSearchParameters sSearchParameters = new
WSmyWebAPI.OrderSearchParameters();
DataSet ds = new DataSet();
DateTime dtmStartDate;
DateTime dtmEndDate;
dtmStartDate = Convert.ToDateTime(dpApproved.Text);
dtmEndDate = Convert.ToDateTime(dpShipped.Text);
oid = txtOrderID.Text;
opid = txtPartID.Text;
sSearchParameters.StartDate = dtmStartDate;
sSearchParameters.EndDate = dtmEndDate;
sSearchParameters.OrderID = oid;
sSearchParameters.OrderPartID = opid;
ds = wsAPI.OrderSearch(sSearchParameters);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Following is a sample code. You may manipulate or adjust your code accordingly.
Make sure you have aspx code in place as well. That means the columns you want to display. Try to add
GridView1.Vissible = True
Further make sure to add a try catch block and check on SqlException. Try to go on line by line debug mode. I would suggest to display the error on a label in the webpage until your entire page is bug free. So you know where and what went wrong.
As for a suggestion, it's better to take out the connection , query parameter portion out Go click event. Add a separate method to do that. Only get the data set or data table (I recommend data table) and set it to gridview under Go click event.
I have one stubborn data grid view is refusing to display the bound data.
i placed a grid view named exhibitgridview and set its datasource to none. then i added a standalone data source that can return columns into the grid but first there data displayed in the grid would be based on a what gets selected from a dropdown list. check it out from the picture below.
So basically some item is selected from the dropdown list next to the caseid label and the grid displays values accordingly... AS such i needed a selectedIndexchanged method so i had this in my page.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
CreateDataSet();
caseID = DropDownList1.SelectedItem.Value.Trim();
DataView exhibitDataView = new DataView(exhibitDataSet.Tables[0]);
exhibitDataView.RowFilter = "FilingID = '" + caseID + "' ";
ExhibitGridView.DataSource = exhibitDataView;
ExhibitGridView.DataBind();
}
private void CreateDataSet()
{
exhibitConnection.ConnectionString =
ExhibitListSqlDataSource.ConnectionString;
exhibitSqlDataAdapter.SelectCommand = new
SqlCommand(ExhibitListSqlDataSource.SelectCommand, exhibitConnection);
exhibitSqlDataAdapter.Fill(exhibitDataSet);
}
The code runs sweet...I inserted a breakpoint as to ensure some data is actually returned for binding and there is...you can see that from the screen shot below:
that was until (ExhibitGridView.DataBind()). So when i run the next block, i expect the data to bind and display in the browser but for some unknown reason the gridview is acting stubborn. i tried specifying the datasource directly and it displays successfully at pageload but otherwise it wouldn't respond.
What could be the cause?
I do believe you need to supply your DataAdapter with the parameters that you are supplying your select statement with. Take a look.
I have given you an example from my code which uses OleDB (I have removed all the open / close connection for ease of reading). They are VERY similar.
SqlCmd = "select * from App_Details WHERE App_Name LIKE #Var";
aCommand = new OleDbCommand(SqlCmd, aConnection);
aCommand.Parameters.AddWithValue("#Var", value);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SqlCmd, aConnection);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter);
// Now I do not see this part in your code right before you bind your data
dataAdapter.SelectCommand.Parameters.AddWithValue("#Var", value);
DataTable table = new DataTable();
dataAdapter.Fill(table);
dgvSearchApp.DataSource = table;
Make sure about Post Back events. Maybe the page is doing two post backs.