Question

Asp.Net C# Object reference not set to an instance of an object

Asked by: homeshopper

Hi, I am getting the following error:
Object reference not set to an instance of an object.
When I intially run the page it displays a list of databases as per attached '*.jpg' file.
Then I am able to select a database name and it displays the corresponding tables.
But, when I then select a table name it gives the error described below:
The complete code is as per attached code snippet.
Thanks in Advance.
Line 117:        {
Line 118:            Response.Write("dbTableName:" + dbTableName + "  ");
Line 119:            dbTableName = Request.QueryString["tableName"].Trim(' ');
Line 120:        }
Line 121:        string oMyString = null;
 d:\aaaWebaaa\assignmentWebDev\assignmentWebData\app\cmsSchema.aspx.cs

//****************************************************************
//cmsSchema.aspx.cs Code behind Page
//****************************************************************
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
 
public partial class cmsSchema : System.Web.UI.Page
{
    public string sSql;
    public string sDir;
    public bool bDatabaseExists = false;
    public bool bTableExists = false;
    public string sDBServer = System.Net.Dns.GetHostName();
    public string cboSQLServers = System.Net.Dns.GetHostName();
    public string sDataSource = "fuertemon";
    public object sDBServerProvider = "\\sqlexpress";
    public string sUSERid = "sa";
    public string sDBName = "Master";
    public string sDBPass = "h6web97";
    public string TempTable = "Employ";
    public string TempProcedure = "UpdateEmployDetails";
    public string strTable;
    public string strProcedure;
    public SqlConnection connection;
    public string ConnectionString;
    public string MsgBoxDatabase = null;
    public string MsgBoxTable = null;
    public string MessageBox = null;
    public string MessageBoxProcedure = null;
    public DataView Source;
    public DataSet Ds;
    public string sOutput;
    public string strResponse;
    public int i;
    public string tablePage;
    public string dbName;
    public string strCountdb;
    public string dbTableName;
    public string oTableName;
    public string strTbleCount;
    public string odbName;
    public string odbTableName;
    protected void Page_Load(object sender, System.EventArgs e)
    {
        HttpContext _Context = HttpContext.Current;
        if (Page.IsPostBack)
        {
            
        }
        if (!Page.IsPostBack)
        {
            BindGrid();
        }
        //***************************************************************
        //Response.Write("1#" + Request["tablePage"]);
        LabelPage.Text = (Request["tablePage"]);
        //***************************************************************
        SqlConnection sConn = null;
        SqlDataReader sReader = null;
        SqlCommand sComm = null;
        string sMyString = null;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        sMyString = Session["connStringValue"] + ";Initial Catalog=" + sDBName;
        sConn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        sComm = new System.Data.SqlClient.SqlCommand("Select * from sys.databases WHERE name NOT IN ('master','model','msdb','tempdb')", sConn);
        try
        {
            sConn.Open();
            sReader = sComm.ExecuteReader();
            Repeater1.DataSource = sReader;
            Repeater1.DataBind();
            sReader.Close();
        }
        catch (Exception ex)
        {
            // all other .NET exceptions
            Labelerr.Text = ex.ToString();
        }
        catch
        {
            // all other exceptions
 
        }
        finally
        {
            if (sConn.State != ConnectionState.Closed)
            {
                sConn.Close();
            }
        }
        //***************************************************************
        Init();
        //testDatabase_SelectedIndexChanged(DropDownList1, Nothing)
        //****************************************************************
        if (!(odbName==""));
        {
            Response.Write("odbName:" + odbName + "  ");
            odbName = Request.QueryString["db"].Trim(' ');
        }
        if (!(dbTableName == "")) ;
        {
            Response.Write("dbTableName:" + dbTableName + "  ");
            dbTableName = Request.QueryString["tableName"].Trim(' ');
        }
        string oMyString = null;
        SqlDataReader odbreader = null;
        SqlCommand odbcomm = null;
        int oCounter = 0;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        oMyString = Session["connStringValue"] + ";Initial Catalog=" + odbName;
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(oMyString);
        SqlConnection odbconn = new SqlConnection(builder.ConnectionString);
        odbconn.Open();
        DataTable tblDatabases = odbconn.GetSchema(SqlClientMetaDataCollectionNames.Tables);
        odbconn.Close();
        ArrayList oItems = new ArrayList();
        ArrayList data = new ArrayList();            
        foreach (DataRow rowDatabase in tblDatabases.Rows)
        {
            if (!(string.IsNullOrEmpty(rowDatabase["table_name"].ToString())))
            {
                builder.InitialCatalog = rowDatabase["table_name"].ToString();
                odbconn.ConnectionString = builder.ConnectionString;
                oItems.Add(rowDatabase["table_name"].ToString());
                //hs.Add("Inv#", "001");
                data.Add(KV("Table:", rowDatabase["table_name"].ToString()));
            }
            oCounter += 1;
        }
        odbconn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        if (odbconn.State != ConnectionState.Closed)
        {
            odbconn.Close();
        }
        //grid.DataSource = oItems;
        //grid.DataBind();
        repTest.DataSource = data;
        repTest.DataBind();
        //Session["oItems"] = oItems;
        //Session["oCounter"] = oCounter;
        
        //****************************************************************
    }
    // KV for KeyValue is defined as returning an anonymous object:
    private object KV(string Key, string Value)
        {
            return new { Key, Value };
        }
    private static new void Init()
    {
        // initiate variables etc
    }
    protected override void OnInit(EventArgs e)
    {
        //MyBase.OnInit(e);
        //Converted event handler wireups:
        Load += Page_Load;
        //AddHandler DropDownList1.SelectedIndexChanged, AddressOf testDatabase_SelectedIndexChanged
        //AddHandler DropDownList1.SelectedIndexChanged, AddressOf DropDownList1_SelectedIndexChanged
        //AddHandler grid.SelectedIndexChanged, AddressOf grid_SelectedIndexChanged
        //AddHandler empDetails.PageIndexChanging, AddressOf empDetails_PageIndexChanging1
        //AddHandler empDetails.ModeChanging, AddressOf empDetails_ModeChanging
        //AddHandler empDetails.ItemUpdating, AddressOf empDetails_ItemUpdating
        //AddHandler empDetails.ItemUpdated, AddressOf empDetails_ItemUpdated
    }
    public void BindGrid()
    {
        //***************************************************************
 
        //***************************************************************
    }
}
<%
    //*************************************************
    //cmsSchema.aspx Code Page
    //*************************************************
