数据提取中的错误。记录未显示。 ThanX提前 [英] error in data fetching. record is not displaying. ThanX in advance

查看:90
本文介绍了数据提取中的错误。记录未显示。 ThanX提前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string query = "Select * from ACCOUNTS where Account_number =''" + comboBox1.SelectedIndex + "''";
           da = new SqlDataAdapter(query, cn);
           DataTable dt1 = new DataTable();
           da.Fill(dt1);


               txtcustomername.Text = dt1.Rows[0]["Name"].ToString();
               txtoccupation.Text = dt1.Rows[0]["Occupation"].ToString();
               txtfathername.Text = dt1.Rows[0]["Father_name"].ToString();
               txthusbandname.Text = dt1.Rows[0]["Husband_name"].ToString();
               txtcustomercnic.Text = dt1.Rows[0]["Customer_CNIC"].ToString();
               txtinitialdeposit.Text = dt1.Rows[0]["Initial_deposit"].ToString();
               dateTimePicker1.Value = Convert.ToDateTime(dt1.Rows[0]["Date"]);
               txtpostaladdress.Text = dt1.Rows[0]["Postal_address"].ToString();
               txtemail.Text = dt1.Rows[0]["Mail"].ToString();
               txtmobilenumber.Text = dt1.Rows[0]["Mobile"].ToString();


           }

推荐答案

为了帮助您解决问题应该发布你得到的错误。



但同时也很少。



永远不会将值直接连接到您的Sql语句。使用 SqlParameter [ ^ ]。



另外,您确定组合框的 SelectedIndex 实际上是数据库中的Account_number。这看起来很奇怪...你应该使用所选的值吗?
In order to help you with your question you should post the error you get.

But in the mean time, few things.

Never ever concatenate values directly to your Sql statement. Use SqlParameter[^].

Another thing, are you sure that the SelectedIndex of the combo box is really the Account_number in your database. This looks really odd... Should you use the selected value instead?


Hi Zeshan,



更新你的代码,如下所示。



Hi Zeshan,

Update your code as below.

string query = "Select * from ACCOUNTS where Account_number ='" + comboBox1.SelectedIndex + "'";
SqlCommand cmd = new SqlCommand(query,cn);
da = new SqlDataAdapter(cmd);
DataSet ds= new DataSet();
da.Fill(ds);
if(ds.Table[0].Rows.Count>0)
{         
    txtcustomername.Text = ds.Table[0].Rows[0]["Name"].ToString();
    txtoccupation.Text = ds.Table[0].Rows[0]["Occupation"].ToString();
    txtfathername.Text = ds.Table[0].Rows[0]["Father_name"].ToString();
    txthusbandname.Text = ds.Table[0].Rows[0]["Husband_name"].ToString();
    txtcustomercnic.Text = ds.Table[0].Rows[0]["Customer_CNIC"].ToString();
    txtinitialdeposit.Text = ds.Table[0].Rows[0]["Initial_deposit"].ToString();
    dateTimePicker1.Value = Convert.ToDateTime(ds.Table[0].Rows[0]["Date"]);
    txtpostaladdress.Text = ds.Table[0].Rows[0]["Postal_address"].ToString();
    txtemail.Text = ds.Table[0].Rows[0]["Mail"].ToString();
    txtmobilenumber.Text = ds.Rows[0]["Mobile"].ToString();
}


看一下你的代码,我注意到你正在使用你的组合框的selectedIndex属性。这可能是因为 SelectedIndex Property [ ^ ]返回所选Item的索引。我认为您需要使用 SelectedItem.Text [ ^ ]属性,如果要获取组合框中所选项目的文本。如果您要检索所选项的值,则需要使用 SelectedItem.value [ ^ ]属性。



如果这样可以解决您的问题,请告诉我。
Looking at your code one thing i noticed is you are using selectedIndex property of your combobox. This might the reason you are not getting any result because SelectedIndex Property[^] returns the index of the selected Item. I think you need to use the SelectedItem.Text[^] property if you want to get the text of the selected item in your combobox. If you are looking to retrieve the value of the selected Item then you need to use SelectedItem.value[^] property.

Let me know if this fixes your issue.


这篇关于数据提取中的错误。记录未显示。 ThanX提前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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