Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I bindind a Combo Box useing this code:

void InformatiiDespreClient()
        {
            txtNumeClient.Items.Clear();
            Program.Connection.CommandText = "select * from DateClientiAmanet";
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            txtNumeClient.DataSource = Table;
            txtNumeClient.DisplayMember = "Nume";
            txtNumeClient.ValueMember = "ClientId";
        }

I set the value of a textBox like this:
this.lblClientID.Text = txtNumeClient.SelectedValue.ToString();


All good.

I want, when the textbox, or the value change to fill some TextBoxes

I write this code, but not work
C#
void InformatiiDespreClientDetaliate()
       {
           Program.Connection.CommandText = "select * from DateClientiAmanet where ClientId=" + txtNumeClient.SelectedValue.ToString();
           DataTable Table = new DataTable();
           Program.Connection.FillDataTable(Table, true);
           foreach (DataRow Row in Table.Rows)
           {
               txtNume.Text = Table.Rows[0]["Nume"].ToString() + ", " + Table.Rows[0]["Prenume"].ToString();
               txtAdresa.Text = "Strada: " + Table.Rows[0]["Strada"].ToString() + ", nr.: " + Table.Rows[0]["Numarul"].ToString();
               txtCNP.Text = Table.Rows[0]["CNP"].ToString();
           }
       }



Some sugestions?
Posted
Updated 13-Mar-11 4:16am
v2
Comments
Sandeep Mewara 13-Mar-11 5:10am    
Can you elaborate on "I write this code, but not work"... whats the error? Issue? Whats not happening/working?

Firstly, can I suggest that you amend your naming convention for your controls.

You have a ComboBox named txtNumeClient and a TextBox named txtNume. This makes it very difficult for others to quickly understand your code and, believe me, in 6 months time it will make it problematic for you too.

I use cboxNumeClient for ComboBoxes lboxNumeClient for ListBoxes and, as you do txtNume for TextBoxes. Obviously use something that makes sense for you but you should change it anyway.

Secondly you should learn how to use Parameterized Queries (Google for that for details), rather than constructing them by concatenation, as you currently do ("select * from DateClientiAmanet where ClientId=" + txtNumeClient.SelectedValue.ToString()). This is dangerous and leaves your code open to SQL Injection attacks.

Lastly, you call FillDataTable(). This is obviously a method of your own, since there is no method by that name in .Net. So without more information about what 'but not work' actually means and, possibly, seeing FillDataTable() it will be very difficult for anyone to help you.
 
Share this answer
 
Comments
Espen Harlinn 13-Mar-11 10:39am    
Seems we are somewhat heading in the same direction, my 5
I guess txtNumeClient.SelectedValue is your culprit - try debugging and see if it's null
You can use something like this:
Wrap Those Session Variables![^] - it's in vb.net, but you get the gist - to keep track of the selected value.

I'd also use SqlParameter like Using SQLParameters with VB.NET/C#[^], and not string concatenation as it may be used to implement a SQL injection attack if you are not careful - see SQL Injection Attacks and Some Tips on How to Prevent Them[^] for more information.

Regards
Espen Harlinn
 
Share this answer
 
Resolved

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestiuneProduse;

namespace FereastraPrincipala.Contracte
{
    public partial class InformatiiContracte : UserControl
    {

        private bool ClientiAdaugati = false;

        public InformatiiContracte()
        {
            InitializeComponent();
            
        }

       

        private void InformatiiContracte_Load(object sender, EventArgs e)
        {
            txtNumeClient.Focus();
            InformatiiDespreClient();
        }

        void InformatiiDespreClient()
        {

            txtNumeClient.Items.Clear();
            Program.Connection.CommandText = "select RTRIM(Nume) + ', '+ RTRIM(Prenume) AS NumeComplet, ClientId from DateClientiAmanet ORDER BY Nume";
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);

            txtNumeClient.DataSource = Table;
            txtNumeClient.DisplayMember = "NumeComplet";
            txtNumeClient.ValueMember = "ClientId";
            ClientiAdaugati = true;

        }


        void InformatiiDespreClientDetaliate()
        {

            Program.Connection.CommandText = "select * from DateClientiAmanet where ClientId=" + txtNumeClient.SelectedValue.ToString();
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            foreach (DataRow Row in Table.Rows)
            {
                txtNume.Text = Table.Rows[0]["Nume"].ToString() + ", " + Table.Rows[0]["Prenume"].ToString();
                txtAdresa.Text = "Strada " + Table.Rows[0]["Strada"].ToString() + ", nr. " + Table.Rows[0]["Numarul"].ToString() + ", bl. " + Table.Rows[0]["Bloc"].ToString() + ", sc. " + Table.Rows[0]["Scara"].ToString() + ", et. " + Table.Rows[0]["Etajul"].ToString() + ", ap. " + Table.Rows[0]["Apartament"].ToString();
                txtCNP.Text = Table.Rows[0]["CNP"].ToString();

            }

            this.txtBoxDetaliiClient.Text = "Client: " + txtNumeClient.SelectedValue.ToString();
        }

        private void txtNumeClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ClientiAdaugati)
                InformatiiDespreClientDetaliate();

        }

        
    }
}
 
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