Click here to Skip to main content
15,886,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am unable to display datagridview record in textbox M'I doing mistake or i am approaching in a wrong way

What I have tried:

C#
using System.Data;
using System.Data.SqlClient;

namespace DOTNET
{
    public partial class FrmCategory : Form
    {
        public FrmCategory()
        {
            InitializeComponent();
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            SqlConnection Con = new SqlConnection("Server=.;database=ProductInfo; integrated security=true;");
            var query = $"Insert into Category values('{TxtCategoryName.Text}')";
            var Command = new SqlCommand(query, Con);
            Con.Open();
            int result = Command.ExecuteNonQuery();
            Con.Close();
            if (result > 0)
                MessageBox.Show(result + "Record is Successfully added");
            GetCategory();
        }
        private void GetCategory()
        {
            SqlConnection Con = new SqlConnection("Server=.;database=ProductInfo; integrated security=true;");
            var query = $"Select * from Category";
            var Command = new SqlCommand(query, Con);
            SqlDataAdapter SDA = new SqlDataAdapter(Command);
            DataTable TBA = new DataTable();
            SDA.Fill(TBA);
            DgvCategory.DataSource = TBA;
            


        }
        private void FrmCategory_Load(object sender, EventArgs e)
        {
            GetCategory();
        }


        private void FrmCategory_Load_1(object sender, EventArgs e)
        {
            GetCategory();
        }

        //private void DgvCategory_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        //{

        //    TxtCategoryName.Text = DgvCategory.Rows[e.RowIndex].Cells[1].ToString();
        //}

        private void DgvCategory_CellEnter(object sender, DataGridViewCellEventArgs e)
        {

            TxtCategoryName.Text = DgvCategory.Rows[e.RowIndex].Cells[1].ToString();
        }
Posted
Updated 26-Mar-22 23:10pm
v2

Without the actual error message, some idea where in that code it occurs, and info about when it occurs we cannot help you with the problem you have spotted.

But ... two others we can.

1) Never hardcode connection strings: that means that you need to change it in multiple places in your code when when you release it and that is prone to error. Store it in a config file so that it can be changed without needing recomp0iliation!

2) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Quote:
TxtCategoryName.Text = DgvCategory.Rows[e.RowIndex].Cells[1].ToString();


When this shows a Type such as DataGridViewTextBoxCell rather than the contents you hope to get, it means you did something wrong. As it is a TextBoxCell, you probably want its text value, so try this:

TxtCategoryName.Text = DgvCategory.Rows[e.RowIndex].Cells[1].Value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900