%>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cmsSchema.aspx.cs" Inherits="cmsSchema" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<link rel="stylesheet" type="text/css" href="../css/outpost.css" />
</head>
<body style="background-image: url(../images/WebFront.jpg); background-repeat:repeat-x">
<!--START PAGE SECTION-->
<table id="table1" style="z-index:101; position:absolute; top:50px; left:5px; width:auto">
<tr>
<td>
<div style="width:900px; height:470px; font-size:smaller; overflow:auto;  border: 0px solid #000000" id="DIV1" onclick="return DIV1_onclick()">
<form id="form1" runat="server">
Heading:1:#
<% 
 for (var i = 0; i <= 5; i++)
{
%>
 <a href="cmsSchema.aspx?tablePage=<%=i%>">[<%=i%>]</a>
<%
}
 
%><a href="../Default.aspx">[Return]</a><br />
Heading:2:#Page:<asp:Label ID="LabelPage" runat="server"></asp:Label><br />
<asp:Repeater id="Repeater1" runat="server">
      <HeaderTemplate>
      </HeaderTemplate>
      <ItemTemplate>
      <a href="cmsSchema.aspx?db=<%# DataBinder.Eval(Container.DataItem, "name")%>">
      <%# DataBinder.Eval(Container.DataItem, "name")%></a>&nbsp;&nbsp;&nbsp; 
      </ItemTemplate>
      <FooterTemplate>
      </FooterTemplate>
      </asp:Repeater><br />
