Click here to Skip to main content
13,089,504 members (60,589 online)
Rate this:
Please Sign up or sign in to 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:

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 + "')";
            Controls_GetUserScraps obj = new Controls_GetUserScraps();

UserControl Code:

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.
Posted 9-Apr-13 22:10pm
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.
mvamsianil 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.
Nandakishorerao 10-Apr-13 5:04am
is dt i.e., datatable or anything is it declared already?
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.
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)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
arbaaz jalil 10-Apr-13 5:24am
I will convert it into Parametrized queries later on when i am almost done :) Thanks!
bluesathish 10-Apr-13 5:28am
Good suggestion @ OriginalGriff.
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!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Well i fixed it myself by not creating an object of UserControl. And Replacing that with
GetUserScraps1.GetUserScraps(int.Parse( Request.QueryString["Id"].ToString()));

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100