Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a WebForm which has a UserControl and i want to Bind the UserControl(It is a GridView) on click even of a button which in the webform outside the UserControl. There is a method in UserControl to Bind the GridView which i have declared Public.

WebForm Code:

C#
protected void Unnamed1_Click(object sender, EventArgs e)
    {
        if (!object.Equals(Session["UserId"], null))
        {
            string postScrap = "Insert INTO Scrap (FromId,ToId,Message) VALUES('" + Session["UserId"].ToString() + "','" + Request.QueryString["Id"].ToString() + "','" + TextBoxScrap.Text + "')";
            dbClass.ConnectDataBaseToInsert(postScrap);           
            Controls_GetUserScraps obj = new Controls_GetUserScraps();
            obj.GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
        }


UserControl Code:

C#
public void GetUserScraps(int Id)
    {
        string getUserScraps = "SELECT u.Id as UserId,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId FROM [tbl_user] as u, Scrap as s WHERE u.Id=s.FromId AND s.ToId='" + Id + "' order by senddate desc";
        dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
        if (dt.Rows.Count > 0)
        {
            GridViewUserScraps.DataSource = dt;   //this is where i am getting the exception. eventhough  dt has values.
            GridViewUserScraps.DataBind();
        }
    }
Posted
Comments
tumbledDown2earth 10-Apr-13 4:24am    
any chance of seeing a stacktrace?
arbaaz jalil 10-Apr-13 4:36am    
I have no clue about stacktrace. I am a beginner.
bluesathish 10-Apr-13 4:40am    
post your full exception details.
arbaaz jalil 10-Apr-13 5:12am    
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_av2ytu4w
StackTrace:
at Controls_GetUserScraps.GetUserScraps(Int32 Id) in c:\Users\Master Admin\Desktop\in process\BACKUP\9-4-2013 5_49pm\social\Controls\GetUserScraps.ascx.cs:line 37
at UserDetails1.Unnamed1_Click(Object sender, EventArgs e) in c:\Users\Master Admin\Desktop\in process\BACKUP\9-4-2013 5_49pm\social\UserDetails1.aspx.cs:line 80
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
RelicV 10-Apr-13 4:53am    
Looks like the Gridview binding event is trying to access a control which is not available. Try to provide the Gridview design code and grid event menthods.

You have not assigned a GridView to GridViewUserScraps - it is a null value, so when you try to access it's DataSource property, you get an exception. Look elsewhere in your code, and find where it is declared - then work out what you meant to do with it! :laugh:

And please, don't do things like that: Check your query strings before you try to use them, and do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. This is particularly important when dealing with web based code, when your database could be destroyed from the other side of the world.
 
Share this answer
 
Comments
arbaaz jalil 10-Apr-13 5:24am    
I will convert it into Parametrized queries later on when i am almost done :) Thanks!
OriginalGriff 10-Apr-13 5:44am    
No, you won't. You will find something else to do, we always do that. Get into the habit of doing it right, right from the start. It's a lot quicker that way and you don't miss any of them until the site falls over because it's DB is missing.
arbaaz jalil 10-Apr-13 6:14am    
You are probably right. I will do it thanks! :)
OriginalGriff 10-Apr-13 6:23am    
You're welcome!
bluesathish 10-Apr-13 5:28am    
Good suggestion @ OriginalGriff.
Well i fixed it myself by not creating an object of UserControl. And Replacing that with
GetUserScraps1.GetUserScraps(int.Parse( Request.QueryString["Id"].ToString()));
 
Share this answer
 

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