C#-Sql How do I connect DataGridView-label?

Oğulcan Akca 196 Reputation points
2021-01-21T11:17:09.897+00:00

I saved the text "** EXAMPLE **" in the first picture to the "Kurallar" column in the database and as 123 to the "Id" column.
In the second picture, when I type 123 into the TextBox under the DataGridView and click the "Göster" button, I want to show the text in the "Kurallar" column of the data with Id = 123 on label22.Text in the third picture.
I hope I could explain.

59139-ekran-goruntusu-2021-01-21-135947.png

59098-ekran-goruntusu-2021-01-21-140108.png

59099-ekran-goruntusu-2021-01-21-140139.png

 Kurallar kurallar = new Kurallar();  
            SqlConnection baglanti5 = new SqlConnection(@"Data Source=(localdb)\mssqllocaldb;initial catalog = ELibraryInformation;integrated security = true;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");  
            baglanti5.Open();  
            SqlCommand komut5 = new SqlCommand("SELECT (Id) FROM [Table]", baglanti5);  

Please continue with the above code

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,708 questions
SQL Server Migration Assistant
SQL Server Migration Assistant
A Microsoft tool designed to automate database migration to SQL Server from Access, DB2, MySQL, Oracle, and SAP ASE.
494 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,239 questions
{count} votes

Accepted answer
  1. Oğulcan Akca 196 Reputation points
    2021-01-25T12:14:18.247+00:00

    FİNİSHED

    I solved my problem by creating a new label. I also created a method to get better lines of code. all codes below ...

     public void kuralGöster()
            {
                SqlConnection baglanti5 = new SqlConnection(@"Data Source=(localdb)\mssqllocaldb;initial catalog = ELibraryInformation;integrated security = true;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
                baglanti5.Open();
                SqlCommand komut5 = new SqlCommand("SELECT (Kurallar) FROM [Table] WHERE [Id] LIKE '%" + tbxAyarlarSil.Text + "%'", baglanti5);
                label1.Text = komut5.ExecuteScalar().ToString();
                baglanti5.Close();
            }
    

    kuralGöster(); ==> button2_Click and other form(Kurallar)'s Load part

    private void Kurallar_Load(object sender, EventArgs e)
            {
    
                this.FormBorderStyle = FormBorderStyle.FixedSingle;
                Ayarlar ayarlar = new Ayarlar();
    
                ayarlar.kuralGöster();
                label22.Text = ayarlar.label1.Text;
            }
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Abdulhakim M. Elrhumi 351 Reputation points
    2021-01-22T20:13:59.023+00:00

    Hi

    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Text;  
    using System.Linq;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
    using System.Data.SqlClient;  
    using Dapper;  
    using DevExpress.XtraEditors;  
    using System.IO;  
    using EmployeePro.Model;  
      
    namespace EmployeePro.View  
    {  
      public partial class frmViewEmployee : DevExpress.XtraEditors.XtraForm  
      {  
        public frmViewEmployee()  
        {  
          InitializeComponent();  
        }  
        SqlConnection con=new SqlConnection("Data Source=.;Initial Catalog=TestDB;Integrated Security=True");  
          
      
        BindingSource bsEmployee=new BindingSource();  
        private void frmViewEmployee_Load(object sender, EventArgs e)  
        {  
          con.Open();  
          List<CLS_Employee> emp = con.Query<CLS_Employee>("SELECT Id,Fname,Lname,Adrs,Image_Employee,FinalTotal  FROM [dbo].[Employee]").ToList();  
          bsEmployee.DataSource=emp;  
          dgvEmployee.DataSource=bsEmployee;  
            
          con.Close();  
        }  
      
         
        private void gcEmployeeView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)  
        {  
           
          label1.Text = gcEmployeeView.GetFocusedRowCellValue("Id").ToString();  
               Byte[] image = (byte[])(gcEmployeeView.GetFocusedRowCellValue("Image_Employee"));  
      
                MemoryStream ms = new MemoryStream(image);  
                piBox.Image = Image.FromStream(ms);  
        }  
      
         
      }  
    }  
    

    59667-picpro.png

    Best Regards.
    Please remember to mark the replies as answers if they help.


  2. Bonnie DeWitt 811 Reputation points
    2021-01-24T19:17:43.14+00:00

    @Abdulhakim M. Elrhumi ,
    As far as I know, there is no .Query() method for the SqlConnection class. Where did yours come from? I bet that it is an Extension method? Probably has something to do with that DevExpress stuff you've got there.

    @Oğulcan Akca ,

    Are you "persisting" the data that you retrieve from the database to a List or a DataSet/DataTable? Or are you reading the data from the database and entering it directly into the DataGridView's rows/column? (you shouldn't be doing this if you are). You're going to want to do some databinding, using a BindingSource, if you're not already.

    You're also going to need to be able to pass the data to your Settings Form. I have a blog post that describes several methodologies to use to do that.

    See these links to my blog posts:

    http://geek-goddess-bonnie.blogspot.com/2011/01/passing-data-between-forms.html
    http://geek-goddess-bonnie.blogspot.com/2012/12/passing-data-between-forms-redux_31.html

    And this one is often relevant too:

    https://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html

    Hope that helps!
    ~~Bonnie


  3. Oğulcan Akca 196 Reputation points
    2021-01-25T10:28:15.05+00:00

    I searched for Id with tbxSettingsSil.Text and wrote the following to show the "Kurallar" column this Id on label22.Text, but it still didn't. What's my mistake ?

      SqlConnection baglanti5 = new SqlConnection(@"Data Source=(localdb)\mssqllocaldb;initial catalog = ELibraryInformation;integrated security = true;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
                        Kurallar kurallar = new Kurallar();
                        baglanti5.Open();
                        SqlCommand komut5 = new SqlCommand("SELECT (Kurallar) FROM [Table] WHERE [Id] LIKE '%" + tbxAyarlarSil.Text + "%'" , baglanti5);
                        kurallar.label22.Text = komut5.ExecuteScalar().ToString();
                        baglanti5.Close();