I have a login page with username and password fields. I want to search an inputted username in my table called 'mytable' and, if this table have such a username, display his data in grid view.
take me linq answer
protected void Button1_Click(object sender, EventArgs e)
{
CostumerDataContext costum = new CostumerDataContext();
LOGIN2 item = new LOGIN2();
var islogin = (from u in costum.LOGIN2s
where u.Username == txtUser.Text
&& u.Passwrod == txtPass.Text select u).ToList();
if (islogin.Count>0)
{
Dgw.Visible = true;
Dgw.DataSource = from u in costum.LOGIN2s select u;
}
else
{
Label3.Visible=true;
}
}
You are selecting data from database on basis of username and password but you are assigning the gridview data of whole table.
You need to bind gridview with filtered data.
var islogin = (from u in costum.LOGIN2s
where u.Username == txtUser.Text.Trim()
&& u.Passwrod == txtPass.Text.Trim() select u).ToList();
if (islogin.Count>0)
{
Dgw.Visible = true;
Dgw.DataSource = islogin;
Dgw.DataBind();
}
else
{
Label3.Visible=true;
}
protected void Button1_Click(object sender, EventArgs e)
{
using (CostumerDataContext costum = new CostumerDataContext ())
{
LOGIN2 item;
item = (from u in costum.LOGIN2s
where u.Username == txtUser.Text
&& u.Passwrod == txtPass.Text select u).FirstOrDefault();
if (item != null)
{
Dgw.Visible = true;
Dgw.DataSource = item;
}
else
{
Label3.Visible=true;
}
}
}
Related
So I have the problem with .net identity model. I created boolean variable IsEnabled in my ApplicationUserManager, trying to make a "block account" method. So it's working fine, anybody with IsEnabled=false cannot login in to my site. The problem is while Im trying to implement administartion method for this variable (Block_Account() for example) I cannot persist my changes in a user. Here is a code;
protected void Block_Click(object sender, EventArgs e)
{
string itemID;
using (GridViewRow row = (GridViewRow)((Button)sender).Parent.Parent)
{
HiddenField lUId = (HiddenField)row.FindControl("ClientId");
itemID = lUId.Value;
}
var user = Context.GetOwinContext().Get<ApplicationDbContext>().Users.Where(a => a.Email == itemID).FirstOrDefault();
//Label1.Text = user.UserName;
if (user.IsEnabled == true || user.IsEnabled == null)
{ user.IsEnabled = false; }
else { user.IsEnabled = true; }
Context.GetOwinContext().Get<ApplicationDbContext>().SaveChanges();
}
And another try
protected void Block_Click(object sender, EventArgs e)
{
string itemID;
using (GridViewRow row = (GridViewRow)((Button)sender).Parent.Parent)
{
HiddenField lUId = (HiddenField)row.FindControl("ClientId");
itemID = lUId.Value;
}
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = manager.FindByEmail(itemID);
Label1.Text = user.UserName;
if (user.IsEnabled == true || user.IsEnabled == null)
{ user.IsEnabled = false; }
else { user.IsEnabled = true; }
manager.Update(user);
Context.GetOwinContext().Get<ApplicationDbContext>().SaveChanges();
}
Both not working actually.
So I got the right value from GridView and find a User with a right email, but after making changes, they wont appear in my database.
I have an aspxgridview, I change displayed text via "CustomColumnDisplayText" event, My problem is when I want to use ASPxGridViewExporter for excel output, one of columns shows wrong data. I don't know how to use ASPxGridViewExporter RenderBrick event.
protected void ASPxGridView1_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDisplayTextEventArgs e)
{
if (e.Column.PropertiesEdit.DisplayFormatString.Equals("Status"))
{
if (Convert.ToInt64(e.Value) == 2)
e.Value = "Successful";
else if (e.GetFieldValue("SaleReferenceId") == null || e.GetFieldValue("SaleReferenceId").ToString() == string.Empty || e.GetFieldValue("SaleReferenceId").ToString().Trim().Equals(""))
e.Value = "Invalid";
else if (e.GetFieldValue("saleOrderId") == null || e.GetFieldValue("saleOrderId").ToString() == string.Empty || e.GetFieldValue("saleOrderId").ToString().Trim().Equals(""))
e.Value = "Invalid";
else
e.Value = "Unsuccessful";
e.DisplayText = e.Value.ToString();
}
}
protected void ASPxGridViewExporter1_RenderBrick(object sender, DevExpress.Web.ASPxGridView.Export.ASPxGridViewExportRenderingEventArgs e)
{
// I don't know how to use it
}
I don't know how but this snippet works fine for me and the result is exactly what I wanted!
protected void ASPxGridViewExporter1_RenderBrick(object sender, DevExpress.Web.ASPxGridView.Export.ASPxGridViewExportRenderingEventArgs e)
{
try
{
GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
if (dataColumn.FieldName == "Status")
{
e.TextValue = "123";
}
}
catch (Exception ex)
{
}
}
Alright, so I have a Gridview and added RowEditing and RowUpdating to it, but it won't really edit something.. This is my code for both:
protected void UserAccounts_RowEditing(object sender, GridViewEditEventArgs e)
{
UserAccounts.EditIndex = e.NewEditIndex;
BindUserAccounts();
}
protected void UserAccounts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = UserAccounts.EditIndex;
GridViewRow row = UserAccounts.Rows[e.RowIndex];
username = UserAccounts.Rows[e.RowIndex].Cells[1].Text;
email = ((TextBox)row.Cells[2].Controls[0]).Text;
MembershipUser user = Membership.GetUser(username);
if (user != null)
{
user.Email = email;
Membership.UpdateUser(user);
ActionStatus.Text = string.Format("User {0} details have been successfully updated!", username);
}
UserAccounts.EditIndex = -1;
BindUserAccounts();
}
What am I doing wrong in here?
EDIT: This is my BindUserAccounts:
private void BindUserAccounts()
{
int totalRecords;
UserAccounts.DataSource = Membership.FindUsersByName(this.UsernameToMatch + "%", this.PageIndex, this.PageSize, out totalRecords);
UserAccounts.DataBind();
bool visitingFirstPage = (this.PageIndex == 0);
lnkFirst.Enabled = !visitingFirstPage;
lnkPrev.Enabled = !visitingFirstPage;
int lastPageIndex = (totalRecords - 1) / this.PageSize;
bool visitingLastPage = (this.PageIndex >= lastPageIndex);
lnkNext.Enabled = !visitingLastPage;
lnkLast.Enabled = !visitingLastPage;
}
i think the should be like this
protected void update_click_foredu(object sender, GridViewUpdateEventArgs e)
{
Label edui = (Label)edugrid.Rows[e.RowIndex].FindControl("label");
TextBox unitxt = (TextBox)edugrid.Rows[e.RowIndex].FindControl("txtuni");
if (unitxt != null && costxt != null && startdatetxt != null && enddatetxt != null)
{
using (Entities1 context = new Entities1())
{
string eduID = edui.Text;
model obj = context.entitytabel.First(x => x.ID == eduID);
obj.Univ_Name = unitxt.Text;
context.SaveChanges();
lblMessage.Text = "Saved successfully.";
edugrid.EditIndex = -1;
bindgrid();
}
}
}
here im using EF like this you can find the control of the text box and save it in gridview
hope this helps you
Somehow it works now after editing the GridView and set "UserName", "IsApproved", "IsLockedOut" and "IsOnline" to ReadOnly="true"
I have been stuck on this and tried a few different things to use the update button to update the record that I selected from the grid view. I have 2 columns in the table Id and Name. I select the record and it populates a text box with the name.... this works fine. I just need to take that same record and update the name from that same text box after it is pulled into the text box by using an update button click event. I have two other buttons that work just fine which are "Add" and "Delete" and I will add that code as well but here is the code:
This is how I have populated the grid view on page load or when I call the method:
private void PopulateCompanyListGrid() //This will populate the grid with the table data on page load
{
IList<Company> companies;
using (var context = new IMSDBContext())
{
companies = context.Companies.ToList();
}
grdvwCompanyList.DataSource = companies;
grdvwCompanyList.DataBind();
}
This is how the grid view is set up:
<asp:GridView runat="server" ID="grdvwCompanyList" OnSelectedIndexChanged="SelectGridRow" DataKeyNames="Id, Name" AllowSorting="True" AutoGenerateSelectButton="True"></asp:GridView>
This is how I put the selected record in the text box:
public void SelectGridRow(object sender, EventArgs e) //This will populate the textbo with the row selected from the gridview
{
GridViewRow name = grdvwCompanyList.SelectedRow;
if (name != null)
{
var dataKey = grdvwCompanyList.DataKeys[name.RowIndex];
if (dataKey != null)
txtCompanyName.Text = (string)dataKey["Name"];
}
}
This is how I am adding records:
protected void btnAdd_Click(object sender, EventArgs e) // This method adds a record to the database
{
if (btnAdd.Text == "Add") // Clears the textbox and notification label and calls method to change name of button if the button says "Add"
{
txtCompanyName.Text = "";
lblCompanyNameNotification.Text = "";
ButtonChangeAddToSave();
}
else if (btnAdd.Text == "Save") // Checks if the button says "Save" and compares textbox and database for a matching record
{
IMSDBContext context = new IMSDBContext();
Company CompanyCheck = context.Companies.SingleOrDefault(Company => Company.Name == txtCompanyName.Text);
if (CompanyCheck != null) // Displays a notification if there is already a matching record
{
lblCompanyNameNotification.Text = "There is already a Company with that name.";
}
else if(txtCompanyName.Text == null)
{
lblCompanyNameNotification.Text = "Please enter a name of a company";
}
else if (txtCompanyName.Text != null) // Write the record to the database if no matching record in the database
{
Company n = new Company();
n.Name = txtCompanyName.Text.ToString();
context.Companies.Add(n);
context.SaveChanges();
txtCompanyName.Text = "";
lblCompanyNameNotification.Text = "";
ButtonChangeSaveToAdd();
}
}
PopulateCompanyListGrid(); // Calls method to repopulate the gridview
}
Add a hidden field in the markup to hold company Id:
<asp:HiddenField ID="hdnCompanyId" runat="server" ></asp:HiddenField>
In the selectGridRow method populate the hidden field with company Id:
public void SelectGridRow(object sender, EventArgs e) //This will populate the textbo with the row selected from the gridview
{
GridViewRow name = grdvwCompanyList.SelectedRow;
if (name != null)
{
var dataKeys = grdvwCompanyList.DataKeys[name.RowIndex];
if (dataKeys["Name"] != null)
txtCompanyName.Text = (string)dataKeys["Name"];
if (dataKeys["Id"] != null)
hdnCompanyId.Value = dataKeys["Id"].ToString();
}
}
In the btnUpdate_Click method get the company by Id and update it :
protected void btnUpdate_Click(object sender, EventArgs e)
{
int companyId;
string companyName = txtCompanyName.Text;
if(int.TryParse(hdnCompanyId.Value, out companyId)){
IMSDBContext context = new IMSDBContext();
Company company = context.Companies.SingleOrDefault(Company => Company.Id == companyId);
if (company != null && txtCompanyName.Text != "")
{
company.Name = companyName;
context.SaveChanges();
}
else
{
lblCompanyNameNotification.Text = "The Company does not exist.";
}
}
PopulateCompanyListGrid(); // Calls method to repopulate the gridview
}
In the main gridview "GridViewTtransmittals" I have the nested gridview with the name "GridViewTranstoCon".When "GridViewTranstoCon" has data, I have no problem, but when there is not any data for that it returns error "A data item was not found in the container", I like it to return null value or have it invisible.
The code is like below:
protected void GridViewTtransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == null)
return;
TransmittaltoConPresentationModel transmittaltoCon = (TransmittaltoConPresentationModel)e.Row.DataItem;
GridView g2 = (GridView)e.Row.FindControl("GridViewTranstoCon");
if (transmittaltoCon.TranstoCons != null)
{
g2.DataSource = transmittaltoCon.TranstoCons;
g2.DataBind();
}
}
The code for data source of main gridview "GridViewTtransmittals" comes of below code:
private void DisplayTransmittals()
{
if (_Transmittals.Any())
{
var query = from transmittalno in _Transmittals
select new TransmittaltoConPresentationModel { TransID = transmittalno.TransID,
Transmittal = transmittalno.TRANSMITTAL, TranstoCons = from doctranstocon in _DocTranstoCons
where doctranstocon == null || transmittalno.TransID == doctranstocon.Transid
select doctranstocon != null ? doctranstocon.tblTranstoCon : null};
GridViewTtransmittals.DataSource = query;
}
else
{
GridViewTtransmittals.DataSource = null;
}
GridViewTtransmittals.DataBind();
}
Try to include this
protected void GridViewTtransmittals_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
}
}