简体   繁体   English

从ASP.NET中的SQL Server通过单选按钮显示数据

[英]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. 该按钮将显示一个单选按钮列表,该列表包含3个与来自服务器的条形码相关的数据块。

sheetDetail.aspx 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 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 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. 该按钮将触发onclick ,该函数将开始运行,并从文本框中获取值。

getsheetitem --> run the query and get the data getsheetitem >运行查询并获取数据

bind it to the gridview... 将其绑定到gridview ...

All it display was System.Data.DataRowView. 它显示的只是System.Data.DataRowView。 All the radio was that.. 所有的收音机就是那个。

I wanted each radiobutton to display. 我希望显示每个单选按钮。

[radiobutton] invtid - descr - transtatuscode [radiobutton] invtid-descr-transtatuscode

Any help will be really awesome. 任何帮助都将非常棒。

Try this for sheetDetail.aspx.cs 试试这个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 我相信您正在获取System.Data.DataRowView,因为您尚未定义DataTextField,即

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. 这样,在绑定到表之前,数据已经被合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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