<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Data Presentation Controls</title><link>http://forums.asp.net/24.aspx</link><description>ASP.NET data-bound controls such as the DataGrid, DataList, GridView, FormView, DetailsView, and Repeater Controls.  &lt;a href="http://aspadvice.com/SignUp/list.aspx?l=23&amp;c=17" target="_blank"&gt;Email List&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: simple Connection to a database</title><link>http://forums.asp.net/thread/3248385.aspx</link><pubDate>Sun, 21 Jun 2009 04:49:41 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3248385</guid><dc:creator>PeteNet</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3248385.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=24&amp;PostID=3248385</wfw:commentRss><description>here&amp;#39;s what you can use:&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="coloredcode"&gt;        &lt;span class="cmt"&gt;// conn and reader declared outside try&lt;br /&gt;		// block for visibility in finally block&lt;/span&gt;
		SqlConnection conn   = &lt;span class="kwd"&gt;null&lt;/span&gt;;&lt;br /&gt;		SqlDataReader reader = &lt;span class="kwd"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;		&lt;span class="kwd"&gt;try&lt;/span&gt;
		{
            &lt;span class="kwd"&gt;string&lt;/span&gt; connstr = ConfigurationManager.ConnectionStrings[&lt;span class="st"&gt;&amp;quot;UnityConnectionString&amp;quot;&lt;/span&gt;].ToString();&lt;br /&gt;			&lt;span class="cmt"&gt;// instantiate and open connection&lt;/span&gt;
			conn =  &lt;span class="kwd"&gt;new&lt;/span&gt; &lt;br /&gt;				SqlConnection(connstr);&lt;br /&gt;			conn.Open();&lt;br /&gt;&lt;br /&gt;&lt;span class="cmt"&gt;			// 1. declare command object with parameter&lt;/span&gt;
			SqlCommand cmd = &lt;span class="kwd"&gt;new&lt;/span&gt; SqlCommand(&lt;br /&gt;                &lt;span class="st"&gt;&amp;quot;Select * from LG_006_CLCARD WHERE DEFINITION_= @DEF&amp;quot;&lt;/span&gt;, conn);&lt;br /&gt;   //assuming your table in the database is indeed called &amp;#39;&lt;span class="st"&gt;DEFINITION_&lt;/span&gt;&amp;#39;&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&lt;br /&gt;			&lt;span class="cmt"&gt;// 2. define parameters used in command object&lt;/span&gt;
			SqlParameter param  = &lt;span class="kwd"&gt;new&lt;/span&gt; SqlParameter();&lt;br /&gt;            param.ParameterName = &lt;span class="st"&gt;&amp;quot;@DEF&amp;quot;&lt;/span&gt;;&lt;br /&gt;            param.Value = txtAccountCode.Text;&lt;br /&gt;&lt;br /&gt;			&lt;span class="cmt"&gt;// 3. add new parameter to command object&lt;/span&gt;
			cmd.Parameters.Add(param);

			&lt;span class="cmt"&gt;// get data stream&lt;/span&gt;
			reader = cmd.ExecuteReader();

			&lt;span class="cmt"&gt;// write each record&lt;/span&gt;
			&lt;span class="kwd"&gt;while&lt;/span&gt;(reader.Read())&lt;br /&gt;			{&lt;br /&gt;                &lt;span class="cmt"&gt;//Response.Write(&amp;quot;{0}, {1}&amp;quot;, &lt;br /&gt;                //    reader[&amp;quot;column1&amp;quot;],&lt;br /&gt;                //    reader[&amp;quot;column1&amp;quot;]);&lt;/span&gt;
                Label6.Text = txtAccountCode.Text;  &lt;span class="cmt"&gt;//will display the value in the last record if there are multiple rows found for the select criteria&lt;/span&gt;
			}

            &lt;span class="cmt"&gt;//GridView1.DataSource = reader;&lt;br /&gt;            //GridView1.DataBind();&lt;/span&gt;
		}
		&lt;span class="kwd"&gt;finally&lt;/span&gt;
		{
			&lt;span class="cmt"&gt;// close reader&lt;/span&gt;
			&lt;span class="kwd"&gt;if&lt;/span&gt; (reader != &lt;span class="kwd"&gt;null&lt;/span&gt;)&lt;br /&gt;			{&lt;br /&gt;				reader.Close();&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;			&lt;span class="cmt"&gt;// close connection&lt;/span&gt;
			&lt;span class="kwd"&gt;if&lt;/span&gt; (conn != &lt;span class="kwd"&gt;null&lt;/span&gt;)&lt;br /&gt;			{&lt;br /&gt;				conn.Close();&lt;br /&gt;			}&lt;br /&gt;		}&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="coloredcode"&gt;you don&amp;#39;t need the gridview to simply test a select, step through the &lt;span class="kwd"&gt;while&lt;/span&gt;(reader.Read()) loop or let it write to the label&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&amp;nbsp;here&amp;#39;s a link that might help: &lt;a href="http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx"&gt;http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx &lt;/a&gt;&lt;/pre&gt;&lt;pre class="coloredcode"&gt;besides, there are many tutorials on this site itself that might help you work your way up.&lt;a href="http://www.asp.net/learn/data-access/"&gt;http://www.asp.net/learn/data-access/&lt;/a&gt; &lt;br /&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;</description></item><item><title>simple Connection to a database</title><link>http://forums.asp.net/thread/3248301.aspx</link><pubDate>Sun, 21 Jun 2009 03:31:03 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3248301</guid><dc:creator>khparhami</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3248301.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=24&amp;PostID=3248301</wfw:commentRss><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I&amp;#39;m realy lazy in database programming and I don&amp;#39;t know why! I would like to know the best way of retrieving data from a database to fill out a few tex boxes. I have a form that will be filled by users but the header sould be filled from a database according a selected code:&lt;/p&gt;
&lt;p&gt;I tried a few codes I found on the net but I don&amp;#39;t know what the problem is because the data has not being shown. to test if the data is available I used a gridview as follow:&lt;/p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlConnection&lt;/font&gt;&lt;font size="2"&gt; sqlCon;&lt;/p&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlCommand&lt;/font&gt;&lt;font size="2"&gt; sqlComm;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlDataReader&lt;/font&gt;&lt;font size="2"&gt; SqlDr;&lt;/p&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlParameter&lt;/font&gt;&lt;font size="2"&gt; paramDef;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt; strConstr = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;ConfigurationManager&lt;/font&gt;&lt;font size="2"&gt;.ConnectionStrings[&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&amp;quot;UnityConnectionString&amp;quot;&lt;/font&gt;&lt;font size="2"&gt;].ConnectionString;&lt;/p&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt; strComm = &lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&amp;quot;Select * from LG_006_CLCARD WHERE DEFINITION_= @DEF&amp;quot;&lt;/font&gt;&lt;font size="2"&gt;;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;sqlCon = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlConnection&lt;/font&gt;&lt;font size="2"&gt;();&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;sqlCon.ConnectionString = strConstr;&lt;/p&gt;sqlComm = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlCommand&lt;/font&gt;&lt;font size="2"&gt;();&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;sqlComm.CommandText = strComm;&lt;/p&gt;sqlComm.CommandType = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;CommandType&lt;/font&gt;&lt;font size="2"&gt;.Text;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;sqlComm.Connection = sqlCon;&lt;/p&gt;
&lt;p&gt;paramDef = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlParameter&lt;/font&gt;&lt;font size="2"&gt;();&lt;/p&gt;paramDef.ParameterName = &lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&amp;quot;@DEF&amp;quot;&lt;/font&gt;&lt;font size="2"&gt;;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;paramDef.SqlDbType = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;SqlDbType&lt;/font&gt;&lt;font size="2"&gt;.VarChar;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;paramDef.Size = 17;&lt;/p&gt;paramDef.Direction = &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;ParameterDirection&lt;/font&gt;&lt;font size="2"&gt;.Input;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;paramDef.Value = txtAccountCode.Text;&lt;/p&gt;
&lt;p&gt;sqlComm.Parameters.Add(paramDef);&lt;/p&gt;
&lt;p&gt;sqlComm.Connection.Open();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;SqlDr = sqlComm.ExecuteReader(&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;CommandBehavior&lt;/font&gt;&lt;font size="2"&gt;.CloseConnection);&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;Label6.Text = txtAccountCode.Text;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;GridView1.DataSource = SqlDr;&lt;/p&gt;
&lt;p&gt;GridView1.DataBind();&lt;/p&gt;
&lt;p&gt;sqlComm.Dispose();&lt;/p&gt;
&lt;p&gt;sqlCon.Dispose();&lt;/p&gt;&lt;/font&gt;
&lt;p&gt;please advise how to do it and how to make my database knowledge more powerfull!&lt;/p&gt;</description></item></channel></rss>