Sunday, March 11, 2012

Data Connection to SQL Server 2000 using C#

I have asp code that uses C# to create a SQL connection and a command. Then I create a data set and fill the data set using my comand. Finally I bind my data set to a data grid named myDataGrid. I am not sure where myDataGrid comes from since it is never defined. Where does myDataGrid come from? Also, I believe that I do not need to call page_Load since auto wire is set to true by default so it is my understanding that this method page_Load will be called by default.
void page_load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server='(local)'; trusted_connection=true; database='mattsDEMO'"); SqlDataAdapter myCommand = new SqlDataAdapter("select * from people", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "people");
MyDataGrid.DataSource=ds.Tables["people"].DefaultView;
MyDataGrid.DataBind();
}

When I place a data grid controll on my page named myDataGrid, I do not see a data grid and there is no data either. The code in the HTML looks like this:

<body>
<form runat="server">
<p>
<asp:DataGrid id="myDataGrid" runat="server" BorderColor="Maroon"></asp:DataGrid>
</p>
</form>
</body>
I tried using debug that comes with the .Net framework (since I am using Web Matrix) but I get an error saying no symbols were found for this document when setting break points. What are symbols and why do I need them?
So, I basically have four (4) important questions that I need help answering:
1) Why do I not have any data in my data grid?
2) Where did myDataGrid come from?
3) Is the page_Load method called by default if auto wire is set to true or not declared?
4) Why do I need symbols for debug?
Below is all my code in one complete view:
<%@. Page Language="C#" %>
<%@. Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %>
<%@. import Namespace="System.Data" %>
<%@. import Namespace="System.Data.SqlClient" %>
<script runat="server">

// Insert page code here
//

</script>
<html>
<head>
<script language="C#">
void page_load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server='(local)'; trusted_connection=true; database='mattsDEMO'");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from people", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "people");

MyDataGrid.DataSource=ds.Tables["people"].DefaultView;
MyDataGrid.DataBind();
}

</script>
</head>
<body>
<form runat="server">
<p>

</p>
<p>
<asp:Label id="Label1" runat="server">This is a test!</asp:Label>
</p>
<p>
<asp:DataGrid id="myDataGrid" runat="server" BorderColor="Maroon"></asp:DataGrid>
</p>
</form>
</body>
</html>
Thanks,
Ryan

(1) you could put a if not ispostback condition in your page_load.
(2) You could set the autogeneratecolumns property of the datagrid to true. I am not good at C#. So I wouldnt try writing code for you.

No comments:

Post a Comment