|
My code is working fine for retrieval of 2 records from database after that its not working, no error.
Please help
rotected void btn_nxt_Click(object sender, EventArgs e)
{
i++;
s = er.scalarReturn("select min(q_id) from questions where q_id="+i);
if (s.Equals(""))
{
MessageBox.Show("Quize Over");
btn_nxt.Enabled = false;
}
else
{
i = Convert.ToInt32(s);
lb_qs.Text = er.scalarReturn("select q_title from questions where q_id=" + i);
lb_op1.Text = er.scalarReturn("select optiona from questions where q_id=" + i);
lb_op2.Text = er.scalarReturn("select optionb from questions where q_id=" + i);
lb_op3.Text = er.scalarReturn("select optionc from questions where q_id=" + i);
lb_op4.Text = er.scalarReturn("select optiond from questions where q_id=" + i);
}
}
|
|
|
|
|
First check that i is set to the correct id value you expect.
Second why not get the entire record in the first query and test if the object is null.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
MessageBox.Show will not work in an ASP.NET application. It might appear to work when you're debugging in Visual Studio; but that's only because the server and client are the same computer in that scenario. As soon as you deploy your code to a real server, it will break.
In the best-case scenario, you'll get an exception telling you that you can't display a messagebox from a non-interactive application. In the worst-case, the message will appear on the server, where nobody will ever see it, and your code will hang waiting for a response.
Sehal wrote: er.scalarReturn("select q_title from questions where q_id=" + i)
Whilst you're OK in this specific case (since the parameter is an integer), that code suggests that you've created a library which will force you to write code which is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
Sehal wrote: i++;
It looks like you're storing the current question number in a field on your page. Field values are not persisted between requests. You need to review the ASP.NET Page Lifecycle[^], and understand what's going on "behind the scenes".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well I was going to post a question on Reactjs and why it converted my model class to lowercase which crashed the page. Talk about a head scratchier, trying to figure out if my packages are not setup right, an error in my JSX file, or Bable.
<script>ReactDOM.hydrate(React.createElement(CommentBox, {"initialData":[{"id":1,"author":"Daniel Lo Nigro","text":"Hello ReactJS.NET World!"},{"id":2,"author":"Pete Hunt","text":"This is one comment"},{"id":3,"author":"Jordan Walke","text":"This is *another* comment"}],"url":"/comments","submitUrl":"/comments/new","pollInterval":2000}), document.getElementById("react_0HLF3D2L0PCGB"));
But I figured it out
<script>ReactDOM.hydrate(React.createElement(CommentBox, {"initialData":[{"Id":1,"Author":"Daniel Lo Nigro","Text":"Hello ReactJS.NET World!"},{"Id":2,"Author":"Pete Hunt","Text":"This is one comment"},{"Id":3,"Author":"Jordan Walke","Text":"This is *another* comment"}],"url":"/comments","submitUrl":"/comments/new","pollInterval":2000}), document.getElementById("react_0HLF3DH1L6U8N"));
</script>
Just had to change the NewtonSoft ContractResolver From CamelCase to DefaultContractResolver in in Startup.cs.
At any rate, I have just completed porting my app over, and it was really hard to learn all of these new concepts, while expanding on subjects I never bothered to look deeper into like HttpContext, Session and Cookie.
But what I ended up with is a very clean project with no clutter that is coded without evil bad habits.
I dumped some of the Razor elements for just tags and asp-....., which I might go farther with. Not sure if this is the death or Razor for me, or if I still need to use it.
I'm very happy with .Net Core 2.1 and I dig it. Seems really fast. Now I need to figure out what middleware is, and learn more about the details. Hopefully I server this up on my production web server.
I feel good about this!
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
How to use angular along with ASP.NET mvc project?
Aren't Angular and ASP.NET MVC mutually exclusive?
I mean angular is totally different than asp.net mvc,
here(asp.net) code executes on server and in another(angular) js code executes on client side.
asp.net view and angular view are totally different
=====================================================
The grass is always greener on the other side of the fence
|
|
|
|
|
There's an existing template in visual studio 2017 in .net core that use angular. I suggest you can see that project on how it works as a starting point.
Quote: asp.net view and angular view are totally different
An overview, asp.net view runs on server side and will return html to browser then AngularJS will then inspect the generated html (from server/asp.net) and executes to render and set the behavior accordingly.
[Signature space for sale]
|
|
|
|
|
Nitin Sawant wrote: I mean angular is totally different than asp.net mvc,
here(asp.net) code executes on server and in another(angular) js code executes on client side.
asp.net view and angular view are totally different
True.
Nitin Sawant wrote: How to use angular along with ASP.NET mvc project?
Angular is somewhat a client-side MVC. You can probably mix Angular and ASP.NET MVC but it's not ideal. You could however use ASP.NET Core / ASP.NET Web API with Angular to communicate with your data from the server and if you are really trying to build a SPA application.
modified 5-Jul-18 21:08pm.
|
|
|
|
|
You can probably mix Angular and ASP.NET MVC but its not ideal.
I wonder what is ideal to use with Angular I mean for authentication & db connectivity etc.
=====================================================
The grass is always greener on the other side of the fence
|
|
|
|
|
Quote: I wonder what is ideal to use with Angular I mean for authentication & db connectivity etc.
ASP.NET Core/ Web API as your back-end is the way to go.
|
|
|
|
|
I've been pondering the notion of mixing Angular with .Net MVC as well for about 3 weeks now.
Noticed how I wrote Angular and not AngularJS? AngularJS was the first version, then they changed the name to just Angular.
I just added Angular 5 to my .Net Core 2.1 project, and will write some Angular stuff next week. So I'm moving forward with it and decided to adopt it.
Why?
I questioned why so many software recruiters are looking for the Senior .Net Core 2 and Angular guy, and thought why? There both MVC except one is client and one is server. So I started searching the interwebs for the answer and this is what I found.
Weather.com
At first, I found weather.com, that uses Angular to display the weather for your city for that day. A single page app in which you can move to the next day, or a week, 10 day forecast, and the home page "Wall" of weather related topics. So I get it.
TV Guide
The I went to TV Guide, and saw similiar technology being used for TV Listings. You can hit next and get the listings for the next hour. Makes sense to me here as well.
Navigation
After that I realized that it could be used for menu's and navigation as well. Being able to take a Bootstrap dropdown menu and use Angular to populate departments, categories instead of the menu being static. Or create flyouts ads, and grab an ad using Angular and then animate it.
My thoughts
So MVC has the controller and Models, then you create a limited view, and finish it off with Angular. When I look at .Net Core 2.1, I can see how you can build a limited version of core with a couple View pages and serve up Angular or ReactJS pages instead. I'm looking at the Angular Demo now, and that would be too hard core for me to start with. I choose to use NPM and distribute it with Gulp and bundle it, and will try building another Angular page within a View.
Summary
Well that's as far as I have gotten on the subject. But once again it's another move towards shifting server CPU loads and bandwidth back to the client and use their CPU power and electricity to generate a page. Servers can just send Json formatted data. I'll find out in time if it's worth it or not, plus I have zero debate points to argue in favor of it.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
jkirkerx wrote: notion of mixing Angular with .Net MVC
You probably can mix Angular and ASP.NET MVC, but if you are aiming for SPA, I would suggest to just stick to Angular as your front-end and use ASP.NET Web API /ASP.NET Core Web API as your back-end. Keep in mind that ASP.NET MVC is also front-end but generates pages from the server.
|
|
|
|
|
I'll get to that next week to see what you mean, and if my project build is correct.
But thanks for the heads up on that.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
what are some good open source projects written in angular 4+
with combination of ASP.NET MVC
=====================================================
The grass is always greener on the other side of the fence
|
|
|
|
|
|
This works in Fire Fox, but not in Chrome or Edge on Win 10.
In the Controller, when you login, I write a session value, and then redirect to a page.
On that page, I have a Controller Attribute called Admin Check, that checks for the session value.
I just can't get anything out of the HttpContext. I can on Fire Fox, but not chrome or Edge.
Not sure if it has something to do with HttpContext.Current, or the header, cookie.
I'm scratching my head on this one.
I ran some test, I get the session value in the IActionResult right after I write it.
Tried HttpContext.Items, same thing, so it must be related to HttpContext.
[AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class AdminSecurityCheckAttribute : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = filterContext.Controller as AdminController;
var HttpContext = controller.HttpContext;
if (HttpContext.Session.GetString("Admin_AccountName") != null)
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I changed the cookie policy to none, renamed the Cookie and added IsEssential.
Not sure which one made it work, I'll have to isolate that.
It works on all browsers now, but I know it will come back and haunt me later.
<pre>
services.AddSession(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.Name = ".AspNetCore.Session";
options.Cookie.SecurePolicy = CookieSecurePolicy.None;
options.Cookie.SameSite = SameSiteMode.Lax;
options.IdleTimeout = TimeSpan.FromMinutes(1440);
});
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I have a sql table that I want to loop through and create html links
I currently have :-
DS = x.LoadBlueUploads(mynumber)
Dim dr As DataRow
dt = DS.Tables(0)
For Each dr In dt.Rows
Next dr
I've tried doing it as a label e.g. :-
Label1.Text += "<a href=""~/uploads/" + dr("FormUploadFileName").ToString() + """</a><br />"
But that doesn't seem to work, is there a better way ?
|
|
|
|
|
Try:
DS = x.LoadBlueUploads(mynumber)
Dim sb As New System.Text.StringBuilder()
For Each dr As DataRow In DS.Tables(0).Rows
sb.AppendFormat("<a href=""~/uploads/{0}"">{0}</a><br />", dr("FormUploadFileName"))
Next
Label1.Text = sb.ToString()
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Hi,
I am using Kendo Dropdown list and Kendo Grid, when I select an item, in the dropdown, the Kendo grid should display the records for value of the selected item of the Kendo dropdown, when I change it, the records in the Grid also change. I am trying in the below way, not able to achieve, any help would be very helpful - thanks in advance.
@{
ViewBag.Title = "ListLookups";
//Layout = "~/Views/Shared/_LayoutFinal.cshtml";
}
<style>
.k-edit-form-container {
width: 500px;
}
</style>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="tab-content" id="dvtabs" style="padding-top:10px;">
<label class="col-md-4 control-label">Lookup</label>
@(Html.Kendo().DropDownList().Name("Lookup").OptionLabel("Select")
.DataTextField("Text")
.DataValueField("Value").HtmlAttributes(new { style = "width:280px;", @id = "drpLookup" })
.DataSource(dataSource => dataSource.Read(read => read.Action("GetListOfLookupTables", "Admin")))
.Events(e =>
{
e.Change("GetLookupTableValue");
})
<pre>
)
</div>
<div style="margin-top: 5px; padding-left:3%; padding-right:2%">
<div>
<div>
<h4>List of Contacts</h4>
</div>
<div class="row table-responsive " style="padding-left: 3%">
<input type="hidden" id="LookupId" />
<div id="datalist">
@(Html.Kendo().Grid<DHCS.BH.Provider.Models.LookupTable>()
.Name("LookupGrid")
.EnableCustomBinding(true)
.AutoBind(false)
.Columns(columns =>
{
columns.Bound(p => p.Id).Hidden();
columns.Bound(p => p.Type);
columns.Bound(p => p.Description);
columns.Command(command => { command.Custom("View").Click("ViewLookupTableValues").HtmlAttributes(new { @class = "k-primary" }); }).Width(100);
})
.Pageable()
.Sortable()
.Scrollable()
.Resizable(resize => resize.Columns(true))
.Filterable(x => x.Extra(false).Operators(O => O.ForString(str => str.Clear().StartsWith("Starts With"))))
.HtmlAttributes(new { style = "height:550px;" })
.DataSource(ds => ds
.Ajax()
.PageSize(20)
.Model(model => { model.Id(p => p.Id); })
.Read(read => read.Action("GetLookupValues", "Admin").Data("GetLookupTableValue"))
)
)
</div>
</div>
</div>
</div>
}
<pre>
$(document).ready(function ()
{<br />
var grid = $("#LookupGrid").data("kendoGrid");
grid.dataSource.read();<br />
})
function ViewLookupTableValues(e)
{
var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
var PkContactId = dataItem.Id;
window.location.href = '@Url.Action("GetProgramTypeById", "ProgramType")?id=' + Id + "&Type=View";
}
function GetLookupTableValue(e)
{ return {
LookupTableId: $("#Lookup").kendoDropDownList().val()
}
}
</pre>
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
|
So I have it working where I can get the wwwroot from the controller, but I would like to be able to get it from a class static function that the controller calls.
I’m at the point where I have to decide whether to refactor my static class functions to pass the Environment, or search for a way to access it in the static functions, like for sending email and picking up a HTML template to populate.
This is sort of a dumb question, but is it possible to do?
Or should just rewrite my class functions and pass the Enviroment?
In addition, I need to get my settings in appsettings.json as well, in which I can access them in the view and controller, it not in a class, so the same question applies for that as well.
I’ve searched all over the Internet and It’s just examples of controller and view. On a side question, what’s with the binding options in configureServices in Startup.cs? I don’t have my computer in front of me to give better details about it.
Future Questions:
I just have a few more issues to go, and I should be able to complete my port over to core.
Cookies, core seems to use the single cookie value and not the cookie collection ; in which I posted a question on this earlier.
System.drawing Color, missing in that Nuget Package.
User-Agent
IpAddress
Server.MapPath - what is your work around?
A couple more
Warming;
Any typos or misspelling I will blame on my phone and not me.
I’m actually impressed with how well this works on my phone.
Job well done!
If it ain't broke don't fix it, I broke the rule on this.
Discover my world at jkirkerx.com
modified 29-Jun-18 2:30am.
|
|
|
|
|
Refactor the static methods and pass the IHostingEnvironment to them. Otherwise, they'll be impossible to test.
Similarly with the configuration: pass the IConfiguration (or the strongly-typed configuration object) to the methods.
Not sure what you mean by bindingOptions ?
Multi-value cookies were never standardized, so Core doesn't support them. There are various options for handling multiple values suggested in this StackOverflow thread[^] - the Cookie Manager project[^] looks promising.
Several suggestions for Core image processing libraries on the .NET blog: .NET Core Image Processing | .NET Blog[^]
User agent should be available with: Request.Headers[HeaderNames.UserAgent]
How to get user Browser name ( user-agent ) in Asp.net Core? - Stack Overflow[^]
IP address is available from HttpContext.Connection.RemoteIpAddress - inject the IHttpContextAccessor service to get the current context.
Get Client IP Address in ASP.NET Core 2.0 - Edi Wang[^]
Server.MapPath doesn't really apply any more, since Core doesn't support virtual directories. You just use IHostingEnvironment[^] to get the physical root path, and then Path.Combine[^] to build the path to a folder or file within that path.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Things re much clearer now, thanks!
You gave me the info I needed to push ahead now.
This is the binding thing in ConfigureServices. I was trying to get the Settings and Configuration direct from the static class function and added the 2nd call. The 3rd call was from another online source. I suspect the 2nd and 3rd calls are not needed, but I'll have to comment them out and see what happens to determine that.
services.Configure(Configuration.GetSection("Settings"));
services.Configure(options => Configuration.GetSection("Settings").Bind(options));
services.AddSingleton(Configuration);
As for System.Drawing, I'm using CoreCompat.System.Drawing which works so far, it just have Color, but has Bitmap and stuff like that.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I have 2 issues here on replacing MapPath. After reading many articles on the subject, guess I am the sloppy programmer that used MapPath inside functions to handle physical file locations for image and html file reading and writing.
On the first issue, I seeded my database with data that includes blobs of image bytes and then wrote the bytes to the folder as an image. I'm seeding my database in Program.cs which works fine, it creates the database but I can't grasp the MapPath or Environment.
I noticed in CreateWebHostBulder , if I add .ConfigureAppConfiguration((hostingContext, config) => that I can access the enviroment, so I can pass it to SeedData.Intialize(services, environment); , but then I can't access the DBcontext. But in the code below, I can access the DBContext, but can't figure out how to access the environment.
To the best of my understanding, I'm injecting the environment in startup.cs, which runs after program.cs. I'm lead to believe that I can't use or Initialize the enviroment in the actual SeedData either, so I thought I could pass it along.
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{<br />
var context = services.GetRequiredService();
context.Database.Migrate();
SeedData.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
On issue 2, I just can't seem to understand how create a class for MapPath. I tried this, the code below, but I'm testing on the Database Seed function so I know it's not injected yet because it's injected in startup.cs, and not program.cs.
So I'm flying around in circles, and not really sure how to approach this.
public class ServerPath
{
private IHostingEnvironment _hostingEnvironment;
public ServerPath(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public string MapPath(string path)
{
string filePath = string.Empty;
try
{
filePath = Path.Combine(_hostingEnvironment.WebRootPath, path);
}
catch(Exception e)
{
Console.WriteLine(e.Message.ToString());
}
return filePath;
}
public string CombinePath(string path, string fileName)
{
string filePath = string.Empty;
try
{
filePath = Path.Combine(_hostingEnvironment.WebRootPath, path + "\" + fileName);
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
return filePath;
}
}
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|