Extract data from SQL Server database - asp.net

I've connected to database, using data set .xsr now I wan't to extract all rows in my table. Here is my table structure :
Lastname
Address
zipcode
city
country
email
So here is one example of table row entry :
Danish Morten Olsen 58b 82341 Kobenhavn Denmark email#email.com
Now I'd like for someone to tell me how can I extract this info for all entries in the table in format :
<tr>
<td>Lastname</td>
<td>Address</td>
<td>zipcode</td>
<td>city</td>
<td>country</td>
<td>email</td>
</tr>
where of course one html table row represents one table row in database, I'm a newbie just two days ago installed necessary tools, database and started programming asp <% %>(asp noob yes, programming noob no) .. this is important so please answer if you can spare time .
thank you

Standard ASP.NET Webforms (which is what I guess you'll be using) doesn't give you total control over your HTML - you're not programming directly in HTML and markup, but against an abstracted "webform" model. E.g. you use and create server-side objects like data source (to grab your data), a gridview etc. - and the server control then really renders out the HTML to be sent back to the customer's browser.
So you might have a SQL data source to reach into your SQL Server table and grab the data:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString2 %>"
SelectCommand="SELECT [Lastname], [Address], [zipcode], [city],
[country], [email] FROM [Addresses]">
</asp:SqlDataSource>
and then hook that up to a data-bound ListView on your ASP.NET form:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<span style="background-color: #E0FFFF;color: #333333;">Lastname:
<asp:Label ID="LastnameLabel" runat="server" Text='<%# Eval("Lastname") %>' />
<br />
Address: <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' /><br />
zipcode: <asp:Label ID="zipcodeLabel" runat="server" Text='<%# Eval("zipcode") %>' /><br />
city: <asp:Label ID="cityLabel" runat="server" Text='<%# Eval("city") %>' /><br />
country: <asp:Label ID="countryLabel" runat="server" Text='<%# Eval("country") %>' /><br />
email: <asp:Label ID="emailLabel" runat="server" Text='<%# Eval("email") %>' />
<br /><br /></span>
</ItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<LayoutTemplate>
<div ID="itemPlaceholderContainer" runat="server"
style="font-family: Verdana, Arial, Helvetica, sans-serif;">
<span ID="itemPlaceholder" runat="server" />
</div>
<div style="text-align: center;background-color: #5D7B9D;
font-family: Verdana, Arial, Helvetica, sans-serif;color:#FFFFFF;">
</div>
</LayoutTemplate>
</asp:ListView>
Check out the official ASP.NET website which has plenty of articles, tutorials, screen casts and demos on how to get started on ASP.NET webform development.
If you want total control over your markup, you should check out ASP.NET MVC which is a new concept for ASP.NET developers, but which might be a lot more along the lines of what you already know from PHP programming. Here, you do have total control over your HTML and every bit and byte being sent back to the browser.
Marc

Have a look at: http://www.learnasp.com/freebook/asp/dbsimple.aspx
If you've just started learning ASP, you should rather start having a look at ASP.NET.

I never use asp but I did same thing in JSP, I just came across the link
http://www.powerasp.com/content/database/using_select.asp
This would probably help your using asp.

Your post led me to believe you were coding in Classic ASP, as opposed to ASP.NET. I recommend that you get started by watching some of the videos over on asp.net/learn. Start with this one on how to get data out of the database using a DataSet and Gridview by creating a data access layer (DAL) in your app. Optionally, you could read the HTML version of the tutorial.

Related

TextBox and button in panel control are not vissble in design ..?

i have taken one panel control in asp.net in that having one text box control and one button control ,when i open design the text box and button are not visible,when i run this i can see in the controls in browser
<asp:Panel ID="Panel1" runat="server">
<td align="center" colspan="4">
<asp:Label ID="Label20" runat="server" Style="font-weight: 700" Text="search by phone number : " ></asp:Label><asp:TextBox ID="txtphno" CssClass="ip" runat="server" placeholder="Phone Number"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="Red" ControlToValidate="txtphno" Font-Size="Large" ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtphno" ForeColor="Red" runat="server" ErrorMessage="Only Numbers" ValidationExpression="\d+"></asp:RegularExpressionValidator>
<asp:Button ID="btnSearchphno" runat="server" CssClass="inputbtn" OnClick="btnSearchphno_Click" Style="font-weight: 700; height: 26px;" Text="search" />
</td>
</asp:Panel
this is the source code what i have written please help me . .
thanks
teja
Couple of points to get u started
1) Clear Your project solution and rebuild your project
2) Restart your Visual studio instance and open your project freshly
After you clear project and reopen visual studio, if problem still continue, try delete the designer and regenerate it.