Heading:3:#<br />
<asp:Repeater runat="server" ID="repTest">
    <ItemTemplate>
<a href="cmsSchema.aspx?tableName=<%# DataBinder.Eval(Container.DataItem, "Value")%>">
<%# DataBinder.Eval(Container.DataItem, "Value")%></a>&nbsp;&nbsp;&nbsp; 
<!--
            <%#Eval("Key") %><br />
            <%#Eval("Value") %> 
  -->       
    </ItemTemplate>
</asp:Repeater>
  
<div id="news" class="news" style="float:left;border:1px solid #CCC;padding:.5em;">
<h5 class="headline">Table Data</h5>
<asp:Label ID="Labelerr" runat="server"></asp:Label><br />
<asp:Label ID="detailsLabel" runat="server" />
</div>
</form>
</div>
</td>
</tr>
</table>
<!--END PAGE SECTION CODE-->
</body>
</html>

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-12 at 09:36:43ID24805061
Topics

Programming for ASP.NET

,

Microsoft Visual C#.Net

Participating Experts
1
Points
500
Comments
20

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. trim?
    sorry guys need to ask u all something.. is it possible to trim this thingy here... FNCusSupCode 00003 M3X10 1S000017 it is in one of the data field in a table but i juz need to take the 1S000017 and trim away the 00003 M3X10... i need to this in a sq...
  2. Alternatives to &nbsp;
    Does anyone know of an alternate way to add spaces to HTML without using "&nbsp;"? Our users are opening an HTML page we have created in Excel. Everything is fine except the space convert to A0 in Excel and Excel doesn't know what to do with them.
  3. &nbsp; = &nbsp;
    Howdy! I have a problem where ASP.NET sometimes turns an "&nbsp;" into "&amp;nbsp;" when working with server based controls. I have created the code below to provide a simple example though it is in a different much more complex situation. If y...
  4. page is trimming the text.
    Hi everyone, I have a small problem. The page is trimming the text. For instance, if I have a text string which is " John, Doe ". The Page will display it as "John, Doe". Is there a simple to way to fix this? Thanks,

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: burakiewiczPosted on 2009-10-12 at 09:51:56ID: 25552810

does your querystring have all of the same values in it when you redirect looks like the links only provide 1 value like tablename or db, but the code is looking for both to be present. which line does it bomb on?

 

by: homeshopperPosted on 2009-10-12 at 10:08:48ID: 25552931

it does not like line 119  
dbTableName = Request.QueryString["tableName"].Trim(' ');
sometimes line 114
odbName = Request.QueryString["db"].Trim(' ');
How can I combine the two requests?
In the main page can I do the following:
<a href="cmsSchema.aspx?db=<%# DataBinder.Eval(Container.DataItem, "name")%>&tableName=<%# DataBinder.Eval(Container.DataItem, "Value")%>">
<a href="cmsSchema.aspx?tableName=<%# DataBinder.Eval(Container.DataItem, "Value")%>&db=<%# DataBinder.Eval(Container.DataItem, "name")%>">

 

by: homeshopperPosted on 2009-10-12 at 10:11:10ID: 25552949

I still get Object reference not set to an instance of an object
error at line 114
Line 112:        {
Line 113:            Response.Write("odbName:" + odbName + "&nbsp;&nbsp;");
Line 114:            odbName = Request.QueryString["db"].Trim(' ');
Line 115:        }
Line 116:        if (!(dbTableName == "")) ;

 

by: burakiewiczPosted on 2009-10-12 at 10:16:13ID: 25552988

it is because those items are not present in the querystring
try this and see if it bombs

if (Request.QueryString["db"]!=null)
{
           Response.Write("odbName:" + odbName + "&nbsp;&nbsp;");
            odbName = Request.QueryString["db"].Trim(' ');
}

 

by: homeshopperPosted on 2009-10-12 at 10:26:07ID: 25553099

