Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL Ajax
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 + "')";
            dbClass.ConnectDataBaseToInsert(postScrap);           
            Controls_GetUserScraps obj = new Controls_GetUserScraps();
            obj.GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
        }
 
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.
            GridViewUserScraps.DataBind();
        }
    }
Posted 9-Apr-13 23:10pm
Comments
tumbledDown2earth at 10-Apr-13 4:24am
   
any chance of seeing a stacktrace?
arbaaz jalil at 10-Apr-13 4:36am
   
I have no clue about stacktrace. I am a beginner.
bluesathish at 10-Apr-13 4:40am
   
post your full exception details.
arbaaz jalil at 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:
mvamsianil at 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 at 10-Apr-13 5:04am
   
is dt i.e., datatable or anything is it declared already?
Rate this: bad
good
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 | :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.
  Permalink  
Comments
arbaaz jalil at 10-Apr-13 5:24am
   
I will convert it into Parametrized queries later on when i am almost done :) Thanks!
OriginalGriff at 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 at 10-Apr-13 6:14am
   
You are probably right. I will do it thanks! :)
OriginalGriff at 10-Apr-13 6:23am
   
You're welcome!
bluesathish at 10-Apr-13 5:28am
   
Good suggestion @ OriginalGriff.
Rate this: bad
good
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()));
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
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