Insert.aspx.vb - Set focus to control in dynamic FormView

I am having a bit of trouble with a dynamic asp.net FormView (within Insert.aspx.vb) in my ASP.NET LINQ to SQL Website. Here is the code for the FormView...
<asp:FormView runat="server" ID="FormView1" DataSourceID="DetailsDataSource" DefaultMode="Insert"
OnItemCommand="FormView1_ItemCommand" OnItemInserted="FormView1_ItemInserted" RenderOuterTable="false">
<InsertItemTemplate>
<table id="detailsTable" class="DDDetailsTable" cellpadding="6">
<asp:DynamicEntity runat="server" Mode="Insert" />
<tr class="td">
<td colspan="2">
<asp:LinkButton runat="server" CommandName="Insert" Text="Insert" />
<asp:LinkButton runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>
When this form is displayed in a browser, there are 5 user input controls, and two buttons (insert/cancel). But here in the code, I do not see any reference to the 5 user input controls, unless they are represented by this line of code...
<asp:DynamicEntity runat="server" Mode="Insert" />
First, am I correct in assuming that this line of code builds the user input controls dynamically?
And, my actual problem -- I need to set the Focus to the first of these input's upon page initialization. Basically when someone chooses to insert a new item and is sent to this page, I want them to be able to start typing right away, without the need to actually click in that textbox to begin. Any help would be greatly appreciated?
I was able to resolve my issue by adding a Search field via Code, and then using this in the Page_Load event...
If table.DisplayName = "Employees" Then
MultiSearchFieldSet.Visible = True
txbMultiColumnSearch.Focus()

Updating Database with value from datepicker control ASP.NET VB