As suggested made alterations to code and it has stopped bombing.
Thanks for the help.
I just need to now display the data from selected table.
The alteration as suggested is below:
if (Request.QueryString["db"] != null)
        {
            Response.Write("odbName:" + odbName + "&nbsp;&nbsp;");
            odbName = Request.QueryString["db"].Trim(' ');
        }
        if (Request.QueryString["tableName"] != null)
        {
            Response.Write("dbTableName:" + dbTableName + "&nbsp;&nbsp;");
            dbTableName = Request.QueryString["tableName"].Trim(' ');
        }

 

by: burakiewiczPosted on 2009-10-12 at 10:26:55ID: 25553105

try this
<a href="<%# ="cmsSchema.aspx?db=" + DataBinder.Eval(Container.DataItem, "name") +"&tableName=" + DataBinder.Eval(Container.DataItem, "Value")%>">

 

by: burakiewiczPosted on 2009-10-12 at 10:27:44ID: 25553114

oops with the #
<a href="<% ="cmsSchema.aspx?db=" + DataBinder.Eval(Container.DataItem, "name") +"&tableName=" + DataBinder.Eval(Container.DataItem, "Value")%>">

 

by: homeshopperPosted on 2009-10-12 at 12:10:17ID: 25554065

Hi,
I have tried as suggested:
<a href="<% ="cmsSchema.aspx?db=" + DataBinder.Eval(Container.DataItem, "name") +"&tableName=" + DataBinder.Eval(Container.DataItem, "Value")%>">
in place of:
<a href="cmsSchema.aspx?db=<%# DataBinder.Eval(Container.DataItem, "name")%>&tableName=<%# DataBinder.Eval(Container.DataItem, "Value")%>">
I get the following error:
The name 'Container' does not exist in the current context.
Do you have any further ideas? Many Thanks in advance.

 

by: burakiewiczPosted on 2009-10-12 at 12:12:06ID: 25554075

where are you getting the db and table name from?

 

by: homeshopperPosted on 2009-10-12 at 12:31:02ID: 25554229

Do you mean in the main aspx page.
<asp:Repeater id="Repeater1" runat="server">
      <HeaderTemplate>
      </HeaderTemplate>
      <ItemTemplate>
<a href="cmsSchema.aspx?db=<%# DataBinder.Eval(Container.DataItem, "name")%>">
      <%# DataBinder.Eval(Container.DataItem, "name")%></a>&nbsp;&nbsp;&nbsp;
      </ItemTemplate>
      <FooterTemplate>
      </FooterTemplate>
      </asp:Repeater><br />
<asp:Repeater runat="server" ID="repTest">
    <ItemTemplate>
<a href="cmsSchema.aspx?tableName=<%# DataBinder.Eval(Container.DataItem, "Value")%>">
<%# DataBinder.Eval(Container.DataItem, "Value")%></a>&nbsp;&nbsp;&nbsp;

 

by: homeshopperPosted on 2009-10-12 at 12:33:20ID: 25554250

code behind page:
//****************************************************
//cmsSchema.aspx.cs Code behind Page
//****************************************************
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Drawing;
using System.Data.OleDb;

