从数据库中显示文本框中的数据 [英] show data in textbox from database

查看:168
本文介绍了从数据库中显示文本框中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从不同的表中检索数据,我想在C#中的文本框中显示第一个文本框是从数据库填充但第二个1没有显示我的代码是

I am retrieving data from differetnt tables and I want to show in textboxes in C# first textbox is filling from database but 2nd 1 is not showing my code is


string str = "Select Address from AddressInfo where Contactid=(Select Contactid from ContactInfo where Fid=(Select Fid from Farmers where Name=''" + txtUpName.Text + "'')); Select PhoneNo from PhoneInfo where Contactid=(Select Contactid from ContactInfo where Fid=(Select Fid from Farmers where Name=''" + txtUpName.Text + "'')); ";
                con.Open();
                SqlCommand command = new SqlCommand(str, con);
                SqlDataReader dr = command.ExecuteReader();
                dr.Read()
                
                    if (dr.HasRows)
                    {
                        txtUpAddress.Text = dr[0].ToString();
                        txtUpPhoneNo.Text = dr[1].ToString();
                        con.Close();
                    }

                    else
                    {
                        MessageBox.Show("No data recieved ");
                    }

推荐答案

由于您只从数据库表中选择一个列(地址),因此您的SqlDataReader将只有一列,所以访问dr [1]总会给你一个例外。



尝试在SELECT查询中添加电话号码!
Since you are selecting only a single column (Address) from your database table, your SqlDataReader will only have one column, so accessing dr[1] will always give you an exception.

Try adding the phone number to your SELECT query!


您的查询应该是这样的:



Your query should be something like :

string str = " Select Address,PhoneNo from AddressInfo A,PhoneInfo P where A.Contactid = P.Contactid and A.Contactid=(Select Contactid from ContactInfo where Fid=(Select Fid from Farmers where Name='" + txtUpName.Text + "'));"; 


可能的原因可能是:



1. dr [1] .ToString()是空值。

OR

2. txtUpPhoneNo.text值在某处无效。



如果有任何疑问,请告诉我......
Possible causes could be:

1. dr[1].ToString() is null.
(OR)
2. txtUpPhoneNo.text value is getting nullified somewhere.

If any queries do let me know...


这篇关于从数据库中显示文本框中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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