Apologies in advance, for what may be a newbie question. (new to asp.net, coding in VB, zero knowledge of best controls). Please respect that I am not interested in AJAX controls, or using MVC and trying to minimise javascript. What I need to do is very simple in terms of technology.
I am developing a form that allows users to Edit database data. I have chosen to use FormView so that I can format it like a legacy vba app. I am able to set the data source to my database table and the values from the database successfully show up if I allow VWD to automatically format the control. I can edit everything in the database using this form as well.
However, the boss hates having to type dates.
I haven't been able to find a datepicker control with a datasource feature that works like the text boxes that are automatically built in formview (with a bind to a datasource feature). So, I am assuming none exist. I need some assurances that what I think I need to do is the right way.
Instead of using the FormView control's datasource via the front-end automagical stuff, I should instead
place one of these datapicker controls that simply combine calendar with a text box for all date fields in the formview (no disrespect to those who built them, just can't believe they are not more feature-rich, this seems so needed giving the number of datepickers available)
declare my data source in Page_Load and load all the controls if they have existing data utilising FindControl
use Data_Bound block to retrieve the selected values from each control utilising FindControl and build a dynamic SQL string for Update
declare and update my database using one of the code behind blocks, perhaps in the DataBound
Am I on the right track? I have no experience to be confident in my assumption.
and please, if there is an easier way, I'll take it, but I have to make it "pretty".
And suggestions for controls of any sort are welcomed.
---Further into my issue
Here is some code to prove I've actually tried to resolve this...
In my FormView code block I have:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="MASTERID_Action"
DataSourceID="srcAction">
<EditItemTemplate>
MASTERID_Action:
<asp:Label ID="MASTERID_ActionLabel1" runat="server"
Text='<%# Eval("MASTERID_Action") %>' />
<br />
Action_Description:
<asp:TextBox ID="Action_DescriptionTextBox" runat="server"
Text='<%# Bind("Action_Description") %>' />
<br />
Action_Target:
<asp:TextBox ID="Action_TargetTextBox" runat="server"
Text='<%# Bind("Action_Target") %>' />
<br />
Action_Captured:
<asp:TextBox ID="Action_CapturedTextBox" runat="server"
Text='<%# Bind("Action_Captured") %>' />
<br />
Action_Declined:
<asp:TextBox ID="Action_DeclinedTextBox" runat="server"
Text='<%# Bind("Action_Declined") %>' />
<br />
Action_AgreedDate:
<ewc:CalendarPopup ID="CalendarPopup1" runat="server" Culture="en-AU"
PostedDate="" SelectedDate='<%# Bind("Action_AgreedDate") %>'
SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
VisibleDate="01/14/2013 08:47:54" />
<br />
...
</EditItemTemplate>
My database holds this Action_AgreedDate as nullable.
When I view the ItemTemplate (in the browser) the date shows up as 0.000 (because its a text field and bound to Action_AgreedDate, no error occurs) and when I click Edit to go to the EditItemTemplate I get this error:
Conversion from type 'DBNull' to type 'Date' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.
Source Error:
Line 95:
Line 96: Action_AgreedDate:
Line 97: '
Line 99: SelectedValue="01/14/2013 08:47:54" UpperBoundDate="12/31/9999 23:59:59"
I can easily translate this into "the control found a null field and doesn't know what to do with it"; problem is, I don't know what to do. I have checked the properties of the field (the CalendarPOP field to see if there is a setting for handling nulls and nothing is obvious to me. I'm currently trying to find further documentation on the control online. (I've contacted eWorld and hope they will be able to respond.)
I should also add that if I request a record that already has an Action_AgreedDate I get no errors because there is a value present in the database for the control to display.
I think the best way for you to do this would be to use jQuery and jQuery UI, which has a date picker, which can be attached to a regular ASP.NET TextBox in your FormView. So if you had FormView code that looks something like this for example:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1">
<EditItemTemplate>
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:TextBox ID="date_modifiedTextBox" runat="server" Text='<%# Bind("date_modified") %>' />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("solution_id") %>' />
<br />
date_modified:
<asp:Label ID="date_modifiedLabel" runat="server" Text='<%# Bind("date_modified") %>' />
</ItemTemplate>
</asp:FormView>
You could run something like this using jQuery UI to get a datepicker on the date_modified text box:
$('[id$=date_modifiedTextBox]').datepicker()
The $('[id$=date_modifiedTextBox]') part is necessary to select the ASP.NET control using jQuery, if it were just a normal element you could just use $('.className') or $('#id') or something like that. Hope this helps.
Problem solved.
I ran across an article on 4GuysFromRolla.com about the RJS POP Calendar (http://preview.tinyurl.com/cerm9xv) and it ticked all the boxes. Successfully implemented it and, because the calendar is separate from the specified text box, it doesn't have to be bound ! It works very nicely. The 4GuysFromRolla save my bacon yet again...

Error On Postback While Uploading a File

I have an ASP.NET 4.0 web application where users upload videos to the server. I have a FileUpload control and 2 DropDownLists.
The user first selects a video to be uploaded from the FileUploadcontrol after that s/he selects a category from DropDownList1 (category list). After the user selects a category, I fill the second DropDownList with subcategories.
When I select a file to upload, and select a category from the DropDownList, the page disconnects from the server after postback. If I do the same scenario without selecting a file to be uploaded, I succesfully fill the second combo.
Here is my code :
<tr>
<td style="text-align: left;" class="style9" colspan="2">
<asp:Label ID="Label1" runat="server" Text="Video" Width="80px"></asp:Label>
<asp:FileUpload ID="FileUploadVideo" runat="server" ViewStateMode="Enabled" />
</td>
<td style="text-align: left;" class="style4">
<asp:Label ID="Label3" runat="server" Text="Category" Width="80px"></asp:Label>
<br />
<asp:DropDownList ID="cmbCategory" runat="server" AutoPostBack="True" OnSelectedIndexChanged="cmbCategory_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td style="text-align: right;">
<asp:Label ID="Label6" runat="server" Text="Subcategory" Width="80px"></asp:Label>
<br />
<asp:DropDownList ID="cmbSubcategory" runat="server">
</asp:DropDownList>
</td>
</tr>
Any help would be appreciated.
Since you're uploading video, I imagine this is erroring out due to the file size. The default max file size for ASP.NET applications is 4MB. You can add something like this to the <system.web> section of your web.config to increase that deault:
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
This allows, for instance, for a 20MB file to be uploaded.
For more information check out this article: Large file uploads in ASP.NET

Issue with getting a ModalPopUp to display a populated textbox correctly

My site has descriptions of the products we sell and I am currently trying to get those descriptions to pop up inside a modal popupextender. I can get them to popup with a textbox where I have enabled="false" so that it can't be edited, but it shows all the tags along with the text such as the <p>, <li>, etc. Is there a different control I can use inside of my modal that will show my text better?
<!-- Descriptions -->
<asp:TabPanel ID="tab2" runat="server" HeaderText="Descriptions">
<HeaderTemplate>Descriptions</HeaderTemplate>
<ContentTemplate>
<ul class="info">
<asp:ListView ID="lvDescriptions" runat="server" DataSourceID="dsMarketingDescriptions" DataKeyNames="MarketingID">
<ItemTemplate>
<li>
<asp:LinkButton ID="ViewDescriptionButton" runat="server"><%#Eval("MarketingTitle")%></asp:LinkButton>
<asp:ImageButton ID="DeleteDescriptionButton" runat="server" Style="float:right;" AlternateText="" ImageUrl="../../images/delete.png" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this description?')" />
<asp:Panel ID="ViewDescriptionPanel" runat="server">
<asp:ImageButton ID="CancelDescriptionButton" runat="server" ImageUrl="../../images/exit.png" AlternateText="" Style="float:right;"/>
<asp:TextBox ID="Description" runat="server" width="100px" height="100px" Enabled="false" TextMode="MultiLine" Text='<%# Eval("MarketingData") %>'/>
</asp:Panel>
<asp:ModalPopupExtender ID="ViewDescriptionModal" runat="server" BackgroundCssClass="modalBackground" DropShadow="false" DynamicServicePath="" Enabled="true" PopupControlID="ViewDescriptionPanel" TargetControlID="ViewDescriptionButton" CancelControlID="CancelDescriptionButton"></asp:ModalPopupExtender>
</li>
</ItemTemplate>
</asp:ListView>
</ul>
</ContentTemplate>
</asp:TabPanel>
Here's an example of the <%# Eval("MarketingData")%>:
<p>If you are currently trading options on futures or are interested in exploring them further, check out this newly updated trading guide, featuring 25 commonly used options strategies, including butterflies, straddles, strangles, backspreads and conversions. Each strategy includes an illustration demonstrating the effect of time decay on the total option premium involved in the position.</p><p>Options on futures rank among the most versatile risk management tools, and are offered on most CME Group products. Whether you trade options for purposes of hedging or speculating, you can limit your risk to the amount you paid up-front for the option while maintaining your exposure to beneficial price movements.</p>
If the text wont be editable, why don't you use a asp:label instead?
Does the MarketingData field include html tags? Please provide an example of the value for MarketingData.
Edit:
If you know wich tags can be present on your text, you can simply create a property on the class that hold the propoerty called MarketingData, like this:
public string EditedMarketingData
{
get {return MarketingData.Replace("<p>","").Replace("</p>","").Replace("<li>","");}
}
And then change the binding in your text box as:
Text='<%# Eval("EditedMarketingData") %>'
The replacing is kind of ugly, hope you can get that to look better ;)
I think a Literal would be the most appropriate control for this type of content, as it will preserve the HTML in the content. You could probably also use a Label.
If you would prefer to stick with a TextBox though, I would try setting the ReadOnly property to false (instead of Enabled). If you want to achieve the disabled.

Resources