Timer countdown sql, asp.net c#

bside2011
bside2011 used Ask the Experts™
on
i have the next javascript code at my try.aspx file:
<input type="hidden" id="auctionEndDate" value="<%=TargetDate%>"> 
<script language="JavaScript"> 
     
 
    TargetDate = "document.forms[0].TargetDate.value"; 
    /*this is a property in code behind*/BackColor = "palegreen"; 
    ForeColor = "navy"; 
    CountActive = true; 
    CountStepper = -1; 
    LeadingZero = true; 
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; 
    FinishMessage = "the auction end" 
    </script> 
    <script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script> 
    </script>

Open in new window


code behind:
public string TargetDate () 
{    
    String tDate = "";  
    DataTable dataTable = new DataTable();  
    using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))  
    using (SqlCommand command = connection.CreateCommand())    
    {        
        command.CommandText = "select * from endTime";      
        connection.Open();       
        SqlDataReader reader = command.ExecuteReader();     
        dataTable.Load(reader);     
        tDate = dataTable.Rows[0][0].ToString(); // Expecting the first row and first column 
    } 
    return tDate; 
}

Open in new window


but i got that error:
CS1502: The best overloaded method match for 'System.IO.TextWriter.Write(char)' has some invalid arguments

for the line:
<input type="hidden" id="auctionEndDate" value="<%=TargetDate%>">

what i'm trying to do is to build timer countdown for item, and the endtime coming from sql database. maybe all the way is wrong, i'll be glad to get others way to do that, or fix the error's on the code that i post here.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Have you debuged the TargetDate method to see if it returned what you would expect? you should really put that code in a try{}catch{} block. You could also use execute scalar you wouldn't need the data reader then or the datatable.

public string TargetDate () 
{    
    string tDate = "";  
     using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))  
    using (SqlCommand command = connection.CreateCommand())    
    {        
        command.CommandText = "select * from endTime";      
        try
        {
           connection.Open();       
           tDate = (string)command.ExecuteScalar(); // Expecting the first row and first column
        }
        catch(Exception ex)
       {
          //handle exception here
       }
       finally
       {
          connection.Close();
       }
    } 
    return tDate; 
} 

Open in new window

Commented:
Also I wouldn't have exposed that method, I would have put it as a private method, set the hidden input to runat the server and set the value in the page_load method. Making it look something like this (not tested -sorry):

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostBack) //<-- up to you if you use this or not just habit for me
     {
         auctionEndDate.value = TargetDate();
     }
}

private string TargetDate () 
{    
    string tDate = "";  
     using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))  
    using (SqlCommand command = connection.CreateCommand())    
    {        
        command.CommandText = "select * from endTime";      
        try
        {
           connection.Open();       
           tDate = (string)command.ExecuteScalar(); // Expecting the first row and first column
        }
        catch(Exception ex)
       {
          //handle exception here
          //Response.Redirect("to and error page maybe?.aspx");
       }
       finally
       {
          connection.Close();
       }
    } 
    return tDate; 
}  

Open in new window


<input type="hidden" id="auctionEndDate" runat="server"> 
<script language="JavaScript"> 
     
 
    TargetDate = "document.forms[0].TargetDate.value"; 
    /*this is a property in code behind*/BackColor = "palegreen"; 
    ForeColor = "navy"; 
    CountActive = true; 
    CountStepper = -1; 
    LeadingZero = true; 
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; 
    FinishMessage = "the auction end" 
    </script> 
    <script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script> 
    </script>

Open in new window

Author

Commented:
thank you gavsmith, but that's not working.

 i got error: auctionEndDate.value = TargetDate();
