Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Create table dynamically after looking-up its value

$
0
0

Hello,

I created a SQL database and a master table, if I look-up a student I would like to pass the colour to a DataTable and create a new table. I followed all the tutorials but my table wont create. I also think there is an error in passing a value.

So frustrated

// DataRetrieve
// a simple program to learn how to access Sql and retrieve data and display in a label
//
// Data base    Database1.mdf
// Table        Student
// Fields       StudentId   int
//              Name        NVARCHAR (50)
//              Surname     NVARCHAR (50)
//              Colour      NCHAR (10)
//
// after retrieving the name, use the colour to create a table dynamically with 2 fields
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace DataRetrieve
{
    public partial class StudentLookUp : Form
    {
        SqlConnection connection;
        string connectionString;
        SqlCommand command;
        SqlDataReader sdr;

        public StudentLookUp()
        {
            InitializeComponent();
            connectionString = ConfigurationManager.ConnectionStrings["DataRetrieve.Properties.Settings.Database1ConnectionString"].ConnectionString;
        }

        private void BtnStudentLookUp_Click(object sender, EventArgs e)
        {
            //using (connection = new SqlConnection(connectionString));
            connection = new SqlConnection(connectionString);
            string selectQuery = "SELECT * FROM Student WHERE StudentId=" + int.Parse(txtStudentId.Text);

            command = new SqlCommand(selectQuery, connection);
            connection.Open();
            sdr = command.ExecuteReader();      //Sql Data Reader

            //Looking-up student details
            if (sdr.Read())
            {
                lblStudentName.Text     = (sdr["Name"].ToString());
                lblStudentSurname.Text  = (sdr["Surname"].ToString());
                lblStudentColour.Text   = (sdr["Colour"].ToString());

                //Name the new table to the colour
                String
                StudentTable = (sdr["Colour"].ToString());      // IS THIS CORRECT?
            }

            else
            {
                lblStudentName.Text     = "";
                lblStudentSurname.Text  = "";
                lblStudentColour.Text   = "";

                MessageBox.Show("No data for this student");
            }

            connection.Close();
        }

       private void BtnTableCreate_Click(object sender, EventArgs e)
       {
            //name the DataTable
            dt = StudentTable();

            //Create table dynamically
            DataTable dt = new DataTable();             // TABLE IS NOT CREATED

            //Add column name
            dt.Columns.Add("AppleID",typeof(int));      // this should be the Colour value + Id
            dt.Columns.Add("AppleName",typeof(string)); // this should be the Colour value + Name

            MessageBox.Show("Table created");
        }



        private void Button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>