public partial class cmsSchema : System.Web.UI.Page
{
    public string sSql;
    public string sDir;
    public bool bDatabaseExists = false;
    public bool bTableExists = false;
    public string sDBServer = System.Net.Dns.GetHostName();
    public string cboSQLServers = System.Net.Dns.GetHostName();
    public string sDataSource = "fuertemon";
    public object sDBServerProvider = "\\sqlexpress";
    public string sUSERid = "sa";
    public string sDBName = "Master";
    public string sDBPass = "h6web97";
    public string TempTable = "Employ";
    public string TempProcedure = "UpdateEmployDetails";
    public string strTable;
    public string strProcedure;
    public SqlConnection connection;
    public string ConnectionString;
    public string MsgBoxDatabase = null;
    public string MsgBoxTable = null;
    public string MessageBox = null;
    public string MessageBoxProcedure = null;
    public DataView Source;
    public DataSet Ds;
    public string sOutput;
    public string strResponse;
    public int i;
    public string tablePage;
    public string dbName;
    public string strCountdb;
    public string dbTableName;
    public string oTableName;
    public string strTbleCount;
    public string odbName;
    public string odbTableName;
    protected void Page_Load(object sender, System.EventArgs e)
    {
        HttpContext _Context = HttpContext.Current;
        if (Page.IsPostBack)
        {
           
        }
        if (!Page.IsPostBack)
        {
            BindGrid();
        }
        //****************************************************
        //Response.Write("1#" + Request["tablePage"]);
        LabelPage.Text = (Request["tablePage"]);
        //****************************************************
        SqlConnection sConn = null;
        SqlDataReader sReader = null;
        SqlCommand sComm = null;
        string sMyString = null;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        sMyString = Session["connStringValue"] + ";Initial Catalog=" + sDBName;
        sConn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        sComm = new System.Data.SqlClient.SqlCommand("Select * from sys.databases WHERE name NOT IN ('master','model','msdb','tempdb')", sConn);
        try
        {
            sConn.Open();
            sReader = sComm.ExecuteReader();
            Repeater1.DataSource = sReader;
            Repeater1.DataBind();
            sReader.Close();
        }
        catch (Exception ex)
        {
            // all other .NET exceptions
            Labelerr.Text = ex.ToString();
        }
        catch
        {
            // all other exceptions

        }
        finally
        {
            if (sConn.State != ConnectionState.Closed)
            {
                sConn.Close();
            }
        }
        //****************************************************
        Init();
        //testDatabase_SelectedIndexChanged(DropDownList1, Nothing)
        //****************************************************
        if (Request.QueryString["db"] != null)
        {
            Response.Write("odbName:" + odbName + "&nbsp;&nbsp;");
            odbName = Request.QueryString["db"].Trim(' ');
        }
        if (Request.QueryString["tableName"] != null)
        {
            Response.Write("dbTableName:" + dbTableName + "&nbsp;&nbsp;");
            dbTableName = Request.QueryString["tableName"].Trim(' ');
        }
        string oMyString = null;
        SqlDataReader odbreader = null;
        SqlCommand odbcomm = null;
        int oCounter = 0;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        oMyString = Session["connStringValue"] + ";Initial Catalog=" + odbName;
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(oMyString);
        SqlConnection odbconn = new SqlConnection(builder.ConnectionString);
        odbconn.Open();
        DataTable tblDatabases = odbconn.GetSchema(SqlClientMetaDataCollectionNames.Tables);
        odbconn.Close();
        ArrayList oItems = new ArrayList();
        ArrayList data = new ArrayList();            
        foreach (DataRow rowDatabase in tblDatabases.Rows)
        {
            if (!(string.IsNullOrEmpty(rowDatabase["table_name"].ToString())))
            {
                builder.InitialCatalog = rowDatabase["table_name"].ToString();
                odbconn.ConnectionString = builder.ConnectionString;
                oItems.Add(rowDatabase["table_name"].ToString());
                //hs.Add("Inv#", "001");
                data.Add(KV("Table:", rowDatabase["table_name"].ToString()));
            }
            oCounter += 1;
        }
        odbconn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        if (odbconn.State != ConnectionState.Closed)
        {
            odbconn.Close();
        }
        //grid.DataSource = oItems;
        //grid.DataBind();
        repTest.DataSource = data;
        repTest.DataBind();
        //Session["oItems"] = oItems;
        //Session["oCounter"] = oCounter;
        //****************************************************
        //****************************************************************
        SqlConnection oconn = null;
        SqlDataReader reader2 = null;
        SqlCommand ocomm = null;
        bool First = false;
        string sqlstmt = null;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        //oMyString = Session["connStringValue"] + ";Initial Catalog=" + odbName;
        oMyString = Session["connStringValue"] + ";Initial Catalog=ianwebb";
        oconn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        //ocomm = new System.Data.SqlClient.SqlCommand("SP_Columns '" + Request["tableName"] + "'", oconn);
        ocomm = new System.Data.SqlClient.SqlCommand("SP_Columns '" + "HelpDesk" + "'", oconn);
        try
        {
            oconn.Open();
            reader2 = ocomm.ExecuteReader();
            Hashtable TypeTable = new Hashtable();
            object[] array = new object[101];
            var counter = 0;
            while (reader2.Read())
            {
                TypeTable.Add(reader2["Column_Name"], reader2["Type_Name"]);
                array[counter] = reader2["Column_Name"];
                counter += 1;
            }
            reader2.Close();
        }
        catch (Exception ex)
        {
            // all other .NET exceptions
            Labelerr.Text = ex.ToString();
        }
        catch
        {
            // all other exceptions

        }
        finally
        {
            if (oconn.State != ConnectionState.Closed)
            {
                oconn.Close();
            }
        }

        //****************************************************
    }
    // KV for KeyValue is defined as returning an anonymous object:
    private object KV(string Key, string Value)
        {
            return new { Key, Value };
        }
    private static new void Init()
    {
        // initiate variables etc
    }
    protected override void OnInit(EventArgs e)
    {
        //MyBase.OnInit(e);
        //Converted event handler wireups:
        Load += Page_Load;
        //AddHandler DropDownList1.SelectedIndexChanged, AddressOf testDatabase_SelectedIndexChanged
        //AddHandler DropDownList1.SelectedIndexChanged, AddressOf DropDownList1_SelectedIndexChanged
        //AddHandler grid.SelectedIndexChanged, AddressOf grid_SelectedIndexChanged
        //AddHandler empDetails.PageIndexChanging, AddressOf empDetails_PageIndexChanging1
        //AddHandler empDetails.ModeChanging, AddressOf empDetails_ModeChanging
        //AddHandler empDetails.ItemUpdating, AddressOf empDetails_ItemUpdating
        //AddHandler empDetails.ItemUpdated, AddressOf empDetails_ItemUpdated
    }
    public void BindGrid()
    {
        //****************************************************

        //****************************************************
    }
    protected void grid_SelectedIndexChanged(object sender, EventArgs e)
    {
        //****************************************************

        //****************************************************
    }
}

 

