Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Display Default Content If No QueryString Provided

Posted on 2009-05-13
11
567 Views
Last Modified: 2012-06-22
Hi,
I have a query that takes data (articles) from a database and displays them on the page. If someone visits Page.aspx?id=1 they will see article 1. My problem is I want to display some default content such as a list of articles on Page.aspx or, if someone just manually removes the query string from the URL. I used to do this all the time in PHP/MySQL but don't know how to do it in ASP.NET C#. The code provided shows where I am up to. I did this with the "Configure Data Source".

Thanks,
Keith
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="C_ID" HeaderText="C_ID" SortExpression="C_ID" />
                <asp:BoundField DataField="C_Name" HeaderText="C_Name" 
                    SortExpression="C_Name" />
                <asp:BoundField DataField="C_Title" HeaderText="C_Title" 
                    SortExpression="C_Title" />
                <asp:BoundField DataField="C_Category" HeaderText="C_Category" 
                    SortExpression="C_Category" />
                <asp:BoundField DataField="C_Author" HeaderText="C_Author" 
                    SortExpression="C_Author" />
                <asp:BoundField DataField="C_Content" HeaderText="C_Content" 
                    SortExpression="C_Content" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT [C_ID], [C_Name], [C_Title], [C_Category], [C_Author], [C_Content] FROM [SJCS_Content] WHERE ([C_Name] = @C_Name)">
            <SelectParameters>
                <asp:QueryStringParameter Name="C_Name" QueryStringField="id" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        <br />

Open in new window

0
Comment
Question by:nedlogan
  • 5
  • 4
  • 2
11 Comments
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 60 total points
ID: 24373565
in your page load you could check for the existance of the querystring parameter and redirect to another page if its not there:
 

if (Request.QueryString["id"] == null)
{
    Response.Redirect("anotherpage.aspx");
}

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24373647
Try the code provided in snipped box below in your code behind file. Make sure to make appropriate changes as per your situation.
'Define this on class/page level
Private m_intArticleId As Integer
Private m_strName As String
 
'Write below code in Page_Load event
If Request.QueryString("id") IsNot Nothing Then
 
  'For Integer Values
	If Integer.TryParse(Request.QueryString("id"), m_intArticleId) Then
    SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, m_intArticleId)
    
  Else
    'Send some default value here such as 0
    SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, 0)
 
    'Alternatively hide the grid and display default content on page directly
    'GridView1.Visible = False
    'Show some default text on some Label, etc. or unhide a pre
  
	End If
  
  'For String Values
	If Request.QueryString("id").Length > 0 Then
    SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, m_intArticleId)
    
  Else
    'Send some default value here such as 0
    SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, 0)
 
    'Alternatively hide the grid and display default content on page directly
    'GridView1.Visible = False
    'Show some default text on some Label, etc. or unhide a pre
  
	End If
 
End If

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24373657
The String part in the above posted code has been wrongly posted, Use this one instead.
  'For String Values
	If Request.QueryString("name").Length > 0 Then
    SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.String, Request.QueryString("name"))
    
  Else
    'Send some default value here
    SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, "SomeDefaultName")
 
    'Alternatively hide the grid and display default content on page directly
    'GridView1.Visible = False
    'Show some default text on some Label, etc. or unhide a pre
  
	End If

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 2

Author Comment

by:nedlogan
ID: 24373997
Thanks so far. Moghazali I need any code in C# yours looks like VB?

Keith
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24374051
Find herewith the code in C#. I mostly use this converter to get the most good inter-convertion of VB and C#: http://www.developerfusion.com/tools/convert/vb-to-csharp/

//Define this on class or page level
private int m_intArticleId;
private string m_strName;
 
//Write below code in Page_Load event
if (Request.QueryString("id") != null) {
    
  //For Integer Values
  if (int.TryParse(Request.QueryString("id"), m_intArticleId)) {
      SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, m_intArticleId);
  }
  else {
      
      //Send some default value here such as 0
      SqlDataSource1.SelectParameters.Add("ArticleId", TypeCode.Int32, 0);
  }
  
  //Alternatively hide the grid and display default content on page directly
  //GridView1.Visible = False
  //Show some default text on some Label, etc. or unhide a pre
  
  
  //For String Values
  if (Request.QueryString("name").Length > 0) {
      SqlDataSource1.SelectParameters.Add("C_Name", TypeCode.String, Request.QueryString("name"));
  }
  else {
      
      //Send some default value here
      SqlDataSource1.SelectParameters.Add("C_Name", TypeCode.Int32, "SomeDefaultName");
  }
  
  //Alternatively hide the grid and display default content on page directly
  //GridView1.Visible = False
  //Show some default text on some Label, etc. or unhide a pre
  
}

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24374064
Change Line 30 of above code to:

      SqlDataSource1.SelectParameters.Add("C_Name", TypeCode.String, "SomeDefaultName");

Open in new window

0
 
LVL 2

Author Comment

by:nedlogan
ID: 24374468
Hi Moghazali,
I'm getting errors from line 9 in your code. The first error is: "The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments"?

Thanks,
Keith
0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 65 total points
ID: 24374696
Please try using Int32.TryParse() instead of int.
0
 
LVL 2

Author Comment

by:nedlogan
ID: 24375812
Moghazali,
The error now is: "The name 'int32' does not exist in the current context."

Keith
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24376516
don't need to use int32 change it to
if (int.TryParse(Request.QueryString("id").ToString(), out m_intArticleId)) {
0
 
LVL 2

Author Closing Comment

by:nedlogan
ID: 31580903
Hi,
Thanks for your help guys, in the end I used the first solution that GuitarRich posted.
Moghazali, I couldn't get your code to work but thanks anyway for the work you did.
Keith
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question