Monday, March 26, 2012
Newbie ASP project
I have just developed a quite large app in VB.NET that integrates with a SQL dbase. I have now been asked to develop a very simple app in ASP, this would take me a day or so in VB.NET, but I'm gonna struggle in ASP.NET.
This project is a questionnaire that will be completed by users, via a webpage, this will then write back to a SQL database. There are 50 questions, these are always the same, the answers are multi-choice.
I'm thinking this is an easy task, but time is against me ! Has anybody got any code examples, or is there a better way to do this ! If anybody could point out the key differences between coding in ASP.NET and VB.NET it would be appreciated !
Cheers
BobIf you know VB.NET you shouldn't have too much trouble transfering over to ASP.NET. It's all very similar. The thing that ofter confuses people though is the whole client server relationship. Your ASP code will run on the server ONLY, it compiles to HTLM and some Script. So things like mouse over, timers etc don't work. Also your page is reloaded on every postback. Thus global var's don't retain there info, you need to use session var's instead. You also need to reload any buttons etc you've added and addhandler for them again. www.asp.net has a lot of good info.
TPM
Newbie datagrid woes
Hi Guys, I am fairly new to .net but have found a lot of the stuff I am attempting is working out as expected. I have however hit a snag with an updateable datagrid. I have spent the last two days searching forums and google for an answer but nothing I do seems to work.
My db is SQL 2000 with 3 columns movieID (Int, Pri) movieTitle(Varchar) movieCategory(int,4 used as a relation to categories table)
Any advice offered will be gratefully received as I am totally at my wits end
The error occurs on datagrid update and I get the following error
Specified cast 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: Specified cast is not valid.
Source Error:
Line 104: Dim CategoryID As String = CType(e.Item.FindControl("movieCategory"), DropDownList).SelectedItem.Value
Line 105:
Line 106: Dim MovieTitle As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Line 108:
I am using vs.net 2003 my code is below
Update function
Public Sub OnUpdate(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
Dim CategoryID As String = CType(e.Item.FindControl("movieCategory"), DropDownList).SelectedItem.ValueDim MovieTitle As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
' form the update statement
Dim cmd As String = "Update tblMovies set MovieTitle = " & MovieTitle & "MovieCategory = " & CategoryID & "where movieID = " & DataGrid1.DataKeys(e.Item.ItemIndex)' connect to database
Dim connectionString As String = Global.dbConnectDim connection As New SqlConnection(connectionString)
connection.Open()' call the update and rebind the datagrid
Dim command As New SqlCommand
command.CommandText = cmd
command.Connection = connection
command.ExecuteNonQuery()DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
Datagrid tag
You cannot directly cast a objects like that into a string object - try something like this:
<asp:DataGrid
id="DataGrid1"
runat="server"
AutoGenerateColumns="False"
CellPadding="5"
HeaderStyle-BackColor="PapayaWhip"
BorderWidth="5px"
BorderColor="#000099"
AlternatingItemStyle-BackColor="LightGrey"
HeaderStyle-Font-Bold="True"
EditItemStyle-BackColor="Yellow"
EditItemStyle-ForeColor="Black"
DataKeyField ="movieID"
OnEditCommand="OnEdit"
OnCancelCommand="OnCancel"
OnUpdateCommand="OnUpdate">
<Columns
<asp:BoundColumn DataField="MovieID" HeaderText="ID" ReadOnly="True" />
<asp:TemplateColumn HeaderText="Movie">
<ItemTemplate>
<asp:Label
Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"MovieTitle")) %>'
Runat="server" ID="lblProduct"/>
</ItemTemplate
<EditItemTemplate>
<asp:TextBox ID="MovieTitle" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"MovieTitle") %>'/>
</EditItemTemplate
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<asp:Label
Text='<%# Convert.ToString(DataBinder.Eval(Container.DataItem,"MovieCategory")) %>'
Runat="server" ID="Label1"/>
</ItemTemplate
<EditItemTemplate>
<asp:DropDownList
Runat="server"
ID="movieCategory"
DataSource='<%# GetValues("Category") %>'
DataTextField ="MovieCategory"
DataValueField ="id"
Width ="200" />
</EditItemTemplate
</asp:TemplateColumn
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Save" /
</Columns>
</asp:DataGrid>
Dim CategoryID As String
Dim MovieCategoryDDL as dropdownlist
MovieCategoryDDL = e.item.findcontrol("moviecategory")
CategoryID = MovieCategoryDDL.selecteditem.value
Dim MovieTitle As String
Dim myTextBox as textbox
myTextBox = e.item.findcontrol("MovieTitle")
MovieTitle = myTextBox.text
Saturday, March 24, 2012
Newbie here need some help..
Basically do you have any advice on how i should go about it
Thanks
Zim
This is possible there are 2 ways of doing so.
A) Create an administration section that has a few pages to modify the prices / items/ personel information or whatnot
B) Integrate a CMS (content management solution) that will modify the xml or html or aspx directly and save it.
If you are interested in B Telerik RAD has a nice set of controls , one of them being a cms solution. If you want a cost effective solution
get a good book on ado.net look at the update/delete/ insert and create a simple admin section with a login.
Good luck with your site.
Friday, March 16, 2012
Newbie needs help with Rounding Time
I need the current Military time (ex 906 equals 9:06) rounded to the nearest ten. So I need ten minute increments. Instead of 906 I need 910, and instead of 903 I need 900. I've tried the using the System.Math.Round function but it only takes decimals and it doesn't round whole numbers.
Any help would be greatly appreciated!!
Thanks,
Chris
Hi Chris,
Here's a start. This code rounds military time to the nearest ten. Modify the last line if you need the output in a different format.
string timeStr = "2259";
timeStr = timeStr.Insert(timeStr.Length-2,":");
DateTime nowTime = Convert.ToDateTime(timeStr);
decimal dTimeMin = Convert.ToDecimal(nowTime.Minute);
decimal dMinRound = dTimeMin / 10;
dMinRound = System.Math.Round(dMinRound);
dMinRound = dMinRound * 10;
double diffVal = (int)dMinRound - (int)dTimeMin;
DateTime myResult = nowTime.AddMinutes(diffVal);
Response.Write(myResult.TimeOfDay.ToString());
Hope this helps.
-- Erik Reitan
ASP.NET User Education
This posting is provided "AS IS" with no warranties, and confers no rights.