by: burakiewiczPosted on 2009-10-12 at 12:53:40ID: 25554403

what happens if you try  something like this
<a href="<% ="cmsSchema.aspx?db=" + DataBinder.Eval(DataItem, "name") +"&tableName=" + DataBinder.Eval(DataItem, "Value")%>">
or
<a href="<% ="cmsSchema.aspx?db=" + DataBinder.Eval( "name") +"&tableName=" + DataBinder.Eval( "Value")%>">

 

by: homeshopperPosted on 2009-10-12 at 13:02:11ID: 25554472

The first one gives error:
the name 'DataItem' does not exist in current context
<a href="<% ="cmsSchema.aspx?db=" + DataBinder.Eval(DataItem, "name") +"&tableName=" + DataBinder.Eval(DataItem, "Value")%>">
The second gives error:
No overload for method 'Eval' takes '1' arguments.
<a href="<% ="cmsSchema.aspx?db=" + DataBinder.Eval( "name") +"&tableName=" + DataBinder.Eval( "Value")%>">

 

by: burakiewiczPosted on 2009-10-12 at 13:11:02ID: 25554541

the easiest way is probably to assign ids to the hyperlinks, and then in the databound event use findcontrol to find the html links and set the urls up from there

 

by: homeshopperPosted on 2009-10-12 at 13:30:45ID: 25554729

I have used findcontrol only as follows:
I am not familiar in their use as per your suggestion.
Is it possible to give an example, then I might be able to figure it out.
Once again, many thanks.
// Read the employee from the DetailsView object
    int employeeId = (int)employeeDetails.DataKey.Value;
    // Find the TextBox controls with updated data
    TextBox newAddressTextBox =
       (TextBox)employeeDetails.FindControl("editAddressTextBox");
    TextBox newCityTextBox =
       (TextBox)employeeDetails.FindControl("editCityTextBox");
    // Extract the updated data from the TextBoxes
    string newAddress = newAddressTextBox.Text;
    string newCity = newCityTextBox.Text;
    // Define data objects
    SqlConnection conn;
    SqlCommand comm;
    // Initialize connection
    string connectionString =

 