(the name 'auctionEndDate' does not exist in the current context...
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
Are you using Visual Studio for your development? If so which version? In the older versions you had to declare these manually such as:

protected System.Web.UI.HtmlControls.HtmlGenericControl auctionEndDate;

Author

Commented:
I usin vwd 2010
i'll try what you wrote here, thanks.

Author

Commented:
my code now look like that:
<input type="hidden" id="auctionEndDate" value="<%=TargetDate%>"/>
<script language="JavaScript">


    TargetDate = "document.forms[0].TargetDate.value";
    /*this is a property in code behind*/BackColor = "palegreen";
    ForeColor = "navy";
    CountActive = true;
    CountStepper = -1;
    LeadingZero = true;
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
    FinishMessage = "the auction end"
    </script>
    <script type="text/javascript" language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
    </script>

Open in new window


    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected System.Web.UI.HtmlControls.HtmlGenericControl auctionEndDate;
    
    public string TargetDate()
    {
        string tDate = "";
        using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = "select * from endTime";
            try
            {
                connection.Open();
                tDate = (string)command.ExecuteScalar(); // Expecting the first row and first column
            }

            finally
            {
                connection.Close();
            }
        }
        return tDate;
    }  


    }

Open in new window


and the error is:
CS1502: The best overloaded method match for 'System.IO.TextWriter.Write(char)' has some invalid arguments

Open in new window


<input type="hidden" id="auctionEndDate" value="<%=TargetDate%>"/>

Open in new window


yesterday i played with the code and i got the error pipe40 somthing about the sql...maybe i made everything good exept the connection string, but i deleted the code and now i have the code that i included at that reply.

Commented:
I use VWD 2010 also... there was a small mistake in my code example, it should have been a capital V in value 'auctionEndDate.Value'. I've just tested my code example above on my machine and it works fine. Did you add runat="server" to the hidden input??

After testing, I changed the hidden input to a text input so I could see it working (putting the value into the textbox).

If you insist on having it public it might be better to have it as a property instead of a method, to do this you would do:

 public string TargetDate
    {
       get{
        string tDate = "";
        using (SqlConnection connection = new SqlConnection("Server=localhost;Database=Timer;Trusted_Connection=True"))
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = "select * from endTime";
            try
            {
                connection.Open();
                tDate = (string)command.ExecuteScalar(); // Expecting the first row and first column
            }

            finally
            {
                connection.Close();
            }
        }
        return tDate;
      }
    }  

Open in new window


Thinking about this may fix your original problem because really you should have been putting: (notice I've added brackets to the end of TargetDate because it's a method):

<input type="hidden" id="auctionEndDate" value="<%=TargetDate()%>"/>

Open in new window


Try either of those above (although i still think you should follow my first code example it should work)

hope it helps

Author

Commented:
thank you expert ! now i'm think the problem is at the sql connection... i need to add somthing on the web.config ?  i got that error:

provider named pipes provider error 40 could not open a connection to sql server

thank you, i hope for the last time about that question.

Author

Commented:
ok... now i add the runat=server and the page work without error... but i dont get the time... that show:

NaN Days, NaN Hours, NaN Minutes, NaN Seconds.

and i have data at the table.

Author

Commented:
when i'm delete the runat=server from the hidden field , i get again the error of the connection to the sql server pipe 40....
and when i have the runat = server the - value="<%=TargetDate%>"   dont work because i tried to change the TargetDate to blablabla and nothing happen at the page... like that not used when there is runat=server ....

Commented:
I didn't look at you javascript sorry, I spotted the codebehind error first. You can not access a hidden field using javascript the whole purpose of the hidden input is so the client can not access it.

I would suggest instead you use a text input and hide it using display style property:

<input type="text" id="auctionEndDate" runat="server" style="display:none"> 

Open in new window

Author

Commented:
i getting the error: provider: Named Pipes Provider, error: 40, to the line: connection.Open();

  ...somthing wrong at the SqlConnection command?


maybe there is other way to that? with ajax? without javascript?
anyway, i think that the only probleme now is with the sql connection.

Commented:
Yes that is a problem with the sqlconnection.

There may be another way but this way is most likely the easiest!

Author

Commented:
ok. i think you gave the answer for my question, and i'll open new post about the sql connection.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial