Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I am doing the conversion from php chat application to asp.net chat application.
In php chat application work fine,

so i am converting now, the problem i am facing from php is, they used some array with session variable i dont know the correct equalent in Asp.net. this is the code

in php:

PHP
$_SESSION['openChatBoxes'][$chat['from']] = "from value";


in .net i tried:

VB
DirectCast((Session("openChatBoxes")), ArrayList)(getIndexOf(DirectCast((Session("openChatBoxes")), ArrayList), "fromname")) = "messagecontent"


Its shows the error message for me as follows

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Please help me. its very urgent

[Modified: added code formatting]
Posted
Updated 19-Mar-10 12:50pm
v2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class chat : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (Request.QueryString["action"] != null)
{
if (Session["chatHistory"] == null)
{
Session["chatHistory"] = new Hashtable(); ;
}
if (Session["openChatBoxes"] == null)
{
Session["openChatBoxes"] = new Hashtable();
}
if (Session["tsChatBoxes"] == null)
{
Session["tsChatBoxes"] = new Hashtable();
}
switch (Request.QueryString["action"].ToString())
{
case "chatheartbeat":
{
chatHeartbeat();
break;
}
case "sendchat":
{
sendChat();
break;
}
case "closechat":
{
closeChat();
break;
}
case "startchatsession":
{
startChatSession();
break;
}
}
}

Hashtable s = new Hashtable();
}
private void chatHeartbeat()
{
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */

List<Chattlb> messages = Chating.chat.GetNotRecieved(Session["username"].ToString());
string items = "";

foreach (Chattlb message in messages)
{
if (Session["openChatBoxes"] != null && Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["openChatBoxes"])).Contains(message.userfrom) && ((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
items = ((Hashtable)(Session["chatHistory"]))[message.userfrom].ToString();
}
}

message.message = sanitize(message.message);

items = items + "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";

if (Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
((Hashtable)(Session["chatHistory"]))[message.userfrom] = "";

}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[message.userfrom.ToString()] += "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[message.userfrom.ToString()] = null; // Unset means clear the session value
if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[message.userfrom.ToString()] = message.send;


}
if (Session["openChatBoxes"] != null)
{
Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{
int chatbox = i;
if (openChatBoxesArray[i] != null)
{
DateTime time = (DateTime)(openChatBoxesArray[i]);
string timeval = "";
TimeSpan Now = (TimeSpan)(DateTime.Now - time);
timeval = time.ToString("g:iA M dS");
string message = "Sent at " + timeval;

if (Now.Minutes > 180)
{
items += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{
((Hashtable)(Session["chatHistory"]))[chatbox] = "";
}
((Hashtable)(Session["chatHistory"]))[chatbox] += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
((Hashtable)(Session["tsChatBoxes"]))[chatbox] = "1";
}
}
}
}
Chating.chat.UpdateRecieved(Session["username"].ToString());
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
Response.Write("\"items\":[" + items + "]");
HttpContext.Current.Response.Write(" }");
Response.End();
}
private string sanitize(string text)
{
//text = htmlspecialchars(text, ENT_QUOTES); // Convert the html special characters
text = text.Replace("\n\r", "\n");
text = text.Replace("\r\n", "\n");
text = text.Replace("\n", "<br>");
return text;
}
private int getIndexOf(Hashtable SessionArray, string Value)
{
//try
//{
// return SessionArray.IndexOf((object)Value);
//}
//catch
//{
return 0;
//}
}

private string chatBoxSession(int chatbox)
{

string items = "";

if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{ // Check the Session variable
items = ((Hashtable)(Session["chatHistory"]))[chatbox].ToString();
}

return items;
}

