如何从数据库表中获取值到文本框中

问题描述:

我正在使用以下代码将数据库表中的每个值都放入文本框中。如果单击编辑按钮,则文本框中显示的名称与项目的其余部分相同。我怎么能这样做



提前谢谢







I'm using the following code for getting each values from a database table into textbox. If click the button 'Edit' the the name displayed on the textbox and same as the rest of the item. How can I do this

Thanks in advance



private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure want to edit.?", "Warning", MessageBoxButton.YesNo,
              MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {

                DataRowView rowview = GridStockGroup.SelectedItem as DataRowView;
                
                if (rowview != null)
               // int id = 0;
                {
                    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TradingSoft;Integrated Security=True");
                    SqlCommand cmd = new SqlCommand("select * from stock_category", con);
                  //  id = Convert.ToInt32(rowview.Row["stockGroup_id"]);
                    txtName.Text = rowview.Row["name"].ToString();
                    txtDescription.Text = rowview.Row["description"].ToString();
                    txtLocation.Text = rowview.Row["location"].ToString();
                    DtpPostdate.Text = Convert.ToString(rowview.Row["postdate"]);
                    btnUpdateStock.Visibility = Visibility.Visible;
                    btnStockCat.Visibility = Visibility.Hidden;
                    
                    SqlParameter[] parameter = new SqlParameter[0];
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        adapter.Fill(dt);
                    }

                }

问题不明确。

你已经将数据从数据框链接到文本框了吗?

所以当你点击编辑按钮时,文本框中的数据就会出现在那里..





如果你正在谈论点击gridview / datagridview单元格然后它的不同情况。



请澄清
question is not clear.
you are already linking data from datatable to textbox right?
so when you click on the edit button data in the textbox will be there..


if you are talking about clicking on gridview /datagridview cell then its different case.

please clarify


如果我理解正确,你想从SQL表中填充TextBox。

你必须使用SqlDataReader来读取你的数据。

尝试一下像这样。



If I understand correctly, you want to populate TextBox from your SQL table.
You must to use a SqlDataReader to read your datas.
Try something like this.

DialogResult message = MessageBox.Show("Are you sure want to edit.?", "Warning", MessageBoxButton.YesNo,MessageBoxImage.Warning);

if (message == DialogResult.Yes)
            {
             string connString = "Data Source=.;Initial Catalog=TradingSoft;Integrated                    Security=True";

            string query = "select * from stock_category";

            SqlConnection con = new SqlConnection(connString);

            SqlCommand cmd = new SqlCommand(query, con);

            con.Open();

            SqlDataReader dr = cmd.ExecuteReader();

               if (dr.Read())
            {
                txtName.Text = dr.GetValue(0).ToString();
                txtDescription.Text = dr.GetValue(1).ToString();
                txtLocation.Text = dr.GetValue(2).ToString();
                
                //Go on with the other record
            }
                   
              con.Close();
 
                }