Click here to Skip to main content
16,016,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, Im using the code below to load an aspx page

.js file
JavaScript
var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid)
{
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
	page_request = new XMLHttpRequest()
	else if (window.ActiveXObject)
	{ // if IE
		try 
		{
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e)
		{
			try
			{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e)
			{
			
			}
		}
	}
	else
	return false
	page_request.onreadystatechange=function()
	{
		loadpage(page_request, containerid)
	}
	page_request.open('GET', url, true)
	page_request.send(null)
}


function loadpage(page_request, containerid)
{
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
	document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs()
{
	if (!document.getElementById)
	return
	for (i=0; i<arguments.length; i++)
	{
		var file=arguments[i]
		var fileref=""
		if (loadedobjects.indexOf(file)==-1)
		{ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1)
			{ //If object is a js file
				fileref=document.createElement('script')
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1)
			{ //If object is a css file
				fileref=document.createElement("link")
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		
		if (fileref!="")
		{
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects+=file+" " //Remember this object as being already added to page
		}
	}
}

and this code in .cs file

Working fine with this code loading simple pages. (just display Hello World!)

C#
protected void AddORG_Click(object sender, EventArgs e)
{
    Page.RegisterClientScriptBlock("ajax", "<script type='text/javascript'>ajaxpage('Main.aspx', 'pageContent')</script>");
}


but when I make the code like this (to store data in the sql). It display the error.

C#
protected void AddORG_Click(object sender, EventArgs e)
{
    Page.RegisterClientScriptBlock("ajax", "<script type='text/javascript'>ajaxpage('Main.aspx', 'pageContent')</script>");
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AccountConnectionString"].ConnectionString);
    con.Open();
    string cmdStr = "SELECT count(Org_Name) FROM Organization WHERE Org_Name='" + txtOrgName.Text + "'";
    SqlCommand checkOrgExist = new SqlCommand(cmdStr, con);
    int temp = Convert.ToInt16(checkOrgExist.ExecuteScalar().ToString());
    con.Close();

    if (temp == 1)
    {
        lblError.Visible = true;
        lblError.Text = "Organization already exist, Please try another name.";
        lblError.ForeColor = System.Drawing.Color.Red;
    }
    else
    {
        con.Open();
        string addOrg = "INSERT INTO Organization(Org_Name, Org_Info, User_ID) VALUES(@Org_Name, @Org_Info, @User_ID)";
        SqlCommand add = new SqlCommand(addOrg, con);
        add.Parameters.AddWithValue("@Org_Name", txtOrgName.Text);
        add.Parameters.AddWithValue("@Org_Info", txtOrgInfo.Text);
        add.Parameters.AddWithValue("@User_ID", "12345");

        try
        {
            add.ExecuteNonQuery();
            con.Close();
            lblError.Visible = true;
            lblError.Text = "Successfully Created!";
            lblError.ForeColor = System.Drawing.Color.GreenYellow;
            //Response.Redirect("http://google.com");


        }
        catch (Exception er)
        {
            Response.Write(er);
        }
    }
}




ERROR MESSAGE:
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.Web.HttpException: The state information is invalid for this page and might be corrupted.

anyone can help me solve this problem?

Thanks in advance..
Posted
Updated 3-Jun-13 2:31am
v3
Comments
V. 3-Jun-13 5:22am    
Please set some indenting on your code (try spaces instead of tabs).
What do you mean by "when I add sql command" ?
Prasad Khandekar 3-Jun-13 8:48am    
Hello,

What's probably happening is that your return response is causing fields to be duplicated e.g. ASP.NET hidden fields namely __VIEWSTATE and that's causing the invalid state problem.

Regards,
nielcleo 3-Jun-13 9:08am    
thanks for the response.. just want to know how can i prevent the duplication of the fields?
Suvabrata Roy 3-Jun-13 9:51am    
Do you have used any update panel?
nielcleo 3-Jun-13 10:59am    
nope i didn't try to use update panel.

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