Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,700 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,505 people online right now. Registration is fast and FREE... Join Now!




C# / SQL / DataSet

 
Reply to this topicStart new topic

C# / SQL / DataSet

webwired
post 4 Oct, 2008 - 05:41 AM
Post #1


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 122



Thanked 1 times

Dream Kudos: 100
My Contributions


Hi, I was able to use PsychoCoder's code for C# and SQL for working with BindingSources and DataGridViews, but modifying that code to work with DataSets and pulling individual fields has proven a tad more complicated...

What I'd like to do is populate text boxes with a single entry result ... Here's my code for one of those instances...
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace MassageProCS
{
    public partial class LogInForm : Form
    {

        public int TID = 0;

        public LogInForm()
        {
            InitializeComponent();
        }

        private void LogInForm_Load(object sender, EventArgs e)
        {

        }

        private void logInButton_Click(object sender, EventArgs e)
        {
            string sSQL = "ValidateLogin";
            SqlConnection cnGetRecords = new SqlConnection(DataAccess.GetConnectionString("MassageProCS.Properties.Settings.MassageProConnectionString"));
            SqlCommand cmdGetRecords = new SqlCommand();
            SqlDataAdapter daGetRecords = new SqlDataAdapter();
            DataSet dsGetRecords = new DataSet();
            cmdGetRecords.Parameters.Clear();
            cmdGetRecords.CommandText = sSQL;
            cmdGetRecords.CommandType = CommandType.StoredProcedure;
            cmdGetRecords.Parameters.AddWithValue("@TherapistName", therapistNameTextBox.Text);
            cmdGetRecords.Parameters.AddWithValue("@TherapistPassword", therapistPasswordTextBox.Text);
            DataAccess.HandleConnection(cnGetRecords);
            daGetRecords.SelectCommand = cmdGetRecords;
            daGetRecords.Fill(dsGetRecords);
            string tempID = dsGetRecords.Tables["Therapists"].Columns["TherapistID"].ToString();
            TID = Convert.ToInt16(tempID);
            if (TID > 0)
            {
                // Temporary, for testing purposes...
                MessageBox.Show(TID.ToString());
            }
            else
            {
                MessageBox.Show("You have entered a wrong username and/or password.");
            }
        }
    }
}


But I am coming up with this error...
QUOTE

Fill: SelectCommand.Connection property has not been initialized.

For this line of code...
CODE

daGetRecords.Fill(dsGetRecords);
User is offlineProfile CardPM

Go to the top of the page

webwired
post 4 Oct, 2008 - 06:04 AM
Post #2


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 122



Thanked 1 times

Dream Kudos: 100
My Contributions


Ok, figured out that I was missing this line of code...
CODE

cmdGetRecords.Connection = cnGetRecords;


So that took care of that error... but now I have another one...
QUOTE

Object reference not set to an instance of an object.

On this line of code...
CODE

string tempID = dsGetRecords.Tables["Therapists"].Columns["TherapistID"].ToString();

So I tried changing it to this...
CODE

Object tempID = new Object(dsGetRecords.Tables["Therapists"].Columns["TherapistID"]);

But that didn't work...

This post has been edited by webwired: 4 Oct, 2008 - 06:20 AM
User is offlineProfile CardPM

Go to the top of the page

webwired
post 4 Oct, 2008 - 07:02 AM
Post #3


D.I.C Head

Group Icon
Joined: 26 Aug, 2007
Posts: 122



Thanked 1 times

Dream Kudos: 100
My Contributions


I got it...
CODE


            SqlDataReader rdr = null;
            rdr = cmdGetRecords.ExecuteReader();
            while (rdr.Read())
            {
                TID = (int)rdr["TherapistID"];
            }
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 08:58AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month