asp.net,MVC,Sql Question
Friday, April 1, 2016
Wednesday, December 30, 2015
How to upload a file in an ASP.Net MVC
step_1
file id uploadFile
step_2 [HttpPost]
public ActionResult ProfilePicture()
{
HttpPostedFileBase photo = Request.Files["uploadFile"];
string uploadedPath = "Content\\images\\profileimages\\";
string sFileDir = System.Web.HttpContext.Current.Server.MapPath("~") + uploadedPath;
if (photo.ContentLength > 0)
{
string sFileName = System.IO.Path.GetFileName(photo.FileName);
photo.SaveAs(sFileDir + sFileName);
}
}
step_3 in Request.Files["uploadFile"];
and enjoy
Tuesday, December 22, 2015
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I have found error on execute this code
DbContext.GL_tbl_Addresses.Attach(Address);
DbContext.Entry(Address).State = Address.userid == 0 ? EntityState.Added : EntityState.Modified;
DbContext.SaveChanges();
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
the best solution replace Address.userid with GL_tbl_Addresses table primary id eg ID DbContext.Entry(Address).State = Address.Id == 0 ? EntityState.Added : EntityState.Modified; and run your code.
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
the best solution replace Address.userid with GL_tbl_Addresses table primary id eg ID DbContext.Entry(Address).State = Address.Id == 0 ? EntityState.Added : EntityState.Modified; and run your code.
@html.dropdownlistfor set selected value
public class ContactInformation { public string State { get; set; } public IEnumerable
step (2) @Html.DropDownListFor(model => model.State, Model.StateList, "--Select--", new { @class = "form-control" })
step (3) IEnumerable
ContactInformation.StateList = listState.Select(l => new SelectListItem { Selected = (l.Value == ContactInformation.State), Text = l.Text, Value = l.Value });
and run your project.
Sunday, December 12, 2010
Sunday, March 7, 2010
Code of GridviewEvent
Hello Every one This is code for Fill DropDownList in GridView with dynamic values,Add New Records from GridView control,Edit and Update in GridView,Delete in GridView
Pre-requisites Your project or website must be ASP.NET AJAX enabled website. Because we are going to add the GridView in an UpdatePanel. So your GridView control will be look smart without unnecessary postbacks. You need to create a Customer Table with 6 columns for Customer Code[Code], Name[Name], Gender[Gender], City[City], State[State] and Customer Type[Type], with your desired data types. Then create a class file in your App_Code folder and create a Default.aspx along with code-behind file Default.aspx.cs. |
| ||||||
Step 1. Create Class File ‘CustomersCls.cs’
We need to create a class file to do database manipulations such as select, insert, delete and update data in the Customer Table. So we add a class file as ‘CustomersCls.cs’ in App_Code section. Let us write five methods in the class file as follows |
| ||||||
|
| ||||||
Step 2: Make Design File ‘Default.aspx’
In the Default.aspx page, add an UpdatePanel control. Inside the UpdatePanel, add a GridView, set AutoGenerateColumns as False. Change the ShowFooter Flag to True and set the DataKeyNames your column name for Customer Code and Customer Type, in our case it is Code and Type. Then click on the Smart Navigation Tag of the GridView control, choose Add New Column and add 5 BoundField columns with DataField values as Name, Gender, City, State and Type, plus 2 CommandField columns with one for Edit/Update and another for Delete functions. Now your GridView control is ready. But as first step, we need to add some new records into the database. For that we need to place the controls in the Footer row. So we have to convert all these BoundField columns as TemplateField columns. To do this again, click on the Smart Navigation Tag on the GridView choose Edit Columns, the Field’s property window will open. Select column by column from Name to Customer Type, include also Edit column, and select ‘Convert this field into a TemplateField’. Now all the BoundField columns will be converted to TemplateField columns except the Delete column. |
| ||||||
|
| ||||||
Column[4] - Type
In this column’s EditItemTemplate section, replace the TextBox with a DropDownList, name it as cmbType. Also add another DropDownList in the FooterTemplate section and name it as cmbNewType. Both these DropDownList’s we are going to fill with dynamic data from database. So specify both DropDownList’s DataTextField and DataValueField as Type. Column[5] - Edit Just add a link button into the FooterTemplate section, specify its CommandName property as ‘AddNew’. For your persual, we have provided the complete source code of the GridView control below. The State column in our sample is read-only. So you cannot find TextBox for that column in the EditItemTemplate section. |
| ||||||
Click here to view Source Code of the GridView Control [Hide..] |
| ||||||
|
| ||||||
Step 3: Make Code-behind File ‘Default.aspx.cs’ Now we are going to do the code-behind part of this page. Les us explain you event by event coding on each methods. In the code-behind page, create an instance for the Customer class as follows
Then create a private method 'FillCustomerInGrid' to retrieve the existing customer list from the database and bind it to the GridView. The CustomersCls class’s Fetch() method is used and it returns the data to a DataTable. On first stage it will return empty rows. So you cannot see any header, data or even footer rows of the GridView control. You can only see an empty space or you see only the EmptyDataText. So you cannot add any new data from the footer row.
In this article, we have provided a workaround to fix this problem. Closely look at the method FillCustomerInGrid, there is a conditional statement to check the rows exists in DataTable or not. Now go to the else part of the if statement, see the block of code we provided there. Simply we have added an empty row to the DataTable. Then bind it to the GridView control. To give a professional look to the GridView control, we do little bit more by providing ColumnSpan and set a Text as "No Record Found", this text will be displayed if the GridView is empty without any rows and you can see both the Header and Footer of the GridView control. Initialize GridView control In the page load event, we have to call this FillCustomerInGrid method as follows,
Fill DropDownList in GridView with dynamic values In column[4] - Type, there are two DropDownList controls, one in the EditItemTemplate section (cmbType) and another in FooterTemplate (cmbNewType). We have to fill both these DropDownList controls with some dynamic data. If you look at our CustomersCls class, we have a separate method called FetchCustomerType. In the RowDataBound event of the GridView control insert the following code.
Previously in this article, we have set the DataKeyNames values as Code, Type. If you see in the above code, we use one of the DataKeyNames value as the SelectedValue for the cmbType control, this is to retain the value of the cmbType in EditMode. The index value of Code is 0 and Type is 1. So we use as follows
So far we have initialized the GridView control with the datatable and also make some values to be filled in the Footer DropDownList cmbNewType. Run the application, you can see the GridView only with the Footer row and data in the cmbNewType control. Let us start to code for adding new records into the database when we click ‘Add New’ linkbutton. Add New Records from GridView control Create an event for the GridView’s RowCommand and add the following code in it.
In the above code, we are declaring and finding the controls in the GridView’s footer section and use the CustomersCls class insert method to add the new data into the database. Then we are calling the FillCustomerInGrid method to fill the GridView control with the newly inserted values. Now save everything and run your application. Put some test data in the Textboxes and select some values in the DropDownLists and click on the Add New linkbutton. You can see data inserted into the database and listed in the GridView control. |
| ||||||
|
| ||||||
You can update the data to the customer table, by adding the following lines of code in the GridView’s RowUpdating event.
The above block of codes in RowUpdating event, finds the control in the GridView, takes those values in pass it to the CustomersCls class Update method. The first parameter GridView1.DataKeys[e.RowIndex].Values[0].ToString() will return the Code of the Customer. That is the unique code for each customer to perform update function. Delete in GridView To delete a row from the customer table, add the following lines of code in the GridView’s RowDeleting event. Here you have to pass the unique Code of customer which is in GridView1.DataKeys[e.RowIndex].Values[0].ToString() to the Delete method of the CustomersCls class.
|
Saturday, March 6, 2010
Subscribe to:
Posts (Atom)