Anjello Joshua Anjello Joshua - 12 days ago 6
C# Question

Retrieve Rows Value to Textboxes C# & SQL Server

Do you have any ideas on how I can put this to rows:

enter image description here

in this texboxes?

enter image description here

or sooner 10 rows to 10 textboxes?

I only know is to retrieve one row:

if (Inventory.passQty == "1")
{
SqlConnection connection = new SqlConnection("Data Source = DESKTOP-ANJELLO\\SQLEXPRESS; Initial Catalog = db_ADAPurchase; Persist Security Info = True; User Id = sa; Password = mm4;");
connection.Open();

SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("SELECT * FROM tbl_PurchaseRequest WHERE request_id = {0}", Inventory.passID);

SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
label_RID.Text = dr.GetString(0);
label_Item1.Text = dr.GetString(1);
txb_Title.Text = dr.GetString(2);
cbx_vendor.Text = dr.GetString(3);
txb_address.Text = dr.GetString(4);
label_date.Text = dr.GetString(5);
cbx_terms.Text = dr.GetString(6);
txb_ITD1.Text = dr.GetString(7);
txb_Qty1.Text = dr.GetSqlInt32(8).ToString();
label_unit1.Text = dr.GetString(9);
txb_UntP1.Text = dr.GetSqlInt32(10).ToString();
txb_TotP1.Text = dr.GetSqlInt32(11).ToString();
label_total.Text = dr.GetSqlInt32(12).ToString();
txb_reqBy.Text = dr.GetString(13);
}

connection.Close();
}


How do I make this to retrieve multiple rows in different textboxes?

Thanks for helping me!

PS: Sir @mohit-shrivastava can you please help me again?

Answer

You can do something like this but GridView is instead a very good idea to implement this kind of situation.

if (Inventory.passQty == "1")
{
    SqlConnection connection = new SqlConnection("Data Source = DESKTOP-ANJELLO\\SQLEXPRESS; Initial Catalog = db_ADAPurchase; Persist Security Info = True; User Id = sa; Password = mm4;");
    connection.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = connection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = String.Format("SELECT * FROM tbl_PurchaseRequest WHERE request_id = {0}", Inventory.passID);

    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.Read())
    {
        label_RID.Text = dr.GetString(0);
        label_Item1.Text = dr.GetString(1);
        txb_Title.Text = dr.GetString(2);
        cbx_vendor.Text = dr.GetString(3);
        txb_address.Text = dr.GetString(4);
        label_date.Text = dr.GetString(5);
        cbx_terms.Text = dr.GetString(6);
        label_total.Text = dr.GetSqlInt32(12).ToString();
        txb_reqBy.Text = dr.GetString(13);
        int number = 1;
        do
        {
            TextBox tb = FindControl("txb_ITD" + number.ToString()) as TextBox;
            tb.Text = dr.GetString(7);
            TextBox tb1 = FindControl("txb_Qty" + number.ToString()) as TextBox;
            tb1.Text = dr.GetSqlInt32(8).ToString();
            TextBox tb2 = FindControl("label_unit" + number.ToString()) as TextBox;
            tb2.Text = dr.GetString(9);
            TextBox tb3 = FindControl("txb_UntP" + number.ToString()) as TextBox;
            tb3.Text = dr.GetSqlInt32(10).ToString();
            TextBox tb4 = FindControl("txb_TotP" + number.ToString()) as TextBox;
            tb4.Text = dr.GetSqlInt32(11).ToString();
            number++;
            //Since Query can pull more records than 10
            if(number>=10)
            {
                break;
            }
        }
        while(dr.Read())
    }
    connection.Close();
}

Note: The Code has not been tested. but it is to give you the glimpse of what has to be done to get the output as expected.