绑定一个ComboBox,并填充textBoxes [英] Binding a ComboBox, and fill textBoxes

查看:63
本文介绍了绑定一个ComboBox,并填充textBoxes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码绑定组合框:

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


我像这样设置textBox的值:


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

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?

推荐答案

首先,我可以建议您修改控件的命名约定.

您有一个名为 txt NumeClient的组合框和一个名为 txt Nume的文本框.这使其他人很难快速理解您的代码,并且相信我,在6个月的时间内,这也将给您带来麻烦.

我将 cbox NumeClient用于ComboBoxes,将 lbox NumeClient用于ListBoxes,并且将 txt Nume用于TextBoxes.显然,使用对您有意义的东西,但无论如何都应该对其进行更改.

其次,您应该学习如何使用参数化查询(有关详细信息,请参见Google),而不是像现在那样通过串联来构造它们(从DateClientiAmanet中选择*,其中ClientId =" + txtNumeClient.SelectedValue. ToString()).这很危险,并使您的代码容易受到SQL Injection攻击.

最后,您呼叫FillDataTable().显然,这是您自己的方法,因为.Net中没有该名称的方法.因此,如果没有更多关于但不起作用"实际上意味着什么的信息,并且可能看不到FillDataTable(),那么任何人都很难帮助您.
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.


我想txtNumeClient.SelectedValue是您的罪魁祸首-尝试调试并查看它是否为null
您可以使用以下内容:
包装这些会话变量! [在VB.NET/C#中使用SQLParameter [ SQL注入攻击以及有关如何防止它们的一些提示 [<有关更多信息,请参见href ="http://www.codeproject.com/KB/database/SqlInjectionAttacks.aspx" target ="_ blank" title ="New Window"> ^ ].

问候
Espen Harlinn
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


已解决

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();

        }

        
    }
}


这篇关于绑定一个ComboBox,并填充textBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