开发者

how to populate textboxes with values of datareader

开发者 https://www.devze.com 2022-12-28 08:42 出处:网络
i have some textboxes. in first textbox i entered a vaue like empid. after开发者_StackOverflow中文版 the clicking a button which goes to databaseand checks for the columns specified by me.

i have some textboxes. in first textbox i entered a vaue like empid. after开发者_StackOverflow中文版 the clicking a button which goes to database and checks for the columns specified by me. i get that data in datareader.

from datareader i need to display the particular employ information in the remaining textboxes.

how can i achieve this.


Assuming your datareader is called rdr, something like this would work:

while(rdr.Read())
{
   txtBox1.Text = rdr.Item["DBFieldName1"].ToString();
   txtBox2.Text = rdr.Item["DBFieldName2"].ToString();
}


while (dr.Read())
{                    
    string checkValue = dr.GetValue(0).ToString();
    if (checkValue == myEmpIdTextbox.Text)
    {
        Texbox2.Text = dr.GetValue(1).ToString();
        Texbox3.Text = dr.GetValue(2).ToString();
    }
}

Works in C# - Visual Studio 2015.

The values in the brackets () next to GetValue indicate the column number, relative to your SQL Select query.

EXAMPLE:

SELECT coloumn1, coloumn2, coloumn3 FROM table

Then, in this case, Textbox3.Text will be made equal to the data in coloumn3, and Textbox2. Text to coloumn2, for that row where coloumn1 is equal to your value in your Empid textbox.

0

精彩评论

暂无评论...
验证码 换一张
取 消