Sunday, December 8, 2024

How to find values from SQLdatasource dataview - Datatable to list

 DataView view = (DataView)dataSource.Select(new DataSourceSelectArguments());

DataTable groupsTable = view.ToTable();
String value;

foreach (DataRow dr in dt.Rows)
{
    // Do something here IE grab the value of the first column
    value = dr[0];
}


or


 DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    int reorderedProducts = (int)dv.Table.Rows[0][0];
    if (reorderedProducts > 0)
    {
        Label1.Text = "Number of products on reorder: " + reorderedProducts;
    }
    else
    {
        Label1.Text = "No products on reorder.";
    }

or


var view = (DataView)bindingSource1.DataSource;
DataTable productsTable = view.Table;

// Set RowStateFilter to display the current rows.
view.RowStateFilter = DataViewRowState.CurrentRows;

// Query the DataView for red colored products ordered by list price.
var productQuery = from DataRowView rowView in view
                   where rowView.Row.Field<string>("Color") == "Red"
                   orderby rowView.Row.Field<decimal>("ListPrice")
                   select new
                   {
                       Name = rowView.Row.Field<string>("Name"),
                       Color = rowView.Row.Field<string>("Color"),
                       Price = rowView.Row.Field<decimal>("ListPrice")
                   };

// Bind the query results to another DataGridView.
dataGridView2.DataSource = productQuery.ToList();

or

DataView custView = new DataView(custDS.Tables["Customers"], "",  
  "CompanyName, ContactName",  
  DataViewRowState.CurrentRows);  
  
DataRowView[] foundRows =
  custView.FindRows(new object[] {"The Cracker Box", "Liu Wong"});  
  
if (foundRows.Length == 0)  
  Console.WriteLine("No match found.");  
else  
  foreach (DataRowView myDRV in foundRows)  
    Console.WriteLine("{0}, {1}", myDRV["CompanyName"].ToString(),
      myDRV["ContactName"].ToString());  
ref :-  https://dev.to/bytehide/datatable-in-c-usage-and-examples-40f2
ref:- https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/querying-the-datarowview-collection-in-a-dataview

https://stackoverflow.com/questions/5516464/sqldatasource-select-how-do-i-use-this-asp-net

https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.sqldatasource.select?view=netframework-4.8.1

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/finding-rows

Grid view or Dropdown with SQL datasource

 <asp:GridView runat="server" ID="grdSalesReportCustomer" AutoGenerateColumns="False" DataSourceID="SalesReportCustomer">

                        <Columns>

                            <asp:BoundField DataField="Name" HeaderText=" Customer Name" SortExpression="Name"></asp:BoundField>

                            <asp:BoundField DataField="TotalAmountSpent" HeaderText="Total Amount Spent" ReadOnly="True" SortExpression="TotalAmountSpent"></asp:BoundField>

                        </Columns>

                    </asp:GridView>




                    <asp:SqlDataSource runat="server" ID="SalesReportCustomer" ConnectionString='<%$ ConnectionStrings:ConStringTrainee %>' SelectCommand="select  cm.Name, sum(pd.Total_Price) as TotalAmountSpent from Purchase_Details pd inner join Customer_Master cm on cm.Customer_ID= pd.Customer_ID group by pd.Customer_ID, cm.Name"></asp:SqlDataSource>


in c# code

on save click

 grdSalesReport.DataSourceID = "BookSalesReport";

            grdSalesReport.DataBind();

Thursday, December 5, 2024

Convert boolean to Yes or no in Grid view in asp.net

 <asp:TemplateField HeaderText="Active" SortExpression="Active">

    <ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateField>

Saturday, November 30, 2024

How to add two select query in single line in ASP.NET - Dataset of two tables

  string conString = ConfigurationManager.ConnectionStrings["tempdbConnectionString"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                using (SqlConnection con = new SqlConnection(conString))

                {

                    con.Open();

                    using (SqlCommand cmd = new SqlCommand("SELECT [Teacher_ID], [Teacher_Name] FROM [Student_Master]; SELECT [Student_ID] ,[Teacher_ID],[Student_Name],[Passed] FROM [dbo].[Student_Details]", con))

                    {

                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))

                        {

                            DataSet ds = new DataSet();

                            da.Fill(ds);

                            ddlTeacher.DataSource = ds.Tables[0];

                            ddlTeacher.DataBind();

                            ddlTeacher.Items.Insert(0, new ListItem("-Select-", "0"));


                            grdStudentDetails.DataSource =  ds.Tables[1];

                            grdStudentDetails.DataBind();

                        }

                    }

                    con.Close();

                }



              


            }


        }