Thread: c# & SQL newbie

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    c# & SQL newbie

    Hi I am learning to code in C#. But I am stuck at one place. I am trying to connect to SQL server through C#

    I am using VS 2005 and below is the code.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            System.Data.SqlClient.SqlConnection con;
            DataSet ds1;
            System.Data.SqlClient.SqlDataAdapter da;
            DataRow dRow;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                con = new System.Data.SqlClient.SqlConnection();
                ds1 = new DataSet();
                con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\TransNo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                
                con.Open();
                
                string sql = "SELECT * From tblTransNo";
                da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
    
                da.Fill(ds1, "TransNo");
                NavigateRecords();
                con.Close();
            }
    
            private void NavigateRecords()
            {
                DataRow dRow = dsl.Tables["TransNo"].Rows[0];
    
                textBox1.Text = dRow.ItemArray.GetValue(2).ToString();
            }
        }
    }

    Here are the errors I am getting

    Code:
    Error	1	The name 'dsl' does not exist in the current context	C:\Documents and Settings\User\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\Form1.cs	40	28	WindowsApplication2
    Any help is appreciated.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Safari tells me there is one instance of "DSL" and 3 instances of "DS1". Looks like a typo.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Thanks Dino!!!...I guess I have to learn how to troubleshoot these errors. Spent 1 hours looking what's wrong with it

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    The joys of programming.
    Never re-write code unless the user benefits

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Please help create a loop for embedded SQL
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 04-24-2008, 06:46 AM
  3. Embedded SQL
    By sarac in forum C Programming
    Replies: 1
    Last Post: 05-04-2006, 09:09 AM
  4. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  5. Problem with embedded SQL in C/C++ (ECPG)
    By NeuralClone in forum C Programming
    Replies: 4
    Last Post: 10-21-2005, 05:16 PM