|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
App SettingsMy website can use either an SQL or Access database, based on the setting in the web.config file. <appSettings> <!--If using Access then set value="AccessDataSource1"/>--> <!--If using MS SQL then set value="SqlDataSource1"/>--> <add key="MyDataSource" value="SqlDataSource1"/> </appSettings> In my content page I have 1) a gridview defined as follows: <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="AttendantID,TimeOfDay" DataSourceID='<%$ AppSettings:MyDataSource %>' > <Columns> < columns defined> </Columns> 2) a button and textbox defined as: <asp:Button id="Button2" runat="server" Text="View Selected" onclick="Button2_Click" /> <br /> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 3) I have also defined a Button2_Click event as follows: protected void Button2_Click(object sender, EventArgs e) { SqlDataSource1.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber WHERE AttendantID = " + TextBox2.Text; GridView1.DataBind(); } My question is how can I set the Button2_Click event to allow the SqlDataSource1.SelectCommand to reflect the chosen database based on the app setting in web.config. I tried <%$ AppSettings:MyDataSource %>.SelectCommand, using different syntax and keep getting and error. Can you tell me the code to use in the Button2_Click event so I can reference the app setting from the web.config in the .SelectCommand? -- Thanks for your help. Morris Hi Morris,
From your description you want to get the reference of the SqlDataSource whose ID is specified in the appSettings in the web.config. Is my understanding correct? If so please try following code to get the reference: string id = ConfigurationManager.AppSettings["MyDataSource"]; SqlDataSource sds = this.FindControl("MyDataSource") as SqlDataSource; If it's not what you need please clarify your requirement. In addition, from your code I can see your web site is under the risk of SQL injection attack. I strongly recommend you read the following article to learn what it is and how to make your site more secure. http://weblogs.asp.net/scottgu/archive/2006/09/30/Tip_2F00_Trick_3A00_-Guard -Against-SQL-Injection-Attacks.aspx If you need further assistance please feel free to ask. Sincerely, Steven Cheng Microsoft MSDN Online Support Lead Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msd***@microsoft.com. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications. Note: MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 2 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. -------------------- Show quoteHide quote >Subject: App Settings >Date: Tue, 2 Dec 2008 11:23:01 -0800 > >Hi, >My website can use either an SQL or Access database, based on the setting in >the web.config file. ><appSettings> ><!--If using Access then set value="AccessDataSource1"/>--> ><!--If using MS SQL then set value="SqlDataSource1"/>--> > <add key="MyDataSource" value="SqlDataSource1"/> ></appSettings> > >In my content page I have >1) a gridview defined as follows: > <asp:GridView ID="GridView1" runat="server" > AllowPaging="True" AllowSorting="True" > AutoGenerateColumns="False" DataKeyNames="AttendantID,TimeOfDay" >DataSourceID='<%$ AppSettings:MyDataSource %>' > > <Columns> > < columns defined> > </Columns> > >2) a button and textbox defined as: > <asp:Button id="Button2" runat="server" Text="View Selected" > onclick="Button2_Click" /> > <br /> > <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> > >3) I have also defined a Button2_Click event as follows: > protected void Button2_Click(object sender, EventArgs e) > > { > SqlDataSource1.SelectCommand = "SELECT Attendant.AttendantID, >Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, >AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, >Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON >Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber WHERE AttendantID = " >+ TextBox2.Text; > GridView1.DataBind(); > } > > >My question is how can I set the Button2_Click event to allow the >SqlDataSource1.SelectCommand to reflect the chosen database based on the app >setting in web.config. > >I tried ><%$ AppSettings:MyDataSource %>.SelectCommand, using different syntax and >keep getting and error. > >Can you tell me the code to use in the Button2_Click event so I can >reference the app setting from the web.config in the .SelectCommand? >-- >Thanks for your help. >Morris > Thanks Steven.
I made the change as suggested, then I changed the .SelectCommand from SqlDataSource1.SelectCommand to sds.SelectCommand as follows: protected void Button2_Click(object sender, EventArgs e) { string id = ConfigurationManager.AppSettings["MyDataSource"]; SqlDataSource sds = this.FindControl("MyDataSource") as SqlDataSource; sds.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber WHERE AttendantID = " + TextBox2.Text; GridView1.DataBind(); GridView1.Visible = true; } I get the following error: Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 26: SqlDataSource sds = this.FindControl("MyDataSource") as SqlDataSource; Line 27: Line 28: sds.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber ORDER BY Attendant.AttendantID"; ; Line 29: GridView1.DataBind(); Line 30: GridView1.Visible = true; Source File: c:\Inetpub\wwwroot\CMWebManager\SystemAdminOnly\Copies\Test.aspx Line: 28 Three questions for code in the click event? 1) how should I reference the .SelectCommand in your suggestion? 2) How would I be able to check if the app setting is pointing to SqlDataSource1 or AccessDataSource1? 3) if the app setting is pointing to the AccessDataSource1 I don't think I could use the SqlDataSource so what would I use? What would I need in the code in the click event change? Sorry if this is very basic stuff but I am learning and have not dealt much with actual coding. -- Show quoteHide quoteThanks for your time and help Morris ""Steven Cheng"" wrote: > Hi Morris, > > From your description you want to get the reference of the SqlDataSource > whose ID is specified in the appSettings in the web.config. Is my > understanding correct? > > If so please try following code to get the reference: > > string id = ConfigurationManager.AppSettings["MyDataSource"]; > SqlDataSource sds = this.FindControl("MyDataSource") as SqlDataSource; > > If it's not what you need please clarify your requirement. > > In addition, from your code I can see your web site is under the risk of > SQL injection attack. I strongly recommend you read the following article > to learn what it is and how to make your site more secure. > > http://weblogs.asp.net/scottgu/archive/2006/09/30/Tip_2F00_Trick_3A00_-Guard > -Against-SQL-Injection-Attacks.aspx > > If you need further assistance please feel free to ask. > > Sincerely, > > Steven Cheng > > Microsoft MSDN Online Support Lead > > > Delighting our customers is our #1 priority. We welcome your comments and > suggestions about how we can improve the support we provide to you. Please > feel free to let my manager know what you think of the level of service > provided. You can send feedback directly to my manager at: > msd***@microsoft.com. > > ================================================== > Get notification to my posts through email? Please refer to > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications. > > Note: MSDN Managed Newsgroup support offering is for non-urgent issues > where an initial response from the community or a Microsoft Support > Engineer within 2 business day is acceptable. Please note that each follow > up response may take approximately 2 business days as the support > professional working with you may need further investigation to reach the > most efficient resolution. The offering is not appropriate for situations > that require urgent, real-time or phone-based interactions. Issues of this > nature are best handled working with a dedicated Microsoft Support Engineer > by contacting Microsoft Customer Support Services (CSS) at > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. > > > > -------------------- > >Subject: App Settings > >Date: Tue, 2 Dec 2008 11:23:01 -0800 > > > > >Hi, > >My website can use either an SQL or Access database, based on the setting > in > >the web.config file. > ><appSettings> > ><!--If using Access then set value="AccessDataSource1"/>--> > ><!--If using MS SQL then set value="SqlDataSource1"/>--> > > <add key="MyDataSource" value="SqlDataSource1"/> > ></appSettings> > > > >In my content page I have > >1) a gridview defined as follows: > > <asp:GridView ID="GridView1" runat="server" > > AllowPaging="True" AllowSorting="True" > > AutoGenerateColumns="False" DataKeyNames="AttendantID,TimeOfDay" > >DataSourceID='<%$ AppSettings:MyDataSource %>' > > > <Columns> > > < columns defined> > > </Columns> > > > >2) a button and textbox defined as: > > <asp:Button id="Button2" runat="server" Text="View Selected" > > onclick="Button2_Click" /> > > <br /> > > <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> > > > >3) I have also defined a Button2_Click event as follows: > > protected void Button2_Click(object sender, EventArgs e) > > > > { > > SqlDataSource1.SelectCommand = "SELECT Attendant.AttendantID, > >Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, > >AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, > >Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON > >Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber WHERE AttendantID = > " > >+ TextBox2.Text; > > GridView1.DataBind(); > > } > > > > > >My question is how can I set the Button2_Click event to allow the > >SqlDataSource1.SelectCommand to reflect the chosen database based on the > app > >setting in web.config. > > > >I tried > ><%$ AppSettings:MyDataSource %>.SelectCommand, using different syntax and > >keep getting and error. > > > >Can you tell me the code to use in the Button2_Click event so I can > >reference the app setting from the web.config in the .SelectCommand? > >-- > >Thanks for your help. > >Morris > > > > Hi Morris,
I'm Allen Chen. Yesterday I'm out of office so I asked Steven to help me post my replies to your three posts. Now I'll follow up your cases. First, sorry that it's my typo. Here's the correct code: string id = ConfigurationManager.AppSettings["MyDataSource"]; SqlDataSource sds = this.FindControl(id) as SqlDataSource; The first line gets the appsttings value from web.config and the second line uses FindControl method to get the reference of the SqlDataSource control. Quote from Morris ================================================= 1) how should I reference the .SelectCommand in your suggestion? ================================================= Please try the correct code above. Sorry for that again. Quote from Morris ================================================= 2) How would I be able to check if the app setting is pointing to SqlDataSource1 or AccessDataSource1? 3) if the app setting is pointing to the AccessDataSource1 I don't think I could use the SqlDataSource so what would I use? What would I need in the code in the click event change? ================================================= We can try this way to do so: string id = ConfigurationManager.AppSettings["MyDataSource"]; Control datasourcecontrol = this.FindControl(id); if (datasourcecontrol is SqlDataSource) { //Your code } else if (datasourcecontrol is AccessDataSource) { //Your code } You can refer to the following documentation to learn is operator. http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx Please let me know if you have further questions. Regards, Allen Chen Microsoft Online Community Support Hi,
1) When I tried the revised code, I get error. Revised code protected void Button1_Click(object sender, EventArgs e) { string id = ConfigurationManager.AppSettings["MyDataSource"]; SqlDataSource sds = this.FindControl(id) as SqlDataSource; sds.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber ORDER BY Attendant.AttendantID"; ; GridView1.DataBind(); GridView1.Visible = true; } Error Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 28: SqlDataSource sds = this.FindControl(id) as SqlDataSource; Line 29: Line 30: sds.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber ORDER BY Attendant.AttendantID"; ; Line 31: GridView1.DataBind(); Line 32: GridView1.Visible = true; Source File: c:\Inetpub\wwwroot\CMWebManager\SystemAdminOnly\Copies\Test.aspx Line: 30 When I display id in TextBox2 it is set to SqlDataSource1. 3) When I use the button_click as follows, nothing shows in TextBox2. What am I doing wrong? protected void Button1_Click(object sender, EventArgs e) { string id = ConfigurationManager.AppSettings["MyDataSource"]; Control datasourcecontrol = this.FindControl(id); if (datasourcecontrol is SqlDataSource) { TextBox2.Text = "SqlDataSource"; } else if (datasourcecontrol is AccessDataSource) { TextBox2.Text = "AccessDataSource"; //Your code } -- Show quoteHide quoteSorry for so many questions but I have read many articles and examples and walkthroughs but just cannot understand wht I am doint wrong. Any details you can provide would be much appreciated. Thanks for understanding and help. Morris "Allen Chen [MSFT]" wrote: > Hi Morris, > > I'm Allen Chen. Yesterday I'm out of office so I asked Steven to help me > post my replies to your three posts. Now I'll follow up your cases. > > First, sorry that it's my typo. Here's the correct code: > > string id = ConfigurationManager.AppSettings["MyDataSource"]; > SqlDataSource sds = this.FindControl(id) as SqlDataSource; > > The first line gets the appsttings value from web.config and the second > line uses FindControl method to get the reference of the SqlDataSource > control. > > Quote from Morris ================================================= > 1) how should I reference the .SelectCommand in your suggestion? > > > ================================================= > > Please try the correct code above. Sorry for that again. > > Quote from Morris ================================================= > > 2) How would I be able to check if the app setting is pointing to > SqlDataSource1 or AccessDataSource1? > > 3) if the app setting is pointing to the AccessDataSource1 I don't think I > could use the SqlDataSource so what would I use? What would I need in the > code in the click event change? > ================================================= > > We can try this way to do so: > > string id = ConfigurationManager.AppSettings["MyDataSource"]; > Control datasourcecontrol = this.FindControl(id); > if (datasourcecontrol is SqlDataSource) > { > //Your code > } > else if (datasourcecontrol is AccessDataSource) > { > //Your code > } > > You can refer to the following documentation to learn is operator. > http://msdn.microsoft.com/en-us/library/scekt9xw(VS.71).aspx > > Please let me know if you have further questions. > > Regards, > Allen Chen > Microsoft Online Community Support > > Hi Morris,
I think the problem is caused by this line: Control datasourcecontrol = this.FindControl(id); If we set a breakpoint after this line and check the value of datasourcecontrol we may find it's null. (Please confim it on your side. You can refer to http://support.microsoft.com/kb/308469 to learn how to set breakpoints in Visual Studio). There're some general reasons that can cause this problem, including trying to find a control in the ItemTemplate, trying to find a control of the content page from a master page, etc. If you want to find the control of the content page from a master page you can first get the reference of the ContentPlaceHolder, then use ContentPlaceHolder.FindControl method to get the reference of the control you need to find. For example, we need to try following way to find the subButton control from the master page. Master Page: Aspx: <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> Aspx.cs: protected void Button1_Click(object sender, EventArgs e) { Control c = this.ContentPlaceHolder1.FindControl("subButton"); } Content page: <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Button ID="subButton" runat="server" Text="Button" /> </asp:Content> You don't have to feel sorry, Morris. Everyone will have many questions when stepping into a new field. To make our discussion more targeted you can send me a demo. We can discuss further based on the demo. I think it will help to solve this problem faster. Regards, Allen Chen Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msd***@microsoft.com. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications. Note: MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 2 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. You are right, when I set a check point the value of datasourcecontrol is
null. I got around the issue by doing the following: protected void ViewSelect_Click(object sender, EventArgs e) { string id = ConfigurationManager.AppSettings["MyDataSource"]; Control datasourcecontrol = this.FindControl(id); if (id.Equals("SqlDataSource1")) { GridView1.DataSourceID = "SqlDataSource1"; SqlDataSource1.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber WHERE AttendantID = '" + TextBox2.Text + "'"; GridView1.DataBind(); GridView1.Visible = true; } else if (id.Equals("AccessDataSource1")) { GridView1.DataSourceID = "AccessDataSource1"; AccessDataSource1.SelectCommand = "SELECT Attendant.AttendantID, Attendant.TimeOfDay, AttTimeOfDayXref.TimeOfDayNumber, AttTimeOfDayXref.TimeOfDayDescription, Attendant.ExtensionToDial, Attendant.DirectoryListing FROM Attendant INNER JOIN AttTimeOfDayXref ON Attendant.TimeOfDay = AttTimeOfDayXref.TimeOfDayNumber WHERE AttendantID = '" + TextBox2.Text + "'"; GridView1.DataBind(); GridView1.Visible = true; } Hope this is OK to use as I have done. Thanks for your help. -- Show quoteHide quoteThanks Morris "Allen Chen [MSFT]" wrote: > Hi Morris, > > I think the problem is caused by this line: > > Control datasourcecontrol = this.FindControl(id); > > If we set a breakpoint after this line and check the value of > datasourcecontrol we may find it's null. (Please confim it on your side. > You can refer to http://support.microsoft.com/kb/308469 to learn how to set > breakpoints in Visual Studio). > > There're some general reasons that can cause this problem, including trying > to find a control in the ItemTemplate, trying to find a control of the > content page from a master page, etc. > > If you want to find the control of the content page from a master page you > can first get the reference of the ContentPlaceHolder, then use > ContentPlaceHolder.FindControl method to get the reference of the control > you need to find. > > For example, we need to try following way to find the subButton control > from the master page. > > Master Page: > Aspx: > <asp:Button ID="Button1" runat="server" Text="Button" > onclick="Button1_Click" /> > <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> > > </asp:ContentPlaceHolder> > Aspx.cs: > protected void Button1_Click(object sender, EventArgs e) > { > Control c = this.ContentPlaceHolder1.FindControl("subButton"); > } > > Content page: > > <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" > runat="server"> > <asp:Button ID="subButton" runat="server" Text="Button" /> > </asp:Content> > > You don't have to feel sorry, Morris. Everyone will have many questions > when stepping into a new field. > > To make our discussion more targeted you can send me a demo. We can discuss > further based on the demo. I think it will help to solve this problem > faster. > > Regards, > Allen Chen > Microsoft Online Community Support > > Delighting our customers is our #1 priority. We welcome your comments and > suggestions about how we can improve the support we provide to you. Please > feel free to let my manager know what you think of the level of service > provided. You can send feedback directly to my manager at: > msd***@microsoft.com. > > ================================================== > Get notification to my posts through email? Please refer to > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications. > > Note: MSDN Managed Newsgroup support offering is for non-urgent issues > where an initial response from the community or a Microsoft Support > Engineer within 2 business day is acceptable. Please note that each follow > up response may take approximately 2 business days as the support > professional working with you may need further investigation to reach the > most efficient resolution. The offering is not appropriate for situations > that require urgent, real-time or phone-based interactions. Issues of this > nature are best handled working with a dedicated Microsoft Support Engineer > by contacting Microsoft Customer Support Services (CSS) at > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. > > > Hi Morris,
In my opinion relying on the id to decide the programming logic is probably not a good approach. However, if you like to do so you can just keep it. What I want to warn is the risk of the SQL injection attack of your code. Please make sure you've read the following article: http://weblogs.asp.net/scottgu/archive/2006/09/30/Tip_2F00_Trick_3A00_-Guard -Against-SQL-Injection-Attacks.aspx And change your code correspondingly. Regards, Allen Chen Microsoft Online Community Support |
|||||||||||||||||||||||