2015-12-16 70 views
0

enter image description here我有,我想在TextBox的数据是StudentFirstName,并与他们一起SchoolID数据并显示获取一个表,我需要两个空TextBoxes旁边我不知道如何去做。取在文本框从数据库和数据显示

我的数据库表

StudentFirstName SchoolID  StudCourse 
abc     sc123   Bcom 
cef     sc155   Bcom 
gij     sc133   Bcom 
abc     sc122   BCA 
cef     sc156   BCA 
gij     sc144   BCA 

C#

using (MySqlConnection myConnection = new MySqlConnection(constr)) 
{ 
    string oString = "Select * from euser_student WHERE [email protected] order by StudentFirstName ASC"; 
    MySqlCommand oCmd = new MySqlCommand(oString, myConnection); 

    oCmd.Parameters.AddWithValue("@StudCourse", StudCourse); 
    myConnection.Open(); 
    using (MySqlDataReader oReader = oCmd.ExecuteReader()) 
    { 
      if (oReader == null || !oReader.HasRows) 
      { 
       ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('No Student Found')", true); 
      } 
      else 
      { 
       while (oReader.Read()) 
       { 

       } 
      } 
      myConnection.Close(); 
    }    
} 

回答

2

您可以使用SqlDataReader.GetString Method获取指定列的值作为一个字符串像这样:

while (oReader.Read()) 
{ 
    TextBox1.Text = oReader.GetString(1); // 1 is the Parameter that is The zero-based column ordinal you can change it to what you want 
    TextBox2.Text = oReader.GetString(2); 
} 

检查这了解更多:Retrieving Data Using a DataReader

但是,如果您想要显示多行数据,如果使用GridView控件显示数据会更好。首先添加一个GridViewaspx这样的:

<asp:GridView ID="GridView1" runat="server"></asp:GridView> 

然后:

GridView1.DataSource = oReader; 
GridView1.DataBind(); 
+0

那是工作的罚款我对不起这是我的错笏我想是我说,如果我不反对'StudCourse 3个用户''Bcom'我需要在文本框中显示所有三个名字,如果有5个名字,我想根据我的知识显示5个名字,我想要一个动态创建的文本框 – Shaik

+0

@Shaik ...那么为什么你不使用'GridView'来达到这个目的呢? –

+0

我不知道如何使用它,我有一个快速的问题是有可能在每个抓取旁边添加空文本框 – Shaik

相关问题