开发者

how to populate textboxes with values of datareader

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