Gud noon to every one!!
Actually i have generated/bind a Grid (insert update delete) through Data base property/sqldatasource1. and its working well now
But i want to put the some code in c# so that i can search any row data by putting their columnname in text boxes.
For this i have putted the following code insde .CS Page
but its not working please help me.
.CS code for searching :
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select h_id as 'Hoarding ID', Address,Size as 'Hoarding Size', State, District, Cost as 'Hoarding Cost', Pincode,From_date as 'Available From', To_date as 'Available To', hoarding_type as 'Hoarding Type', Lightboxes_type as 'Light Boxes', hoarding_image as 'Hoarding Image' from providers_post where h_id = '"+ TextBox1.Text+"' ",con);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
dt =ds.Tables[0];
if (dt.Rows.Count > 0)
{
//GridView1.DataSource = null;
//GridView1.DataSource = dt;
////GridView1.DataBind();
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
//GridView1.DataSourceID = "SqlDataSource1";
////GridView1.DataBind();
}
else
{
//Response.Write("");
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "alert('No Data Available.');", true);
}
con.Close();
}
And SqlDataSource1 code from designing page
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" Width="1100" AllowSorting="True"
DataSourceID="SqlDataSource1" ondatabound="GridView1_DataBound" >
" >
View Image
<%--">IMAGE--%>
Wrap="True" />
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:HoardingIndiaConnectionString %>"
DeleteCommand="DELETE FROM [providers_post] WHERE [h_id] = @original_h_id AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL)) AND (([Details] = @original_Details) OR ([Details] IS NULL AND @original_Details IS NULL)) AND (([Size] = @original_Size) OR ([Size] IS NULL AND @original_Size IS NULL)) AND (([State] = @original_State) OR ([State] IS NULL AND @original_State IS NULL)) AND (([District] = @original_District) OR ([District] IS NULL AND @original_District IS NULL)) AND (([Landmark] = @original_Landmark) OR ([Landmark] IS NULL AND @original_Landmark IS NULL)) AND (([Cost] = @original_Cost) OR ([Cost] IS NULL AND @original_Cost IS NULL)) AND (([Pincode] = @original_Pincode) OR ([Pincode] IS NULL AND @original_Pincode IS NULL)) AND (([From_date] = @original_From_date) OR ([From_date] IS NULL AND @original_From_date IS NULL)) AND (([To_date] = @original_To_date) OR ([To_date] IS NULL AND @original_To_date IS NULL)) AND (([hoarding_type] = @original_hoarding_type) OR ([hoarding_type] IS NULL AND @original_hoarding_type IS NULL)) AND (([hoarding_image] = @original_hoarding_image) OR ([hoarding_image] IS NULL AND @original_hoarding_image IS NULL)) AND (([Lightboxes_type] = @original_Lightboxes_type) OR ([Lightboxes_type] IS NULL AND @original_Lightboxes_type IS NULL)) AND (([Valid] = @original_Valid) OR ([Valid] IS NULL AND @original_Valid IS NULL))"
InsertCommand="INSERT INTO [providers_post] ([Address], [Details], [Size], [State], [District], [Landmark], [Cost], [Pincode], [From_date], [To_date], [hoarding_type], [hoarding_image], [Lightboxes_type], [Valid]) VALUES (@Address, @Details, @Size, @State, @District, @Landmark, @Cost, @Pincode, @From_date, @To_date, @hoarding_type, @hoarding_image, @Lightboxes_type, @Valid)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [h_id], [Address], [Details], [Size], [State], [District], [Landmark], [Cost], [Pincode], [From_date], [To_date], [hoarding_type], [hoarding_image], [Lightboxes_type], [Valid] FROM [providers_post] ORDER BY [h_id]"
UpdateCommand="UPDATE [providers_post] SET [Address] = @Address, [Details] = @Details, [Size] = @Size, [State] = @State, [District] = @District, [Landmark] = @Landmark, [Cost] = @Cost, [Pincode] = @Pincode, [From_date] = @From_date, [To_date] = @To_date, [hoarding_type] = @hoarding_type, [hoarding_image] = @hoarding_image, [Lightboxes_type] = @Lightboxes_type, [Valid] = @Valid WHERE [h_id] = @original_h_id AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL)) AND (([Details] = @original_Details) OR ([Details] IS NULL AND @original_Details IS NULL)) AND (([Size] = @original_Size) OR ([Size] IS NULL AND @original_Size IS NULL)) AND (([State] = @original_State) OR ([State] IS NULL AND @original_State IS NULL)) AND (([District] = @original_District) OR ([District] IS NULL AND @original_District IS NULL)) AND (([Landmark] = @original_Landmark) OR ([Landmark] IS NULL AND @original_Landmark IS NULL)) AND (([Cost] = @original_Cost) OR ([Cost] IS NULL AND @original_Cost IS NULL)) AND (([Pincode] = @original_Pincode) OR ([Pincode] IS NULL AND @original_Pincode IS NULL)) AND (([From_date] = @original_From_date) OR ([From_date] IS NULL AND @original_From_date IS NULL)) AND (([To_date] = @original_To_date) OR ([To_date] IS NULL AND @original_To_date IS NULL)) AND (([hoarding_type] = @original_hoarding_type) OR ([hoarding_type] IS NULL AND @original_hoarding_type IS NULL)) AND (([hoarding_image] = @original_hoarding_image) OR ([hoarding_image] IS NULL AND @original_hoarding_image IS NULL)) AND (([Lightboxes_type] = @original_Lightboxes_type) OR ([Lightboxes_type] IS NULL AND @original_Lightboxes_type IS NULL)) AND (([Valid] = @original_Valid) OR ([Valid] IS NULL AND @original_Valid IS NULL))">
dateParameters>
11 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Satyapriya NayakPosted Feb 21, 2013, 3:20 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter ad = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dataTable;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
Session["sortBy"] = null;
if (!IsPostBack)
{
FillVendorGrid();
}
}
private void FillVendorGrid()
{
SqlConnection conn = new SqlConnection(connStr);
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM Vendor";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
ResultGridView.DataSource = dataTable;
ResultGridView.DataBind();
}
protected void ResultGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
ResultGridView.EditIndex = e.NewEditIndex;
FillVendorGrid();
}
protected void ResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ResultGridView.PageIndex = e.NewPageIndex;
FillVendorGrid();
}
protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Vendor WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FillVendorGrid();
}
protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtFName = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtFName");
TextBox txtLName = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtLName");
TextBox txtCity = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCity");
TextBox txtState = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtState");
TextBox txtCountry = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCountry");
TextBox txtDescription = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtDescription");
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "UPDATE Vendor SET VendorFName ='" + txtFName.Text + "',VendorLName ='" + txtLName.Text + "',VendorCity ='" + txtCity.Text + "',VendorState ='" + txtState.Text + "',VendorCountry ='" + txtCountry.Text + "',VendorDescription ='" + txtDescription.Text + "' WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
ResultGridView.EditIndex = -1;
FillVendorGrid();
conn.Close();
}
protected void ResultGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ResultGridView.EditIndex = -1;
FillVendorGrid();
}
protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtFName = (TextBox)ResultGridView.FooterRow.FindControl("txtFName1");
TextBox txtLName = (TextBox)ResultGridView.FooterRow.FindControl("txtLName1");
TextBox txtCity = (TextBox)ResultGridView.FooterRow.FindControl("txtCity1");
TextBox txtState = (TextBox)ResultGridView.FooterRow.FindControl("txtState1");
TextBox txtCountry = (TextBox)ResultGridView.FooterRow.FindControl("txtCountry1");
TextBox txtDescription = (TextBox)ResultGridView.FooterRow.FindControl("txtDescription1");
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Vendor(VendorFName, VendorLName,VendorCity,VendorState,VendorCountry,VendorDescription) Values('" + txtFName.Text + "', '" + txtLName.Text + "', '" + txtCity.Text + "', '" + txtState.Text + "', '" + txtCountry.Text + "' , '" + txtDescription.Text + "')";
conn.Open();
cmd.ExecuteNonQuery();
FillVendorGrid();
conn.Close();
}
}
protected void btn_search_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
str = "select * from Vendor where VendorFName like '" + TextBox1.Text + "%'";
cmd = new SqlCommand(str, conn);
sqlda = new SqlDataAdapter(cmd);
ds = new DataSet();
sqlda.Fill(ds, "Vendor");
conn.Close();
ResultGridView.DataSource = ds;
ResultGridView.DataMember = "Vendor";
ResultGridView.DataBind();
}
protected void ResultGridView_Sorting(object sender, GridViewSortEventArgs e)
{
Session["sortBy"] = e.SortExpression;
FillVendorGrid();
}
}
Abhimanyu SinghPosted Feb 20, 2013, 12:44 AM
But only the problem is in i want some search button here with text boxes (see design page/attachment). while i coded on image button event initially it was throwing sqldatasource defination duplicacy, i tried every thing whis are instructed by you but its not working every time throwing some new error.
Please help me (any body here)
Sarch code on image button event:(According to you)
Mayur GujrathiPosted Feb 19, 2013, 2:16 AM
Hover on dataset after
da.Fill(ds);
and check dataset filled or not..
Abhimanyu SinghPosted Feb 19, 2013, 2:10 AM
I did every possible thing as instructed by you and as per last code i again reached on initial stage where from i started still m getting the datasource binding duplicacy.
According to you the final code are:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select h_id as 'Hoarding ID', Address,Size as 'Hoarding Size', State, District, Cost as 'Hoarding Cost', Pincode,From_date as 'Available From', To_date as 'Available To', hoarding_type as 'Hoarding Type', Lightboxes_type as 'Light Boxes', hoarding_image as 'Image Path' from providers_post where h_id = '"+ TextBox1.Text+"' ",con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();
}
I also removed the table as well as deleted the loop for table row data count
still its throwing the previous error:
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Mayur GujrathiPosted Feb 19, 2013, 1:18 AM
GridView1.DataSource =ds.tables[0];
GridView1.DataBind();
Abhimanyu SinghPosted Feb 19, 2013, 12:27 AM
then throwing the error:
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'Table' could not be found.
Abhimanyu SinghPosted Feb 18, 2013, 11:59 PM
Good Morning!!
Sir, I already tried that code instructed by you, but it's throwing the following error
Cannot implicitly convert type 'System.Data.DataTable' to 'string'
and m new in this field so i m unable to identify whats the problem actually.
Can you help me deeply i attached the file here
.cs Code:
Mayur GujrathiPosted Feb 18, 2013, 7:41 AM
Mayur GujrathiPosted Feb 18, 2013, 7:37 AM
just do
GridView1.DataSourceID = ds.Tables[0];
GridView1.DataBind();
Abhimanyu SinghPosted Feb 18, 2013, 7:32 AM
Please read once again in above what i want exactly: means i want when we put any searching keywords in text boxes then it should display in grid. I all ready coded on image button event please check
//GridView1.DataSource = null;
//GridView1.DataSource = dt;
////GridView1.DataBind();
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
//GridView1.DataSourceID = "SqlDataSource1";
////GridView1.DataBind();
Mayur GujrathiPosted Feb 18, 2013, 6:10 AM
Give us exact error.