Gå til innhold
🎄🎅❄️God Jul og Godt Nyttår fra alle oss i Diskusjon.no ×

[Løst] SQL lokal DB. DataGridView viser mer tekst enn jeg skrev inn.


Anbefalte innlegg

Når jeg skriver inn tekst i txtNavn og sender den til datagridview, står det "System.Windows.Forms. TexBoks, Text og det jeg skrev. Se Bilde.

 

Her er hele sql koden.

  

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.Data.SqlClient;
 
 
namespace DB_System
{
    public partial class Form1 : Form
    {
        SqlConnection connection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\Kjetil\Documents\Visual Studio 2017\Projects\DB_System\DB_System\DB_Server1.mdf;Integrated Security = True; Connect Timeout = 30");
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void btnInsert_Click(object sender, EventArgs e)
        {
            connection.Open();
            SqlCommand cmd = connection.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "insert into[MyTable] (Name,Surname,Address) values ('" + txtName + "','" + txtSurname + "','" + txtAddress + "')";
            cmd.ExecuteNonQuery();
            connection.Close();
            txtName.Text = "";
            txtSurname.Text = "";
            txtAddress.Text = "";
            txtSearch.Text = "";
            display_data();
            MessageBox.Show("Data inserted Successfully");
        }
        //To display data
        public void display_data()
        {
            connection.Open();
            SqlCommand cmd = connection.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from [MyTable]";
            cmd.ExecuteNonQuery();
            DataTable dta = new DataTable();
            SqlDataAdapter dataadp = new SqlDataAdapter(cmd);
            dataadp.Fill(dta);
            dataGridView1.DataSource = dta;
            connection.Close();
        }
 
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            display_data();
        }
 
        private void btnDelete_Click(object sender, EventArgs e)
        {
            connection.Open();
            SqlCommand cmd = connection.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "delete from [MyTable] where name = '" + txtName.Text + "'";
            cmd.ExecuteNonQuery();
            connection.Close();
            txtName.Text = "";
            txtSurname.Text = "";
            txtAddress.Text = "";
            txtSearch.Text = "";
            display_data();
            MessageBox.Show("Data deleated Successfully");
        }
 
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            connection.Open();
            SqlCommand cmd = connection.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update [MyTable] set name = '" + txtName.Text + "'where name ='" + txtSurname.Text + "'"; ;
            cmd.ExecuteNonQuery();
            connection.Close();
            txtName.Text = "";
            txtSurname.Text = "";
            txtAddress.Text = "";
            txtSearch.Text = "";
            display_data();
            MessageBox.Show("Data updatet Successfully");
        }
 
        private void btnSearch_Click(object sender, EventArgs e)
        {
            connection.Open();
            SqlCommand cmd = connection.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from [MyTable] where name = '" + txtSearch.Text + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            connection.Close();
            txtName.Text = "";
            txtSurname.Text = "";
            txtAddress.Text = "";
            txtSearch.Text = "";
            
        }
    }
}
 

 

Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...