by: burakiewiczPosted on 2009-10-12 at 13:49:21ID: 25554913

ok, this is a very basic example, you  will need to chage your html links to asp:Hyperlinks
.aspx
  <asp:Repeater ID="Repeater1" runat="server"
     onitemcreated="Repeater1_ItemCreated"
       >
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>

    </ItemTemplate>
    </asp:Repeater>

   protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable hsh = new Hashtable();
            hsh.Add("1", "1");
            hsh.Add("2", "1");
            hsh.Add("3", "1");
            hsh.Add("4", "1");
            hsh.Add("5", "1");
            Repeater1.DataSource = hsh;
            Repeater1.DataBind();
        }
 
        
 
        protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || 
                e.Item.ItemType== ListItemType.AlternatingItem)
            {
                HyperLink hl = (HyperLink)e.Item.FindControl("HyperLink1");
                if (hl != null)
                {
                    hl.NavigateUrl= "www.msn.com?" + ((DictionaryEntry)e.Item.DataItem).Value;
                }
            }
        }

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:

Select allOpen in new window

 

by: homeshopperPosted on 2009-10-12 at 14:37:08ID: 25555292

I have tried the suggestion with my second repeater.
I get two errors:
error:1: no overload for method 'Add' takes 1 argument.
hsh.Add(KV("Table:", rowDatabase["table_name"].ToString()));
error:2: 'ASP.app_cmsschema_aspx' does not contain a definition for 'repTest_ItemCreated' and no extension method 'repTest_ItemCreated' accepting a first argument of type 'ASP.app_cmsschema_aspx' could be found (are you missing a using directive or an assembly reference?)
My code so far is as follows:
*.aspx
<asp:Repeater ID="repTest" runat="server"
     onitemcreated="repTest_ItemCreated">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
    </ItemTemplate>
    </asp:Repeater>  
*.aspx.cs
string oMyString = null;
        SqlDataReader odbreader = null;
        SqlCommand odbcomm = null;
        int oCounter = 0;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        oMyString = Session["connStringValue"] + ";Initial Catalog=" + odbName;
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(oMyString);
        SqlConnection odbconn = new SqlConnection(builder.ConnectionString);
        odbconn.Open();
        DataTable tblDatabases = odbconn.GetSchema(SqlClientMetaDataCollectionNames.Tables);
        odbconn.Close();
        ArrayList oItems = new ArrayList();
        Hashtable hsh = new Hashtable();        
        foreach (DataRow rowDatabase in tblDatabases.Rows)
        {
            if (!(string.IsNullOrEmpty(rowDatabase["table_name"].ToString())))
            {
                builder.InitialCatalog = rowDatabase["table_name"].ToString();
                odbconn.ConnectionString = builder.ConnectionString;
                oItems.Add(rowDatabase["table_name"].ToString());
                //hs.Add("Inv#", "001");
                hsh.Add(KV("Table:", rowDatabase["table_name"].ToString()));
            }
            oCounter += 1;
        }
        odbconn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        if (odbconn.State != ConnectionState.Closed)
        {
            odbconn.Close();
        }
        repTest.DataSource = hsh;
        repTest.DataBind();

Also code as follows:
 protected void repTest_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //****************************************************
        if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hl = (HyperLink)e.Item.FindControl("HyperLink1");
            if (hl != null)
            {
                hl.NavigateUrl = "cmsSchema.aspx?" + ((DictionaryEntry)e.Item.DataItem).Value;
            }
        }
        //****************************************************
    }
    // KV for KeyValue is defined as returning an anonymous object:
    private object KV(string Key, string Value)
        {
            return new { Key, Value };
        }
      

 

by: homeshopperPosted on 2009-10-12 at 15:28:33ID: 25555633

