Click here to Skip to main content
16,011,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good afternoon,


XML
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:TabContainer ID="TabContainer1" runat="server" Width="372px"  ActiveTabIndex="0"  OnClientActiveTabChanged="clientActiveTabChanged" >
            <asp:TabPanel ID="test" runat="server" HeaderText="panel">
            <ContentTemplate>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" 
                 
           
            <table>
            <tr>
            <td><asp:Label ID="lblSearch" runat="server" Text="Search"></asp:Label></td>
            <td><asp:TextBox ID="txtSearch" runat="server"></asp:TextBox></td>
            <td>
                <asp:Button ID="btnSearch" runat="server" Text="Search"
                    onclick="btnSearch_Click" /></td>
            </tr>
            </table>
             <asp:GridView ID="TestGrid" runat="server" AutoGenerateColumns="False"
                    DataKeyNames="ItemID" DataSourceID="SqlDataSource1" Height="141px"
                    Width="334px"  >
             <Columns>
                 <asp:TemplateField HeaderText="Item Id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"  Text='<%# Bind("ItemID") %>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                     <asp:TemplateField HeaderText="ItemDescription">
                     <ItemTemplate>
                    <asp:LinkButton ID="LinkEdit" runat="server" Text='<%# Bind("ItemDescription") %>' CommandArgument='<%# Bind("ItemID")%>' OnClick="LinkEdit_OnClick"  ></asp:LinkButton>
                </ItemTemplate>
                 </asp:TemplateField>
                 <asp:BoundField DataField="Remarks" HeaderText="Remarks"
                     SortExpression="Remarks"  />
             </Columns>
              </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="<%$ ConnectionStrings:DeemahTestConnectionString %>"
                    SelectCommand="SELECT [ItemID], [ItemDescription], [Remarks] FROM [ItemMaster]">
                </asp:SqlDataSource>
                </ContentTemplate>
                </asp:UpdatePanel>
            </ContentTemplate>
            </asp:TabPanel>




and my code behind file is given below..
protected void btnSearch_Click(object sender, EventArgs e)
      {
          string connectionString = (string)ConfigurationManager.ConnectionStrings["DeemahTestConnectionString"].ConnectionString;
          ConnDeemah.ConnectionString = connectionString;
          string sqlQuery = "select top(1) * from itemMaster where ItemDescription like '%+txtSearch.Text+%' ";
          SqlCommand cmd = new SqlCommand(sqlQuery, ConnDeemah);
          ConnDeemah.Open();
          SqlDataReader rdr = null;
          rdr = cmd.ExecuteReader();

          ConnDeemah.Close();
          TestGrid.DataSource = rdr.ToString();
          TestGrid.DataBind();
      }

when i run this page its give me the error

"Both DataSource and DataSourceID are defined on 'TestGrid'. Remove one definition."


so please give me the way which i can do searching in gridview by textboxes

thanks

[edit]added pre tag
Posted
Updated 3-May-11 0:39am
v2

try this...

protected void btnSearch_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
string connectionString = (string)ConfigurationManager.ConnectionStrings["DeemahTestConnectionString"].ConnectionString;
ConnDeemah.ConnectionString = connectionString;
string sqlQuery = "select top(1) * from itemMaster where ItemDescription like '%+txtSearch.Text+%' ";
SqlDataAdapter da = new SQLDataAdapter(sqlQuery, ConnDeemah);
da.Fill(ds);

if(ds.Tables[0].Rows.Count > 0)
{
TestGrid.DataSource = ds;
TestGrid.DataBind();
}
else
{
 'print message no result found
}
}



Please check the case using intellisense, I have not used any editor so may be misspelled.

Hope this helps
 
Share this answer
 
v2
Comments
mohd vaquas 3-May-11 7:10am    
Its only return else messgae but record available in database
Remove one binding either from aspx or from .cs.

comment "TestGrid.DataSource = rdr.ToString();" line

or

remove entire "<asp:sqldatasource xmlns:asp="#unknown">" from aspx file
 
Share this answer
 
//you are Binding your DataGrid with SqlDataSource and from Code behind also 
//Remove One binding from your Code 

//From Code Behind 

string connectionString = (string)ConfigurationManager.ConnectionStrings["DeemahTestConnectionString"].ConnectionString;
ConnDeemah.ConnectionString = connectionString;
string sqlQuery = "select top(1) * from itemMaster where ItemDescription like '%+txtSearch.Text+%' ";
SqlCommand cmd = new SqlCommand(sqlQuery, ConnDeemah);
ConnDeemah.Open();
SqlDataReader rdr = null;
rdr = cmd.ExecuteReader();

ConnDeemah.Close();
TestGrid.DataSource = rdr.ToString();
TestGrid.DataBind();

//Or from Aspx Page


<asp:sqldatasource id=""SqlDataSource1"" runat=""server"<br" mode="hold" xmlns:asp="#unknown" />                    ConnectionString="<%$ ConnectionStrings:DeemahTestConnectionString %>"
                    SelectCommand="SELECT [ItemID], [ItemDescription], [Remarks] FROM [ItemMaster]">
                </asp:SqlDataSource>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900