ASP.NET SqlDataSource bulk update - asp.net

MSDN Library has a walkthrough for bulk updates using a SqlDataSource.
http://msdn.microsoft.com/en-us/library/aa992036(v=VS.90).aspx
Not sure what I'm doing wrong,
but I keep getting an error at:
currentID = Convert.ToInt32(GridDocuments.DataKeys(0).Value)
Are there other resources for SqlDataSource bulk updates?

The line in the example reads:
currentID = Convert.ToInt32(GridView1.DataKeys[r.RowIndex].Value);
This line assumes that you are setting datakeys on your grid, pay special attention to the DataKeyNames attribute in the example: DataKeyNames="EmployeeID"
This example is a little easier to follow. If you would like help debugging the other example please post the error your getting as well.

Related

ASP.NET DetailsView Update exception handling - truncated data

I'm using a DetailsView for updating a record. If the edit input of some fields is too long, the system produces a "data will be truncated" exception.
I can see where I can detect the error in DetailsViewItemUpdating or DetailsViewItemUpdated, and provide a user message. However, I believe the visual feedback should be sufficient for this release, i.e. "hey, it didn't take my 30 characters, even though the header label said it would only allow 20".
Is there a way to force the DetailsView to do the truncation and accept the update?
Or some other approach to this data handling exception, which must be pretty common.
ANSWER: from Ammar Gaffar at EE:
Convert to template field
In EditItemTemplate
Set DataBindings > MaxLength property to desired max length of field
Works fine.

Datetime.Now method in ASP.NET

I am using ASP.NET to provide some variables for the data that has to be added to a SQL database.
Code I am using:
I am using DateTime.Now to select the current time.
How I use it:
I have two pages, one is a page to insert the posts of the users. Other page is used for ajax purpose, to add some text comments to the posts.
In both page I use the same code.
But the code is executing different values. You can have a look here:
In the post the time is saved as "9/1/2013" which means 1st September, 2013! In comment it is saved as Sep 1 2013, which means the same.
My question: how does the code know that the request is an ajax one or the post one. The post page code is wrapped in if(IsPost) { however the comment is an ajax call.
What is the reason behind this?
I found what the issue was.
I had set the column DataType to nvarchar(50) in the database table. After editing it to DataType DateTime I was able to get the same result. So the issue was not the Culture or DateTime. It was the DataType of the column in SQL Server Database.
Assuming that your AJAX call is not changing the format string of your DateTimes, I would assume the threads which process those 2 different requests are operating under different cultures, which would explain why they're being displayed differently (not sure why though, check your settings perhaps). Try the following and you'll see how culture can effect DateTime output:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine (DateTime.Now.ToString());
Thread.CurrentThread.CurrentCulture = new CultureInfo("gu-IN");
Console.WriteLine (DateTime.Now.ToString());
Here's some additional information on CultureInfo.
Code knows from Current thread's Culture information. You should use DateTime.Now.ToString("yyyy-MMM-dd") or any other formate according to your needs to be sure of output.

Newbie question: Need help with ContextTypeName

I was following the Contoso University tutorial on the asp.net website and it all works well. My next step was to replicate some of the things on my own new website. I have added an EntityDataSource that works:
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ConnectionString="name=MyWebsite2011Entities"
DefaultContainerName="MyWebsite2011Entities" EnableFlattening="False"
EntitySetName="product_type">
And according to the tutorial it is a good idea to replace the ConnectionString and DefaultContainerName with a ContextTypeName.
"In the markup for the EntityDataSource control, remove the ConnectionString and DefaultContainerName attributes and replace them with a ContextTypeName="ContosoUniversity.DAL.SchoolEntities" attribute. This is a change you should make every time you create an EntityDataSource control, unless you need to use a connection that is different from the one that's hard-coded in the object context class."
That worked fine for me in the tutorial where they had a:
<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server"
ContextTypeName="ContosoUniversity.DAL.SchoolEntities"
EnableFlattening="False"
EntitySetName="People"
EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>
The difference for me (besides the project name) is that my entity model is not placed in a DAL folder. Instead I accepted the Visual Web Developer's default folder name recommendation. I believe it was "App_Code". But the ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities" doesn't work. When I start the browser, it complains that the type MyWebsite2011.App_Code.MyWebsite2011Entities could not be read.
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities"
EnableFlattening="False" EntitySetName="product_type">
How do I find the correct ContextTypeName to put in? As I said, the ConnectionString and DefaultContainerName worked, so I guess the MyWebsite2011Entities is ok. Any hints would be appreciated because I don't know the naming convention or what to look for.
Open up the .cs file in which the Context is declared, and look at text immediately after the namespace declaration. That's your class's namespace. Your ContextTypeName should be <namespace>.<classname> (without the <> brackets, of course.)
I was following the same Contoso University tutorial but I was using windows Azure as an online Database.
So I also ran into this problem.
For other people interested on how to fix this when using an online database, I did the following.
With your EntityDataSource do:
Remove the following:
ConnectionString=...
DefaultContainerName=...
Add:
OnContextCreating="EntityDataSource_ContextCreating"
Leaving ContextTypeName empty.
And finally implement the following code in the corresponding .cs:
protected void EntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
<ContainerName> context = new <ContainerName>();
e.Context = ((IObjectContextAdapter)context).ObjectContext;
}
ContainerName being your containername ofcourse.
If we would look at the original question then it would be MyWebsite2011Entities.

SqlDataSource erroring when retrieving NVARCHAR(max) column

I'm writing a small ASP .Net application in order to retrieve data from a SQL database. The application uses drop downs in order to select what the next drop down should contain and when a page is selected, it should retrieve the HTML from the database. Everything is working until it gets to the retrival of the HTML data. When I try to retrieve the data, I get:
Microsoft JScript runtime error:
Sys.WebForms.PageRequestManagerServerErrorException:
An unknown error occurred while
processing the request on the server.
The status code returned from the
server was: 500
The HTML column is a defined as NVARCHAR(MAX), but I can't see this causing a problem. The application works if I set the DataValueField to another column. Has one else come across a problem like this? Maybe someone could shine some light on this?
One thing I noted when dealing with varchar(max) columns is that the framework still commonly expects to have a size associated with it. What I ended up having to do was specify the length as -1 to get it to accept a varchar(max) field. Your error message doesn't indicate that this is the problem, but you might try experimenting with it rather than turning off the validation, which could possibly have other repercussions.
Figured it out. Just needed to set ValidateRequest to false at the Page level.

Cannot find typenames ("cannot resolve symbol")

I have a site in ASP.NET using Telerik AJAX controls.
Despite the latest binary being added to the website project, my code (eg myRadGrid.Datasource = "";) shows a red line on type names as they cannot be found for some reason.
Can anyone possibly explain why or have experience in this sort of problem?
Thanks
Why are you assigning an empty string to the radgrid datasource property? Try setting it to null instead, which is what you do if you want to clear the data source...

Resources