Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Thursday, March 29, 2012

newbie - keeping my object data as page refreshes

Hi all

I have some experience in 'Windows' programming but I am new to Web
programming, I wonder if anyone can help.

I have a middle layer object that brings some data from a database class .
The class reprsents a Callout, has lots of methods and properties and
manages the business logic..

I then have a html page (GUI layer) that creates an instance of a callout,
and from the callout object, it displays some textfields, labour etc plus
some datagrids based on some collections it holds.

Everything works fine until the page refreshes, at which time the object
seems to be destroyed and then recreated.

I want to keep the object alive with its existing data, on the server, and
connect back to it , ie. not create a new object each time. I know I could
use my Callouts WriteDate event to persist the data back to the SQLServer,
and then just re load it, but this isn't what I really want. I would like to
keep the object live until all work has been completed.

I have read a few things about sessions states etc, but don't really know
where to start, and how to make the class persist.

initially I had

public partial class CallOutDetail : System.Web.UI.Page
{
ExistingCallout Currentcallout;
int m_CalloutPK = 0;

protected void Page_Load(object sender, EventArgs e)
{
m_CalloutPK = 1212;
Currentcallout = new ExistingCallout(m_CalloutPK);

I have tried putting the Currentcallout in not postback i.e.

public partial class CallOutDetail : System.Web.UI.Page
{
ExistingCallout Currentcallout;
int m_CalloutPK = 0;

protected void Page_Load(object sender, EventArgs e)
{
m_CalloutPK = 1212;
if (!IsPostBack)
{
// first time
Currentcallout = new ExistingCallout(m_CalloutPK);
SetLabour();
SetMaterials();
}

Any pointers would be welcome

thanks

Andycouple options...
both require that it be serialized

you can place it in the Page's viewstate...

private constant vs_callout="A";

protected CalloutType CurrentCallout{
get{return this.ViewState[vs_callout] as CalloutType;}
set{this.ViewState[vs_callout] = value;}
}

...or place it in the session
}

"AAJ" wrote:

Quote:

Originally Posted by

Hi all
>
I have some experience in 'Windows' programming but I am new to Web
programming, I wonder if anyone can help.
>
I have a middle layer object that brings some data from a database class .
The class reprsents a Callout, has lots of methods and properties and
manages the business logic..
>
I then have a html page (GUI layer) that creates an instance of a callout,
and from the callout object, it displays some textfields, labour etc plus
some datagrids based on some collections it holds.
>
Everything works fine until the page refreshes, at which time the object
seems to be destroyed and then recreated.
>
I want to keep the object alive with its existing data, on the server, and
connect back to it , ie. not create a new object each time. I know I could
use my Callouts WriteDate event to persist the data back to the SQLServer,
and then just re load it, but this isn't what I really want. I would like to
keep the object live until all work has been completed.
>
I have read a few things about sessions states etc, but don't really know
where to start, and how to make the class persist.
>
initially I had
>
public partial class CallOutDetail : System.Web.UI.Page
{
ExistingCallout Currentcallout;
int m_CalloutPK = 0;
>
protected void Page_Load(object sender, EventArgs e)
{
m_CalloutPK = 1212;
Currentcallout = new ExistingCallout(m_CalloutPK);
>
>
I have tried putting the Currentcallout in not postback i.e.
>
public partial class CallOutDetail : System.Web.UI.Page
{
ExistingCallout Currentcallout;
int m_CalloutPK = 0;
>
protected void Page_Load(object sender, EventArgs e)
{
m_CalloutPK = 1212;
if (!IsPostBack)
{
// first time
Currentcallout = new ExistingCallout(m_CalloutPK);
SetLabour();
SetMaterials();
}
>
>
Any pointers would be welcome
>
thanks
>
Andy
>
>
>


thanks David

"David Jessee" <DavidJessee@.discussions.microsoft.comwrote in message
news:CF0F5237-8616-4C4B-9DA9-252BA0946C1D@.microsoft.com...

Quote:

Originally Posted by

couple options...
both require that it be serialized
>
you can place it in the Page's viewstate...
>
private constant vs_callout="A";
>
protected CalloutType CurrentCallout{
get{return this.ViewState[vs_callout] as CalloutType;}
set{this.ViewState[vs_callout] = value;}
}
>
...or place it in the session
}
>
>
>
"AAJ" wrote:
>

Quote:

Originally Posted by

>Hi all
>>
>I have some experience in 'Windows' programming but I am new to Web
>programming, I wonder if anyone can help.
>>
>I have a middle layer object that brings some data from a database class
>.
>The class reprsents a Callout, has lots of methods and properties and
>manages the business logic..
>>
>I then have a html page (GUI layer) that creates an instance of a
>callout,
>and from the callout object, it displays some textfields, labour etc plus
>some datagrids based on some collections it holds.
>>
>Everything works fine until the page refreshes, at which time the object
>seems to be destroyed and then recreated.
>>
>I want to keep the object alive with its existing data, on the server,
>and
>connect back to it , ie. not create a new object each time. I know I
>could
>use my Callouts WriteDate event to persist the data back to the
>SQLServer,
>and then just re load it, but this isn't what I really want. I would like
>to
>keep the object live until all work has been completed.
>>
>I have read a few things about sessions states etc, but don't really know
>where to start, and how to make the class persist.
>>
>initially I had
>>
>public partial class CallOutDetail : System.Web.UI.Page
>{
>ExistingCallout Currentcallout;
>int m_CalloutPK = 0;
>>
>protected void Page_Load(object sender, EventArgs e)
>{
>m_CalloutPK = 1212;
>Currentcallout = new ExistingCallout(m_CalloutPK);
>>
>>
>I have tried putting the Currentcallout in not postback i.e.
>>
>public partial class CallOutDetail : System.Web.UI.Page
>{
>ExistingCallout Currentcallout;
>int m_CalloutPK = 0;
>>
>protected void Page_Load(object sender, EventArgs e)
>{
>m_CalloutPK = 1212;
>if (!IsPostBack)
>{
>// first time
>Currentcallout = new ExistingCallout(m_CalloutPK);
>SetLabour();
>SetMaterials();
>}
>>
>>
>Any pointers would be welcome
>>
>thanks
>>
>Andy
>>
>>
>>

Saturday, March 24, 2012

Newbie having problems with a server error

I'm receiving the following server error when debugging my C# code:

"Object reference not set to an instance of an object."

I understand the error but can't see why it's happening. The line it refers to is:

<%Response.Write(Validate.setColour(errorcode[0]));%
The 'Validate' object is an instance of the following class:


using System;

namespace _4Touristweb
{
/// <summary>
/// Summary description for validate.
/// </summary>
public class validate
{
private string outputString;

public validate()
{
//
// TODO: Add constructor logic here
//
}

public string setColour(bool errorcode)
{
if (errorcode)
//set colour to red
outputString = "<DIV style=\"color:"+"#FF0000"+";\">";
else
//set colour to black
outputString = "<DIV style=\"color:"+"#000000"+";\">";

return outputString;
}
}
}

And in the HTML file I have this at the top, which creates the instance of the class.

<%@dotnet.itags.org. Page language="c#" Codebehind="reg2.aspx.cs" AutoEventWireup="false" Inherits="_4Touristweb.WebForm1" %>
<script runat="server" language="C#">
public bool[] errorcode;
public _4Touristweb.validate Validate = new _4Touristweb.validate();
public string businessName;
public string address;
public string district;
public string town;
public string postcode;
public string telephone;
public string fax;
public string county;
public string email;
public string contactName;
public bool terms;
</script
I'm afraid I haven't a clue why the error is coming up.Hi,

In this line <%Response.Write(Validate.setColour(errorcode[0]));%
What is the errorcode[0] value. Seems like there is no value to send to the Validate.setColour() Method.
Thanks for that 'heads up'. I forgot to create an instance of the boolean array in the declarations in the start.

Been a few years since I last did any serious OOP and it's showing.

NEWBIE HELP

Hi All

I have a web form that uses an object class to populate a standard table (not datagrid etc)

I have a button on this form that when clicked opens another form for editing. In this form all the values of the object are used to populate text boxes (in the load event of the form there is a call to a DBHelper class which uses a SQL Parameter Array to execute a strored procedure (SQL) and return the data) and text areas which are free to edit in this form.

Also on this page I have a button called save changes which when clicked I am attempting to create a new paramater array to execute an update procedure again using the DBHelper Class.

Unfortunately when I try to do this the values in the new SQLParamater Array are exactly the same as before and are not reflecting the changes that I make by typing new entries into the text boxes on the form. As a newbie to .net I am lost here and realise that this is probably a scope or state problem but I would like someone to point me in the right direction please. When I hard wire the new SQLParamater Array the update does work so that eliminates the chances of the DBHelper Class from any fault.

Please help as I am going bonkers here!!!!!

Phill

You probably have your "populate form" bit in your Page Load function - which will execute when the save button causes a post back.

Surround your "populate the textboxes" code with:

if (!IsPostBack)
{

...

}

This will mean it only runs when the page is first loaded.


Have you looked if your sql update statement is passing the values as @.new_value

I would recommend doing this on a datagrid, it would be easier to update cuz the datagrid already has an update command field!


I cant use a datagrid as the recordset only ever returns one row with 17 fields. Datagrids need multirows to work dont they?


Datagrids work with "a number" of rows - that can be one row.


welshpunk:

I cant use a datagrid as the recordset only ever returns one row with 17 fields. Datagrids need multirows to work dont they?

Hi welshpunk,

Try to follow those codes as follows:

Working with a Single Table
http://msdn2.microsoft.com/en-us/library/aa719548(VS.71).aspx

Thanks.


Yes but you cannot turn a datagrid on its side. I have 17 fields in the recordset(just one row) Using a datagrid in this fashion is not user friendly as some of the fields are up to 2000 chars long. Datagrids will display like this

Column Heading Column Heading Column Heading Column Heading Column Heading

Data Data Data Data Data

I want to display the data in a more user friendly way like this

Column Header Data

Column Header Data

Column Header Data

Column Header Data

Column Header Data

Column Header Data

Column Header Data

With the 17 column headers on the left and their respective data adjacent to them. This is why I had to create the form in this way.


Hi,

You can write each cell every time by reading each cell once.

Hope this helps.


I have already marked as answer


Don't worry - you'll soon see that comment about marking posts as the answer absolutely everywhere!


You could try using a detailsview. The data would be displayed the way you want.

Thursday, March 22, 2012

newbie hyperlink question

Hi,

Just playing around using the QuickStart site. I placed this code in my code
behind:

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Set hyperlink to "~", which indicates application root.
link1.NavigateUrl = "~"
End Sub

this code in my page:

<asp:hyperlink ID="link1" runat="server">this link test</asp:hyperlink>

The tutorial has me placing the sub in the page itself -- but shouldn't this
work regardless? It displays the text as a link but hovering over it does
nothing.

thanksDo you want the link to go to another page..
What do you want it to do when you hover over it?
Patrick

"thersitz" <thersitz@.gmail.comwrote in message
news:upGZO8sKHHA.4376@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

Hi,
>
Just playing around using the QuickStart site. I placed this code in my


code

Quote:

Originally Posted by

behind:
>
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Set hyperlink to "~", which indicates application root.
link1.NavigateUrl = "~"
End Sub
>
this code in my page:
>
<asp:hyperlink ID="link1" runat="server">this link test</asp:hyperlink>
>
The tutorial has me placing the sub in the page itself -- but shouldn't


this

Quote:

Originally Posted by

work regardless? It displays the text as a link but hovering over it does
nothing.
>
thanks
>
>
>
>
>
>
>


Instead of "~" try "~/" for the navigateurl.

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"thersitz" <thersitz@.gmail.comwrote in message
news:upGZO8sKHHA.4376@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

Hi,
>
Just playing around using the QuickStart site. I placed this code in my
code behind:
>
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Set hyperlink to "~", which indicates application root.
link1.NavigateUrl = "~"
End Sub
>
this code in my page:
>
<asp:hyperlink ID="link1" runat="server">this link test</asp:hyperlink>
>
The tutorial has me placing the sub in the page itself -- but shouldn't
this work regardless? It displays the text as a link but hovering over it
does nothing.
>
thanks
>
>
>
>
>
>
>


thanks.

I originally had the file name -- "default.aspx" as the target -- and a file
by that name in the same directory -- but still the link would not activate
on viewing the page -- the style sheet viewed it as a link and applied the
appropiate style but no link functionality upon hovering over it. I have to
run but will try your suggestion first thing in the morn.

thanks for the response.

"Mark Fitzpatrick" <markfitz@.fitzme.comwrote in message
news:u1HAHXtKHHA.4912@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Instead of "~" try "~/" for the navigateurl.
>
>
--
>
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
>
>
"thersitz" <thersitz@.gmail.comwrote in message
news:upGZO8sKHHA.4376@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>Hi,
>>
>Just playing around using the QuickStart site. I placed this code in my
>code behind:
>>
> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
> ' Set hyperlink to "~", which indicates application root.
> link1.NavigateUrl = "~"
> End Sub
>>
>this code in my page:
>>
><asp:hyperlink ID="link1" runat="server">this link test</asp:hyperlink>
>>
>The tutorial has me placing the sub in the page itself -- but shouldn't
>this work regardless? It displays the text as a link but hovering over it
>does nothing.
>>
>thanks
>>
>>
>>
>>
>>
>>
>>


>
>


Thanks Patrick.

Well, when I hover over it I expected my cursor to change to a hand pointing
cursor and respond as if it were a regular html link and thus allow me to
click and follow the url assigned to it. It does neither. The style sheet
treats it as a link and formats it accordingly. But hovering over it, the
response is the same as hovering over any non-link text.

I have changed the link1,NavigateURL to "default.aspx" with a file so named
in the same directory as the page. I have changed it to "~/" as per another
suggestion made on this thread. And no go.

So I am missing something fairly fundamental. Any ideas. I took this stuff
right off the asp.net tutorial.

"Patrick.O.Ige" <naijacoder@.hotmail.comwrote in message
news:eYk9uStKHHA.5000@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

Do you want the link to go to another page..
What do you want it to do when you hover over it?
Patrick
>
"thersitz" <thersitz@.gmail.comwrote in message
news:upGZO8sKHHA.4376@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>Hi,
>>
>Just playing around using the QuickStart site. I placed this code in my


code

Quote:

Originally Posted by

>behind:
>>
> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
> ' Set hyperlink to "~", which indicates application root.
> link1.NavigateUrl = "~"
> End Sub
>>
>this code in my page:
>>
><asp:hyperlink ID="link1" runat="server">this link test</asp:hyperlink>
>>
>The tutorial has me placing the sub in the page itself -- but shouldn't


this

Quote:

Originally Posted by

>work regardless? It displays the text as a link but hovering over it does
>nothing.
>>
>thanks
>>
>>
>>
>>
>>
>>
>>


>
>

newbie in asp.net

can anybody tell me what is wrong here


public void updateMessage ( object src, EventArgs e )
{
string ServerName = "xxx"; // SQL server name
string Database = "xxx"; // SQL Database Name
string user = "xxx"; // SQL User name
string Pass = "xxx"; // SQL Password

SqlConnection myConn = new SqlConnection("server=" + ServerName + ";database=" + Database + ";UID=" + user + ";PWD=" + Pass + ";");
SqlCommand myCmd = new SqlCommand ( );

SqlDataAdapter myAdapter = new SqlDataAdapter ("select * from cars where ID=" + TheID, myConn );
DataTable msgDetails = new DataTable ( );
myAdapter.Fill ( msgDetails );
DataRowView myRow = msgDetails.DefaultView [ 0 ] ;

SqlDataReader rdr;

if ( Page.IsValid )
{
myCmd.CommandText = "UPDATE cars SET Fastnumer=@dotnet.itags.org.Fastnumer, Tegund=@dotnet.itags.org.Tegund where id=" + TheID;

msgFastnumer.Value = myRow [ "Fastnumer" ].ToString ( );
msgTegund.Value = myRow [ "Tegund" ].ToString ( );

myCmd.Parameters.Add ( "@dotnet.itags.org.Fastnumer", SqlDbType.NVarChar ).Value = msgFastnumer.Value;
myCmd.Parameters.Add ( "@dotnet.itags.org.Tegund", SqlDbType.NVarChar ).Value = msgTegund.Value;

myConn.Open ( );
rdr = myCmd.ExecuteReader ( );
myGrid.DataSource = rdr;
myGrid.DataBind ( );
rdr.Close();
myConn.Close ( );

}
bindPostPanel ( TheID );
}

ExecuteReader: Connection property has not been initialized.
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.InvalidOperationException: ExecuteReader: Connection property has not been initialized.

Source Error:

Line 140:
Line 141: myConn.Open ( );
Line 142: rdr = myCmd.ExecuteReader ( );
Line 143: myGrid.DataSource = rdr;
Line 144: myGrid.DataBind ( );Well, for starters, you need to set the Connection property of your SqlCommand:


myCmd.Connection = myConn;