private void sendChat()
{
string from = Session["username"].ToString(); // Assign Session user name value to $from value
string to = Request["to"]; // Assign POST FORM 'To' value to $to value}
string message = Request["message"]; // Assign POST FORM 'message' value to $message value


if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[to] = DateTime.Now.ToString("Y-m-d H:i:s");

string messagesan = sanitize(message);
if (Session["chatHistory"] != null)
{
if (((Hashtable)(Session["chatHistory"])).Contains(Request["to"]))
{
((Hashtable)(Session["chatHistory"]))[to] = "";
//((Hashtable)(Session["chatHistory"]))[getIndexOf(((Hashtable)(Session["chatHistory"])), Request["to"])] = "";
}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[to] += "\"s\":\"1\",\"f\":\"{" + to + "}\",\"m\":\"{" + messagesan + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[to] = null;

/* Add Insert Query here*/

Chattlb chatObj = new Chattlb();
chatObj.userto = to;
chatObj.userfrom = from;
chatObj.message = message;
chatObj.send = DateTime.Now;
chatObj.recd = 0;
Chating.chat.InsertChat(chatObj);

Response.Write("1");
Response.End();
}
private void closeChat()
{
int chatbox = 1; //value of chatbox
try
{
((Hashtable)(Session["openChatBoxes"]))[chatbox] = null;
}
catch
{
}
Response.Write("1");
}
private void startChatSession()
{
string items = "";
if (Session["openChatBoxes"] != null)
{ // Check the Session variable
Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{ // Loop the session array
items += chatBoxSession(i); // Append to Items variable
}
}
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
HttpContext.Current.Response.Write("\"username\": \"" + Session["username"].ToString() + "\","); // Display the Username Which is set in the session
HttpContext.Current.Response.Write("\"items\": [");
HttpContext.Current.Response.Write(items);
HttpContext.Current.Response.Write("]");
HttpContext.Current.Response.Write("}");
Response.End();
}
}
 
Share this answer
 
Comments
beyazcennet 4-Mar-11 18:30pm    
attempting this project, have run?
Gowris6 23-Sep-11 0:22am    
hi, in this, "Chating.chat.GetNotRecieved", could you please provide the class Chating.chat
divydivy 5-Oct-11 3:02am    
i use code of jquery chat from php to asp.net..but it shows error "The type or namespace name 'Chattlb' could not be found"
if you got result..
So could you help me?.Please.....thax
divydivy 5-Oct-11 2:40am    
i use this code..but it shows error "The type or namespace name 'Chattlb' could not be found"
could help me?....thax
 
Share this answer
 
Comments
divydivy 5-Oct-11 3:08am    
i use code of jquery chat from php to asp.net..but it shows error "The type or namespace name 'Chattlb' could not be found"
could you help me?.Please.....thanx
I'm trying to convert that project to asp.net too.
Did u had any luck doing it?

Bye
 
Share this answer
 
Comments
heling0223 5-Jul-10 21:18pm    
Reason for my vote of 4
fdf
divydivy 5-Oct-11 3:08am    
i use code of jquery chat from php to asp.net..but it shows error "The type or namespace name 'Chattlb' could not be found"
could you help me?.Please.....thanx
ravuravu 14-Mar-13 4:07am    
hey divydivy chattlb is a class file named Chattlb.i don't know the coding inside Chattlb class file u pls create a class file inside it.the Chattlb assembly error will not see.bye.one thing if u get the answer pls share to me because i need a chat application in asp.net like facebook chat
in .net I think you should remove 1 from index. see the updated line here

DirectCast((Session("openChatBoxes")), ArrayList)(getIndexOf(DirectCast((Session("openChatBoxes")), ArrayList), "fromname") - 1) = "messagecontent"

Hope this will work.
 
Share this answer
 
Comments
heling0223 5-Jul-10 21:18pm    
Reason for my vote of 1
sf
divydivy 5-Oct-11 3:08am    
i use code of jquery chat from php to asp.net..but it shows error "The type or namespace name 'Chattlb' could not be found"
could you help me?.Please.....thanx
:laugh: :cool: X| :suss: :omg: :-O :-\ :sigh: :laugh:
 
Share this answer
 
Comments
divydivy 5-Oct-11 3:08am    
i use code of jquery chat from php to asp.net..but it shows error "The type or namespace name 'Chattlb' could not be found"
could you help me?.Please.....thanx
please help me.can anyone post the .aspx page code of chat??
 
Share this answer
 
Comments
Sandeep Mewara 9-Jun-12 7:00am    
This is not an answer. Further expecting a code is not the way it works here. You need to make effort and share what you have done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900