Click here to Skip to main content
15,897,891 members
Home / Discussions / C#
   

C#

 
AnswerRe: List view question Pin
al3xutzu0015-Apr-09 10:10
al3xutzu0015-Apr-09 10:10 
Questionweb develpoment Pin
elazor15-Apr-09 9:46
elazor15-Apr-09 9:46 
AnswerRe: web develpoment Pin
buachaill cliste15-Apr-09 10:02
buachaill cliste15-Apr-09 10:02 
GeneralRe: web develpoment Pin
elazor15-Apr-09 19:14
elazor15-Apr-09 19:14 
QuestionSpreadsheet COM in window form application Pin
Binqing15-Apr-09 8:47
Binqing15-Apr-09 8:47 
AnswerRe: Spreadsheet COM in window form application Pin
buachaill cliste15-Apr-09 10:40
buachaill cliste15-Apr-09 10:40 
GeneralRe: Spreadsheet COM in window form application Pin
Binqing15-Apr-09 11:01
Binqing15-Apr-09 11:01 
QuestionCreating a DataTable with the data from an SQL SELECT statement Pin
stonebergftw15-Apr-09 8:31
stonebergftw15-Apr-09 8:31 
I'm having a rough time creating a DataTable with the contents of an SQL "SELECT" and I'm pretty sure I'm just missing 1 line of code. The program runs without error as is, but the DataTable myTable remains null even after txtMemNum_TextChanged is run.

The goal of the method txtMemNum_TextChanged will be to continually populate a DataGrid with potential matches from an SQL server as the user types the member number in the textbox txtMemNum. Any help is appreciated. -Tim

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private SqlConnection OpenConnection()
        {
            SqlConnection myConnection = null;
            string connectionString = "Integrated Security=SSPI;" + "Initial Catalog=Sales;" + "Data Source=PC-HERE\\SQLEXPRESS;";
             
            try
            {
                myConnection = new SqlConnection(connectionString);
                myConnection.Open();
                //MessageBox.Show("Connection OK!");
                return myConnection;   
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in connection: " + ex.Message);
                myConnection = null;
                return myConnection;
            }
        }
        
        public void AddRow(SqlConnection myConnection, long memNum)
        {
            //FUNCTION SUMMARY
            //----------------
            //Adds a new row to the Members Table in the database
            //Only for new members
            //END SUMMARY
            
            //--------- UPDATE THE 3 BELOW TO LOOP ----------//
            //Update Database Table - Members
            DataSet mySet = new DataSet();
            string selectString = "INSERT INTO Members ([Member Number]) VALUES (" + memNum + ")";
            SqlCommand myCommand = myConnection.CreateCommand();
            myCommand.CommandText = selectString;
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myCommand;
            myAdapter.Fill(mySet, "Members");

            //Update Database Table - Sales
            mySet = new DataSet();
            selectString = "INSERT INTO Sales ([Member Number]) VALUES (" + memNum + ")";
            myCommand = myConnection.CreateCommand();
            myCommand.CommandText = selectString;
            myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myCommand;
            myAdapter.Fill(mySet, "Sales");

            //Update Database Table - Location
            mySet = new DataSet();
            selectString = "INSERT INTO Location ([Member Number]) VALUES (" + memNum + ")";
            myCommand = myConnection.CreateCommand();
            myCommand.CommandText = selectString;
            myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myCommand;
            myAdapter.Fill(mySet, "Location");
            //--------END LOOP-------//
            myConnection.Close();

            int hi = 0; // For breakpoint
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void AddTruck_Click(object sender, EventArgs e)
        {
            long memNum = long.Parse(txtMemNum.Text);
            SqlConnection myConnection = new SqlConnection();
            myConnection = OpenConnection();
            AddRow(myConnection, memNum);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void txtMemNum_TextChanged(object sender, EventArgs e)
        {
            // THIS MODULE UPDATES THE DATAGRIDVIEW UPON TYPING
            long memNum = long.Parse(txtMemNum.Text);
            SqlConnection myConnection = new SqlConnection();
            
            DataSet tableSet = new DataSet();
            string selectString = "SELECT * FROM Members WHERE [Member Number] LIKE '" + memNum + "%'";
            myConnection = OpenConnection(); 
            SqlCommand myCommand = myConnection.CreateCommand();
            myCommand.CommandText = selectString;
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myCommand;
            
            //Do I need to set tableSet to something before running the next line?
            DataTable myTable = tableSet.Tables["Members"];
            
            int hi = 0; // For Breakpoint
        }

    }
}

AnswerRe: Creating a DataTable with the data from an SQL SELECT statement Pin
PIEBALDconsult15-Apr-09 10:17
mvePIEBALDconsult15-Apr-09 10:17 
GeneralRe: Creating a DataTable with the data from an SQL SELECT statement Pin
stonebergftw15-Apr-09 11:02
stonebergftw15-Apr-09 11:02 
GeneralRe: Creating a DataTable with the data from an SQL SELECT statement Pin
PIEBALDconsult16-Apr-09 6:59
mvePIEBALDconsult16-Apr-09 6:59 
QuestionMultithreaded remoting in C# Pin
NickoT15-Apr-09 7:46
NickoT15-Apr-09 7:46 
AnswerRe: Multithreaded remoting in C# [modified] Pin
DaveyM6915-Apr-09 9:45
professionalDaveyM6915-Apr-09 9:45 
Questiongetting a handle to an embedded flash movie inside a webbrowser Pin
jeanbern15-Apr-09 7:00
jeanbern15-Apr-09 7:00 
QuestionCannot connect to SQL Server 2005 Pin
arimao15-Apr-09 6:38
arimao15-Apr-09 6:38 
AnswerRe: Cannot connect to SQL Server 2005 Pin
stonebergftw15-Apr-09 8:54
stonebergftw15-Apr-09 8:54 
GeneralRe: Cannot connect to SQL Server 2005 Pin
arimao15-Apr-09 9:14
arimao15-Apr-09 9:14 
GeneralRe: Cannot connect to SQL Server 2005 Pin
stonebergftw15-Apr-09 10:32
stonebergftw15-Apr-09 10:32 
QuestionHow to Scan Remote PC For UDP Open & Closed Ports? Pin
alparody15-Apr-09 6:34
alparody15-Apr-09 6:34 
QuestionInvoking a WMI method on a Remote Machine Pin
bbranded15-Apr-09 6:05
bbranded15-Apr-09 6:05 
AnswerRe: Invoking a WMI method on a Remote Machine Pin
bbranded15-Apr-09 8:22
bbranded15-Apr-09 8:22 
Questionsynchronous and asynchronous communication Pin
yesu prakash15-Apr-09 5:52
yesu prakash15-Apr-09 5:52 
AnswerRe: synchronous and asynchronous communication Pin
0x3c015-Apr-09 6:29
0x3c015-Apr-09 6:29 
GeneralRe: synchronous and asynchronous communication Pin
yesu prakash15-Apr-09 18:08
yesu prakash15-Apr-09 18:08 
QuestionProblem in disabling close button(X) of window. Pin
priyamtheone15-Apr-09 5:25
priyamtheone15-Apr-09 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.