none
from Textbox to a database? RRS feed

  • Question

  • I have managed to get data from *.mdb to a textbox, here is the code:

    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;

    // needed library
    using System.Data.OleDb;

    namespace projekti_v._0._0
    {
        public partial class Form1 : Form
        {
            // database object
            OleDbConnection ConnectMe;   


            public Form1()
            {
                InitializeComponent();
            }

            private void btn_yhdista_Click(object sender, EventArgs e)
            {
                // connection to (.mdb)
                ConnectMe = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Koulu\\Tietokanta1.mdb");
                // SQL
                string mySelectQuery = "SELECT Kysymys FROM Tietokanta";

                // try to contact
                try
                {               
                    ConnectMe.Open();
                }

                // if something is wrong
                catch (OleDbException)
                {
                    MessageBox.Show("Ei voi yhdistää.", "Virhe", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                // from database to textbox
                try
                {
                   
                    OleDbCommand cmd = new OleDbCommand(mySelectQuery, ConnectMe);
                    // DataReader object
                    OleDbDataReader Reader = cmd.ExecuteReader();
                   

                    //must have!!
                    if (Reader.Read())
                    {
                        // print into textbox
                        txt_kysymys.Text = Reader[0].ToString();
                    }               
                    Reader.Close();
                                  
                }

                // if something is wrong
                catch (OleDbException)
                {
                    MessageBox.Show("Virhe!", "Väärä SQL lause", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
               

            }
        }
    }

    If i made another textbox and a button (when i click the button, it sends the text to database), how can I send text from textbox to a database?



    Thursday, October 16, 2008 1:22 PM

Answers