|
1st post so go easy!
coding: aspx web pages in vb.net 2.0
database: sql server express
i would like to set session variable values from a database query and i cant find out how to do it!
the page flow would be something like this:
1) query database using 'SELECT userID, firstName, lastName FROM users WHERE userID = 1'
2) if data is returned:
create userID, firstName and lastName sessions from the database values for those columns.
3) if no data is returned
tell the user an error has occurred.
4) finally display the session values or the error message on the page.
i can find thousands of tutorials on how to bind data to controls (GridView, DataList, Repeater etc) but none telling me how to query the database and set sessions, cookies or page variables...
if anyone can shed some light on this, it'd be great!
thanks out there,
jake
|
|
|
|
|
i've made a bit of progress with this problem.
this works, but i'm *pretty* sure that it's not the right way to do it.
this code 'loops' through all the database returns but in my example, there should be only one row returned.
i also cant figure out how to do a (this is in english not code):
if (dataSet is empty) Then<br />
<br />
set a error message<br />
<br />
Else<br />
<br />
create the sessions<br />
<br />
End If
hmmm, tricky.
this is the code i've put together:
Dim conn As SqlConnection = [con string here]<br />
Dim dsProjects As DataSet = New DataSet()<br />
Dim cmd As SqlDataAdapter = New SqlDataAdapter("SELECT userID, firstName, lastName FROM users WHERE userID = 1", conn)<br />
<br />
cmd.Fill(dsProjects, "tblProjects")<br />
<br />
Dim dr As DataRow<br />
Dim dt As DataTable = dsProjects.Tables("tblProjects")<br />
<br />
For Each dr In dt.Rows<br />
<br />
Session("clientID") = dr("clientID")<br />
Session("clientID") = dr("firstName")<br />
Session("clientID") = dr("lastName")<br />
<br />
Next
what do you reckon out there? am i going about this the right or wrong way?!
cheers,
jake
|
|
|
|
|
That's pretty much how I'd do it too, with a couple of points to note...
Firstly, I tend not to use raw SQL in my code, for a variety of reasons....I would use a stored proc in the DB to get the data using a sqlCommand Object
Secondly, theere seems to be a little bit of redundency going on - instead of....
Dim dr As DataRow
Dim dt As DataTable = dsProjects.Tables("tblProjects")
For Each dr In dt.Rows
Session("clientID") = dr("clientID")
Session("clientID") = dr("firstName")
Session("clientID") = dr("lastName")
Next
you know that you're only going to return 1 or zero rows, so the loop is not necessary....so it would become
Dim dr As DataRow
Dim dt As DataTable = dsProjects.Tables("tblProjects")
dr = dt.Rows[0]
Session("clientID") = dr("clientID")
Session("clientID") = dr("firstName")
Session("clientID") = dr("lastName")
which could be further simplified to...
Session("clientId") = dt.Rows[0]["clientId"]
Session("firstName") = dt.Rows[0]["firstName"]
Session("lastName") = dt.Rows[0]["lastName"]
Another alternative would be to take advantage of the fact that session stores objects just fine - so if you wanted to, you could store the DataRow itself, and pull the values out when needed using somehting like :
Session ["theDataRow"] = myDataRow;
.
.
.
DataRow dr = (DataRow)Session["theDataRow"];
it's swings and roundabouts really!
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox
|
|
|
|
|
hi richard,
thank you for your post, it's really helped me out! as you say, it's swings and roundabouts; there's never a '100% this is the only way you do it' solution with programming and it's always good to get a 2nd opinion to make sure your heading in the right direction.
i agree with using stored procedures instead of inline sql but for forum posts, i usually type it out to avoid confusion with column names etc (also handy when someone spots a blunder in the sql!).
everything is now working, the only thing i've left to solve is this:
i currently show the session values back to the page in text box's. as all my asp code in a 'code behind' file, i can response.write a message (or bind it to a label) back if no data is returned sort of like this:
If (dt.Rows.Count = 0) Then
Response.Write("no data returned")
End If
but what i cant figure out is how to not show the text boxs on the page, just the message? i know i could do the same if statement on the page, but that means not using the code behind file which i'm very keen on.
back to google for a bit me thinks!
thanks again for your help,
jake
|
|
|
|
|
I think I get what you mean...
The usual way that I show and hide different content is by using divs. If I follow you correctly, I'd set my page up something like
<table>
<tr>
<td>
<div id="divSuccess" style="width:100%; height:100%; display:none">
<asp:TextBox.........>
<asp:TextBox.........>
</div>
<div id="difFailure" style="width:100%; height:100%; display:none">
<asp:label...........>
</div>
</td>
</tr>
</table>
Then I'd use client scripting and document.getElementById() to set the display to "inline" for the div I wanted to show. If you were determined to do something on the server side, then you could either add a runat=server to the divs and manipulate their styles, or alternatively (and IIRC) an <asp:panel> exposes a Visible property, and renders as a div anyway
If you wanted a simpler method, you could of course just manipulate the visible property of the label and textboxes directly - I just like doing it with divs to keep the markup nice and clear, and so that if you encounter a divSuccess.style.display = 'inline' then it's easier to get what's going on quickly.
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox
|
|
|
|
|
hi richard,
wow, thanks for the reply! finally feeling like i'm getting somewhere with the beast that is asp.net!
the funny thing is it's beginning to remind me of the flash actionscripting i did a while back... interesting.
taking your advice with the div thing to show success/failure i've put the code together as a 'finished' product.
there's some area's i'm still unsure about:
1) cant figure out why i have to create a 'SqlDataAdapter' and then a 'DataTable' and then fill them respectively - the whole thing seems a bit long winded!
2) idealy i would like to show the returned data back to the page without using labels as they create additional code i dont need. for example, the 1st label returns as:
userID = <span id="lblUserID">1</span>
and i dont need the span! not sure how to get round that one...
3) tidying up the objects at the end of the script - which do i need to do?! at the moment, i have this:
dsUsers.Dispose()
dsUsers.Dispose()
myAdapter.Dispose()
dt.Dispose()
conn.Close()
do i need to do it?!
that aside, here's the front end code (had to put on my site as it's too long for the forum):
SettingSessionsFromDatabaseQuery.aspx
and this is the code behind file:
SettingSessionsFromDatabaseQuery.aspx.vb
watcha reckon? any improvements?!
thanks again for all your advice,
jake
|
|
|
|
|
I'm looking for help on my CMS (Sprocket) which I believe is significantly architecturally superior to the god-awful mess that is DotNetNuke. DNN is basically the only major open source CMS for the .Net community (unless you count Rainbow which is basically the C# version of it). There are others, but the only ones I have found are either very simple, have mountains of sprawling ASPX/ASCX clutter to try and maintain, and/or are trying to be another 3-column portal engine (like we don't have enough of those).
Clocking in at over 23,000 lines of code so far, Sprocket boasts features such as a completely free-form templating system that borrows inheritance and polymorphism concepts for extreme versatility, in-built english-language-readable scripting language that can be used anywhere, e.g. in the templates, content, or wherever, friendly URLs, a full AJAX framework, and a bunch of other stuff too.
The CMS is built using C# and ASP.Net 2.0 but WITHOUT all those nasty aspx pages and ascx controls. In fact, there are no ASPX/ASCX files to be found anywhere, as the whole system has been built to bypass that messy stuff and work in a much more unified fashion.
Take a look at www.sprocketcms.com[^] (redirects to the google project hosting site) for a complete feature list and to get access to the open source (BSD license) code base for review. There are several sites running it so far and more in development, but the code base has been 100% written by me and some help would be greatly appreciated to help accelerate things to the point where it can start to be viable competition to DNN.
NATHAN RIDLEY
Web Application Developer
email: snowdevil [@] gmail.com
web/blog: www.sprocketcms.com[ ^]
|
|
|
|
|
So, I have vs2005 on Vista, and trying to make a website. Start a new project, select HTTP, type in the URL to my server (for now, running Windows 2003 inside a VMWare machine, but will get a new machine later). VS asks me for login credentials, I supply those of an admin (not administrator, but my user that has admin rights) on the W2k3 machine, and the projects gets copeid over fine. Now,when I try to debug it from vs2005, it can't. I'm getting all sorts of bizarre erors. If on my vista machine im logged in as a power user, then i am told that remote debuggins tools are not running on W2k3 (they are, I installed it and started it). If running as user with admin priveleges on vista machine, i am told "access denied". In both cases, I _am_ asked for login credentials and suply those of admin user on W2k3 machine.
I guess I should also add that there is no domain controller here, so each machine has its own credentials.
Now, has anyone had this kind of thing happen to them? How do I battle this? Its not exactly fun having to debug on the builtin asp.net server of vs2005, then copy files over to W2k3 machine and find out it doesnt work on IIS due to different permissions.
Thanks in advance.
|
|
|
|
|
This[^] post has some tips that you may find useful.
From the above link: If you are debugging from a remote machine, you will need to run the Visual Studio Debugging Monitor on the remote machine, and open the firewall so that Visual Studio can connect to the debugger (msvsmon.exe does this for you by default).
Hope that's of use.
|
|
|
|
|
thanks, I'll look into it. though i am running iis in w2k3 server, but should still help. cant check now as I'm on a diferent machine.
|
|
|
|
|
Hi,
I have a simple form with a textbox and a drop down list on it. I've used an ASP.NET (2.0) ddl control and an ASP.NET button for submitting the form. I have an event handler on the submit button, btnSubmit_Click() which triggers when the submit button is clicked.
When I try and access the ddl's value in the event handler, it's always set to the original value it was when the page was loaded. In other words, it ignores whatever the user changes it too before clicking "Submit".
Here's my aspx:
<form id="frmTest" runat="server" action="test.aspx" method="post">
<asp:DropDownList ID="ddlColor" runat="server">
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem Value="Amber">Amber</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
</asp:DropDownList>
<asp:button id="btnSubmit" text="Submit!" onClick="btnSubmit_Click" runat="server" />
</form>
Here's the code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
String str = ddlColor.SelectedItem.Text;
}
Do I need to call anything to get the changed value of the DDL before I look at the <code>ddlColor.SelectedItem.Text</code> property?
|
|
|
|
|
Hi,
No need to call anything to get the changed value of the DDL.
|
|
|
|
|
Give if not page.ispostback then cmd....,
or change the auto postback properties in ddl,
Magesh
|
|
|
|
|
I have a data grid in ASP.NET 2.0 that contains the following fields:
ID
Task
Comments
User
The User field is populated with the login name of the user who created the item. How can I filter this dataset with a Page_Load event based upon the users login name?
GT
|
|
|
|
|
If you want to disply only a particular user's items only in the datagrid, you will have to create a different datasource for that. For example, if you are using dataset or datatable as datasource, you will have to create a dataview and filter the particular user id. Then bind the datagrid with the dataview as datasource
hope this will help
|
|
|
|
|
Hi,
Firstly You must have a dataset on your hand code behind.
You can write a method like this,
protected void Page_Load(object sender, EventArgs e)
{
if{!IsPostBack}
{
querystring = "Your query";
GetData(String queryString);
yourdatagrid.DataBind;
}
}
DataSet GetData(String queryString)
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(queryString, SqlConnection);
adapter.Fill(ds);
return ds;
}
I hope I understand you correctly.
|
|
|
|
|
Well let me see if I can explain a little better.
I want to somehow take the current users login name and filter a table with that string.
GT
|
|
|
|
|
Yourdatasource.Filter = "User = " + Session["LoginName"].ToString;
My last answer!
|
|
|
|
|
The grids expose a RowFilter property, which can be set to something like
myGrid.RowFilter = "UserName=" + userNameToFilterBy;
myGrid.DataBind();
"Now I guess I'll sit back and watch people misinterpret what I just said......"
Christian Graus At The Soapbox
|
|
|
|
|
For a project I need a rather advanced deployment scenario. I want to deploy my asp.net web app to the server just once and use multiple virtual directories that share the binaries but have the aspx files separate where needed. I know microsoft does the same thing with sharepoint, but I couldn't find any information to achieve the same effect.
Anyone know of a way to achieve this?
WM.
What about weapons of mass-construction?
"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
|
|
|
|
|
I've been experimenting with some settings and found out that you can build something like the following:
- Web1 - C:\AppInstance
- MasterPages (VDir) -- C:\Tenants\Tenant1\MasterPages
- App_Themes (VDir) -- C:\Tenants\Tenant1\App_Themes
- Web2 - C:\AppInstance
- MasterPages (VDir) -- C:\Tenants\Tenant2\MasterPages
- App_Themes (VDir) -- C:\Tenants\Tenant2\App_Themes
Which is exactly what I wanted. Now to get the database to do something like that too.
WM.
What about weapons of mass-construction?
"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
|
|
|
|
|
With LAN connection to my IIS computer, it's possible for others to access this IIS server too?
I have developed my asp.net 2.0 with vb2005 and sqlserver 2005 on WIndows XP. I have created a virtual directory from IIS mapped to teh website folder.
I want to test this site on a Local Area Network(Workgroup).
I tried something like this from one of the pc on the network by typing in internet explorer
http://192.168.1.5/ASPLearning/Default.aspx
where 192.168.1.5 is the IP address of the IIS Computer
ASPLearning is the website Name as given in IIS
The page did not display.
Please is there anything i need to do? pls help
t.aransiola
|
|
|
|
|
Yes !
Can you ping the IIS server from the client machine or see the front page. Is the security on the Virtual Directory set to anyonous (easy for me to spell )
Grady Booch: I told Google to their face...what you need is some serious adult supervision. (2007 Turing lecture)
http:\\www.frankkerrigan.com
|
|
|
|
|
Hi guys, I always wonder where to get all the reference and detailed info about javascript. I have a general knowledge about javascript but when I found javascript codes on different advanced sites they all have Hi-Fi syntax. For instance, divs[i].className=='popupcontainer'. I use VS2005 but it provides basic properties and methods. In addition, how can I be sure what methods and properties the javascript object takes. Unlike in VS.Net for C# or VB, we can see all the properties by just typing "."/ period and it tells what does that method do.
Nevertheless, I have seen a javacript in 24 days but that talks about general stuff. Can sombody tells me where can I found all the javascipt detailed info about methods and properties.
Thanks
|
|
|
|
|
Try "Pure Javascript" (Sams Publishing) by Wyke, Gilliam and Ting. I bought it 6 years ago and it's still useful. It is a true reference. The first few chapters cover the basics, then the rest of the book is a reference with every object with each method and property. Also, each object, method and property has an example and a notes which browsers support it with version numbers.
I love it. Whenever I have a question about any javascript I go there first.
|
|
|
|
|