curred 1 error:
<asp:Repeater ID="repTest" runat="server"
     OnItemCommand="repTest_ItemCommand1">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
    </ItemTemplate>
    </asp:Repeater>  
protected void repTest_ItemCommand1(object source, RepeaterCommandEventArgs e)
    {
        //****************************************************
        if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hl = (HyperLink)e.Item.FindControl("HyperLink1");
            if (hl != null)
            {
                hl.NavigateUrl = "cmsSchema.aspx?" + ((DictionaryEntry)e.Item.DataItem).Value;
            }
        }
        //****************************************************
    }
    // KV for KeyValue is defined as returning an anonymous object:
    private object KV(string Key, string Value)
        {
            return new { Key, Value };
        }
Still one error:
string oMyString = null;
        SqlDataReader odbreader = null;
        SqlCommand odbcomm = null;
        int oCounter = 0;
        Session["connStringValue"] = "server=" + sDBServer + sDBServerProvider + ";uid=" + sUSERid + ";pwd=" + sDBPass;
        oMyString = Session["connStringValue"] + ";Initial Catalog=" + odbName;
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(oMyString);
        SqlConnection odbconn = new SqlConnection(builder.ConnectionString);
        odbconn.Open();
        DataTable tblDatabases = odbconn.GetSchema(SqlClientMetaDataCollectionNames.Tables);
        odbconn.Close();
        ArrayList oItems = new ArrayList();
        Hashtable hsh = new Hashtable();        
        foreach (DataRow rowDatabase in tblDatabases.Rows)
        {
            if (!(string.IsNullOrEmpty(rowDatabase["table_name"].ToString())))
            {
                builder.InitialCatalog = rowDatabase["table_name"].ToString();
                odbconn.ConnectionString = builder.ConnectionString;
                oItems.Add(rowDatabase["table_name"].ToString());
                //hsh.Add("Inv#", "001");
                hsh.Add("1", "1");
                hsh.Add("2", "1");
                hsh.Add("3", "1");
                hsh.Add("4", "1");
                hsh.Add("5", "1");
                //hsh.Add(KV("Table:", rowDatabase["table_name"].ToString()));
            }
            oCounter += 1;
        }
        odbconn = new System.Data.SqlClient.SqlConnection(Session["connStringValue"].ToString());
        if (odbconn.State != ConnectionState.Closed)
        {
            odbconn.Close();
        }
        repTest.DataSource = hsh;
        repTest.DataBind();
second error:
Item has already been added. Key in dictionary: '1'  Key being added: '1'

                hsh.Add("1", "1");
                hsh.Add("2", "1");
                hsh.Add("3", "1");
                hsh.Add("4", "1");
                hsh.Add("5", "1");
I tried putting code as follows:
hsh.Add(KV("Table:", rowDatabase["table_name"].ToString()));

 

by: homeshopperPosted on 2009-10-12 at 15:31:43ID: 25555651

Hi burakiewicz
It is evry late, I am going to a have fresh look at it tomorrow morning.
Thanks for all the help. Regards, Ian.

 

by: burakiewiczPosted on 2009-10-13 at 06:51:44ID: 25559891

yeah a hashtable takes a key value pair that why add doesnt take 1 argument
since it will run the code below more than once it tries adding the keys again

 foreach (DataRow rowDatabase in tblDatabases.Rows)
        {
            if (!(string.IsNullOrEmpty(rowDatabase["table_name"].ToString())))
            {
                builder.InitialCatalog = rowDatabase["table_name"].ToString();
                odbconn.ConnectionString = builder.ConnectionString;
                oItems.Add(rowDatabase["table_name"].ToString());
                //hsh.Add("Inv#", "001");
                hsh.Add("1", "1");
                hsh.Add("2", "1");
                hsh.Add("3", "1");
                hsh.Add("4", "1");
                hsh.Add("5", "1");
                //hsh.Add(KV("Table:", rowDatabase["table_name"].ToString()));
            }
            oCounter += 1;
        }
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...