简体   繁体   中英

Display data in radio button from SQL Server in ASP.NET

I had a textbox to enter the barcode and a search button. The button will display a list of radio button that consist of 3 chunks of data related to the barcode from the server.

sheetDetail.aspx

<div>
    <span>Enter Barcode: </span>
    <asp:TextBox runat="server" ID="txtSearch" />
    <asp:Button runat="server" ID="btnSearchBar" Style="background-color: #c9302c;" CssClass="btn btn-danger" Text="Search" OnClick="btnSearchBar_Click" />
    <asp:RadioButtonList  Font-Size="X-Small" RepeatLayout="Table" RepeatColumns = "2"  ID="rediobtn" runat="server">
    </asp:RadioButtonList>

</div>

sheetDetail.aspx.cs

protected void btnSearchBar_Click(object sender, EventArgs e)
{
    eExistingSheetQuery existingSheetQuery = new eExistingSheetQuery();

    string value = txtSearch.Text.Trim();
    // Your Ado.Net code to get data from DB

    rediobtn.Items.Clear();

    DataTable dt = existingSheetQuery.GetSheetItem(value);

    rediobtn.DataSource = existingSheetQuery.GetSheetItem(value);


    rediobtn.DataBind();
}

eExistingSheetQuery.cs

public DataTable GetSheetItem(string barcode)
{
    try
    {
        conn = new SqlConnection(estocktake);
        conn.Open();

        DataTable dtd = new DataTable();
        GridView gvd = new GridView();

        cmds = new SqlCommand(@"SELECT  [invtid],[transtatuscode] ,[descr] FROM [Products] where ib_itemcode1='" + barcode + "' ;", conn);
        adpt = new SqlDataAdapter();

        adpt.SelectCommand = cmds;

        cmds.ExecuteNonQuery();
        adpt.Fill(dtd);
        conn.Close();
        conn.Dispose();


        return dtd;
    }
    catch (Exception)
    {
        conn.Close();
        conn.Dispose();
        return null;
    }
}

The button will trigger the onclick and the function will start to run, get the value from the textbox.

getsheetitem --> run the query and get the data

bind it to the gridview...

All it display was System.Data.DataRowView. All the radio was that..

I wanted each radiobutton to display.

[radiobutton] invtid - descr - transtatuscode

Any help will be really awesome.

Try this for sheetDetail.aspx.cs

protected void btnSearchBar_Click(object sender, EventArgs e)
{
    eExistingSheetQuery existingSheetQuery = new eExistingSheetQuery();

    string value = txtSearch.Text.Trim();
    // Your Ado.Net code to get data from DB

    DataTable dt = existingSheetQuery.GetSheetItem(value);  

    rediobtn.Items.Clear();
    rediobtn.Items.Add(Rows[0]["invtid"]);
    rediobtn.Items.Add(Rows[0]["transtatuscode"]);
    rediobtn.Items.Add(Rows[0]["descr"]);
}

I believe you are getting the System.Data.DataRowView because you have not defined the DataTextField ie

rediobtn.DataTextField = "transtatuscode";
rediobtn.DataValueField = "invtid";
rediobtn.DataBind();

If you want to combine all of those fields together you can change your query to concatenate those three fields together.

Thanks guys for the help, the solution that i figure out is to do all the changing on the query like this.

            cmds = new SqlCommand(@"SELECT  [invtid] +' '+ [descr] +' '+ [transtatuscode] as ProductDesc FROM [Products] p where ib_itemcode1='" + barcode + "' ;", conn);

This way the data will be already combine before binding onto the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM