I have a stored procedure in SQL that searches employee details. When it finds something,it returns and displays the data in a gridview. But how can I handle if it did not return anything? like when 'no record is found'?
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBReader"].ConnectionString))
{
using (SqlCommand com = new SqlCommand("[Reader].[usp_SearchUser]", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.Add("#empID", SqlDbType.Int).Value = this.EmpID;
con.Open();
int result = com.ExecuteNonQuery();
if (result == 0)
{
this.NoRecord = "No Record Found";
}
else
{
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
search.DataSource = ds;
search.DataBind();
}
}
}
Did not get what is your exact question? do you want the gridview property when there is no data then it will show as no records found i.e. EmptyDataText="No records Found"
e.g.
<asp:GridView ID="GridView1" runat="server" EmptyDataText="No records Found">
<Columns>
<asp:boundfield datafield="empID" headertext="Employee ID"/>
<asp:boundfield datafield="empName" headertext="Employee Name"/>
</Columns>
</asp:GridView>
I suppose you use a webcontrol GridView? So you might use the GridView.EmptyDataTemplate to have full control of what to render if no data has been bound.
<asp:gridview id="yourGridView" runat="server">
<emptydatatemplate>
No Data Found!
<img src="noData.jpg"/>
</emptydatatemplate>
</asp:gridview>
Or just use EmptyDataText Property if you just want to show a text Message
<asp:gridview id="yourGridView" emptydatatext="No Data Found" runat="server">
....
</asp:gridview>
Related
I am calling the server and returning a datatable of results but when I try to bind to the gridview, it is throwing an error about not being able to find a field or property on the data source. I'm newer to gridviews and .NET controls so any help would be appreciated.
The GridView
<asp:GridView ID="gv_Search" AutoGenerateColumns="false" runat="server" AllowSorting="true" OnSorting="gv_Search_Sorting">
<Columns>
<asp:HyperLinkField NavigateUrl="#" Text="View"/>
<asp:BoundField SortExpression="Hdefendant_name" HeaderText="Name" DataField="Hdefendant_name"/>
<asp:BoundField SortExpression="Hdefendant_location" HeaderText="Location" DataField="Hdefendant_location"/>
<asp:BoundField SortExpression="Hdate" HeaderText="Date Entered" DataField="Hdate"/>
</Columns>
</asp:GridView>
The Code-Behind for Gridview page
(resultsList is a DataTable type)
var resultsList = defendantRepository.Search(start, end, name, location);
if (Defendant.hasRecords)
{
ViewState["PreliminaryInjunctions"] = resultsList.Columns;
gv_Search.DataSource = resultsList.Columns;
gv_Search.DataBind();
}
The Search (populates a DataTable and returns it to the variable resultsList from above)
dv is a DefaultView and
ds is a DataSet
using (var da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
da.Fill(ds, "PreliminaryInjunctions");
}
dv = ds.Tables[0].DefaultView;
dt = dv.ToTable();
return dt;
The issue is I believe that you are binding the GridView to the Data Table's Columns property:
gv_Search.DataSource = resultsList.Columns;
gv_Search.DataBind();
That property is of type DataColumnCollection which does not contain names matching those defined in the BoundField elements, and hence the error.
Instead bind to the DataTable itself:
gv_Search.DataSource = resultsList;
gv_Search.DataBind();
On my webform for ASP.Net I display a table from SQL Server. However, what I would like to do is offer the user of the web page the ability to check some boxes and only show those particular columns once I refresh the page.
Here is the aspx html:
<div style="width: 1250px; height: 300px; overflow: auto">
<asp:GridView ID="GridView1" HeaderStyle-BackColor="Black" HeaderStyle-ForeColor="Silver" RowStyle-BackColor="#EEEEEE" AlternatingRowStyle-BackColor="White"
AlternatingRowStyle-ForeColor="#000" runat="server" AutoGenerateColumns ="false" AllowPaging="false" OnPageIndexChanging="OnPageIndexChanging" AllowSorting="True">
<Columns>
<asp:BoundField DataField ="WeekEndingDate" HeaderText="Week Ending Date" ItemStyle-Width="150px" dataformatstring="{0:MM-dd-yyyy}" />
<asp:BoundField DataField ="Week_Number" HeaderText="Week Number" ItemStyle-Width="150px" />
<asp:BoundField DataField ="Class" HeaderText="Class" ItemStyle-Width="150px" />
<asp:BoundField DataField ="Animal" HeaderText="Animal" ItemStyle-Width="150px" />
<asp:BoundField DataField ="North_Island" HeaderText="North Island" ItemStyle-Width="150px" DataFormatString="{0:F2}" />
<asp:BoundField DataField ="South_Island" HeaderText="South Island" ItemStyle-Width="150px" DataFormatString="{0:F2}" />
<asp:BoundField DataField ="New_Zealand" HeaderText="New Zealand" ItemStyle-Width="150px" DataFormatString="{0:F2}" />
</Columns>
</asp:GridView>
</div>
Here is the bindgrid method:
private void BindGrid()
{
string strConnString = "server= N-1077; Trusted_Connection=yes; database=Slaughter; connection timeout=30";
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CONVERT(date, Week_Ending_Date) AS WeekEndingDate," +
"Week_Number, Class, North_Island = CAST(North_Island as float), South_Island = CAST(South_Island as float)," +
"(CAST(North_Island as float)) + (CAST(South_Island as float)) AS New_Zealand," +
"Animal = (CASE WHEN Class = 'Sheep' OR Class = 'Lamb' THEN 'Ovine' WHEN Class = 'Calf' THEN 'Calf' ELSE 'Bovine' END)" +
"FROM Slaughter ORDER BY WeekEndingDate DESC"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
Is there a simple way to select particular columns and only show these? i.e. if they were only interested in week_number, class and south_island it would only display the data for those three columns? I don't mind building the SQL query based on the user inputs but it's how I change the boundfield Datafields to reflect on those columns that were selected in that query.
This is one way of doing it with the Visible property.
protected void RefreshSQLDisplay(object sender, EventArgs e)
{
foreach (BoundField col in GridView1.Columns)
{
if (col.DataField == "WeekEndingDate")
{
col.Visible = false;
break;
}
}
}
The disadvantage is that the columns will be still in memory and the sql query will retrieve the data. So it's better to change the sql query after all for performance reasons.
Update: I edited the code as you asked
How can I render links in columns of WebForms GridView?
I have the following code
SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.UsersContacts WHERE UserId='vika'", con);
con.Open();
var list1 = new List<string>();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var node = reader[1];
list1.Add(node.ToString());
}
}
con.Close();
GridView1.DataSource = list1;
GridView1.DataBind();
and I want to do something such as list1.Add("<a href='#'>"+node.ToString()+"<a>"); and make it a link in my GridView.
I would rather do this at the gridview template definition instead of passing the link from the datasource. Your gridview definition can have the asp:HyperLinkField or asp:HyperLink field and bind the data as required.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:HyperLinkField
HeaderText="View Details"
DataNavigateUrlFields="node"
DataNavigateUrlFormatString="~/TargetPage.aspx?Id={0}"
DataTextField="node"
/>
</Columns>
</asp:GridView>
Or
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("node", "~/TargetPage.aspx?Id={0}") %>' Text="View Details" />
I have a gridview which contains textboxes and dropdowns.In gvScheduleBattingScore_RowDataBound Event I am binding dropdowns without any problem. The button control is outside to the gridview. I actually want to submit all the textbox values and dropdown selected values to the database on buttonclickevent But I don't know where I am going wrong.
The Problem is Textboxes do not contain any text and am getting Exception
Input string was not in a correct format.
Please help me out...
<asp:GridView ID="gvScheduleBattingScore" runat="server" AllowSorting="false" AutoGenerateColumns="False" AllowPaging="false"
GridLines="None" CellPadding="1" CssClass="GridViewStyle" ShowFooter="false" width="100%"
OnRowDataBound="gvScheduleBattingScore_RowDataBound">
<Columns>
<asp:BoundField DataField="P_PlayerId" HeaderText="Player Id" HeaderStyle-Wrap="true" HeaderStyle-Width="5%" Visible="false"/>
<asp:BoundField DataField="PlayerName" HeaderText="Player Name" HeaderStyle-Wrap="true" HeaderStyle-Width="30%"/>
<asp:TemplateField HeaderText="Playing Order" HeaderStyle-Wrap="true" HeaderStyle-Width="5%">
<ItemTemplate>
<asp:TextBox ID="txtPlayingOrder" runat="server" CssClass="TinyTexBox"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:TextBox ID="txtStatus" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bold By">
<ItemTemplate>
<asp:DropDownList ID="ddlBoldBy" runat="server"> </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</br>
<asp:Button ID="ButtonAdd" runat="server" Text="Add" CssClass="SmallButton"
ValidationGroup="Add" onclick="ButtonAdd_Click"/>
On ButtonClick Event:
protected void ButtonAdd_Click(object sender, EventArgs e)
{
SqlConnection dBConnection = null;
try
{
int playerId;
short plyerOrder;
string BatsmanStatus;
int boldBy;
dBConnection = new SqlConnection();
dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["CriConn"].ConnectionString;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("SP_InsertScores", dBConnection);
cmd.CommandType = CommandType.StoredProcedure;
foreach (GridViewRow GVRow in gvScheduleBattingScore.Rows)
{
string textPlayerId = GVRow.Cells[0].Text;
TextBox textPlyerOrder = (TextBox)GVRow.Cells[1].FindControl("txtPlayingOrder");
TextBox textBatsmanStatus = GVRow.Cells[2].FindControl("txtStatus") as TextBox;
DropDownList DropDownBoldBy = (DropDownList)GVRow.Cells[18].FindControl("ddlBoldBy");
playerId = Convert.ToInt32(textPlayerId );
if (!string.IsNullOrEmpty(textPlyerOrder.Text))
plyerOrder = Convert.ToInt16(textPlyerOrder.Text);
if (!string.IsNullOrEmpty(textBatsmanStatus.Text))
BatsmanStatus = textBatsmanStatus.Text;
if (!string.IsNullOrEmpty(DropDownBoldBy.SelectedValue) && DropDownLbwBy.SelectedValue != "Select")
boldBy = Convert.ToInt32(DropDownBoldBy.SelectedValue);
cmd.Parameters.Add("#PlayerId", SqlDbType.NVarChar).Value = playerId;
cmd.Parameters.Add("#PlayerId", SqlDbType.Int).Value = playerId;
cmd.Parameters.Add("#plyerOrder", SqlDbType.Int).Value = plyerOrder;
cmd.Parameters.Add("#BatsmanStatus", SqlDbType.NVarChar).Value = BatsmanStatus;
dBConnection.Open();
dataAdapter.InsertCommand = cmd;
cmd.ExecuteNonQuery();
dBConnection.Close();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Close data reader object and database connection
cmd.Dispose();
cmd = null;
if (dBConnection.State == ConnectionState.Open)
dBConnection.Close();
}
As per the Chat discussion with #bhoopendra.sahoo, we come to the conclusion that it is a Binding issue.
When Button Click Event is fired, the GridView binds again causing the issue.
The fix is to bind the GridView only once and restrict its binding during other events.
Add a null checking before converting the textbox value to int
if (!string.IsNullOrEmpty(textPlayerId.Text))
playerId = Convert.ToInt32(textPlayerId);
Also make these changes to you code, to find the textbox controls in the correct cell
TextBox textPlyerOrder = (TextBox)GVRow.Cells[2].FindControl("txtPlayingOrder");
TextBox textBatsmanStatus = (TextBox)GVRow.Cells[3].FindControl("txtStatus");
I am attempting to bind a GridView to a DataSet, but it throws a Stack overflow error. When I debug it, it runs to the DataBind line just fine (it seems like its getting the right records from the server and everything), but after executing the DataBind, it jumps to the top of the method and reruns the entire method, which results in an stack overflow.
I can't understand why this isn't working. I've done something very similar with a DataTable before and it worked fine.
Here is how I'm binding
public void CreateGrid(String str)
{
try
{
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["MY_CONNECTION_STRING"].ConnectionString;
sqlConnection.Open();
DataSet dt = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(str, sqlConnection);
adapter.Fill(dt);
sqlConnection.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ":" + ex.Message);
}
if (dt.Tables.Count > 0)
{
Grid.DataSource = dt;
Grid.DataBind();
}
}
And here is my HTML part
<asp:GridView runat="server" ID="Grid" AutoGenerateColumns="false"
OnDataBinding="RebindGrid" AllowPaging="True" PageSize="10" AllowSorting="True" CellPadding="5"
OnPageIndexChanging="Grid_PageIndexChanging"
OnSorting="Grid_Sorting"
Width="100%" CssClass="mGrid">
<Columns>
<asp:BoundField DataField="ID" ItemStyle-Width="0%"
HeaderText="" Visible="false" SortExpression="ID"/>
</Columns>
</asp:GridView>
This looks like a problem
OnDataBinding="RebindGrid"
Every time data is bound, you rebind. We would have to see the code for RebindGrid.
It depends on what the RebindGrid method actually does but it looks like your rebinding your grid as your binding it.
Remove OnDataBinding="RebindGrid".