I have a gridview on my webpage that binds from an excel sheet using OleDb
Now I'm trying to set it up so that people can search just by typing a word in the textbox, and then after button click, it'd filter all the results that have the same word in any column
I was advised to use this code:
protected void Button1_Click(object sender, EventArgs e)
{
string search = TextBox1.Text;
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
grvCarProof.DataSource = dt.Select("CP number = ");
grvCarProof.DataBind();
}
what I can't figure out is what kind of writing should go in my SelectCommand(). Am i on the right track
Use DataView and its RowFilter property.
DataView dv=dt.DefaultView;
dv.RowFilter="[Cp Number]='value']";
grvCarProof.DataSource = dv;
grvCarProof.DataBind();
Or add WHERE clause,
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$] where [column1]=#col1", conn);
cmd.Parameters.AddWithValue("#col1",inputValueFromUser);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
grvCarProof.DataSource = dt;
grvCarProof.DataBind();
Related
can anybody help?
I have two stored procs, each calling two separate result tables.
I need to create a dropdown list so when a table is selected it will refresh the page and populate the grid view with the new table result...
at the moment all i have is the connection calling one of the tables on the page load... but i need this populating to the dropdown list also...
public void Refreshdata()
{
SqlConnection con = new SqlConnection(#"Server=(LocalDB)\v11.0;Database=WorkstationCells");
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "dbo.uspTargetQuantitesg120";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Parameters.Add("#Product", SqlDbType.VarChar, 10).Value = "g120C";
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
I know similar questions have been answered but i am new to this and not sure where to start.
First create a drop down list in your form and set AutoPostBack=true
<asp:DropDownList ID="DropDownlist1" runat="server" AutoPostBack="true"
Width="200px" onselectedindexchanged="DropDownlist1_Changed">
</asp:DropDownList>
Refreshdata()
void Refreshdata()
{
SqlConnection con = new SqlConnection(#"Server=(LocalDB)\v11.0;Database=WorkstationCells");
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "dbo.uspTargetQuantitesg120";
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Parameters.Add("#Product", SqlDbType.VarChar, 10).Value = "g120C";
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Void DropDown()
void DropDown()
{
con.Open();
String str = "Your Query";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery()
con.Close();
}
Page_Load()
if (!IsPostBack)
{
DropDown();
}
}
DropDownlist1_Changed()
protected void DropDownlist1_Changed(object sender, EventArgs e)
{
Refresh();
}
Please try this to bind data to DropDownList in GridView in ASP.Net
https://www.aspsnippets.com/Articles/How-to-bind-data-to-DropDownList-in-GridView-in-ASPNet.aspx
I'm trying to filter my GridView with values from a header dropdown.
However I get this error message: "DataBinding: 'System.Data.DataRowView' does not contain a property with the name '[AnotherColumnThatIsNotSTATUS]'"
[AnotherColumnThatIsNotSTATUS] = another column for some reason and not STATUS?
Can anyone see what I´m missing?
protected void ddlStatusHeader_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlStatusHeader = ((DropDownList)sender);
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT [Status] FROM [BI_Planning].[dbo].[tblStatus] WHERE [Status] LIKE '%' + #Status + '%' ", con);
cmd.Parameters.AddWithValue("#Status", ddlStatusHeader.SelectedValue);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Status");
gwActivity.DataSource = ds;
gwActivity.DataBind();
}
}
}
This is working for me.Hope this helps..
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select [text] from main where status='"+DropDownList1.SelectedValue.ToString()+"'";
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Status");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
Have you specified Columns for your Gridview??
I do import the data from Excel sheet to Sql database..everything is fine,when i run this code,my access engine could not find mt sheet,it throws like this error..
my error is
The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
but i already checked with my designation folder,its correct..then i don't know why it's repeated
my C# code below..
public partial class _Default : System.Web.UI.Page
{
string constr = #"Data Source=VIS1-B12\SQLEXPRESS;Initial Catalog=Sql_Excel;Integrated Security=True providerName=System.Data.SqlClient" ;
protected void btn_okClick(object sender, EventArgs e)
{
string path = Fup_Excel.PostedFile.FileName;
string exconstr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties=Excel 8.0";
OleDbConnection excelcon = new OleDbConnection(exconstr);
excelcon.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", excelcon);
OleDbDataReader dbreader;
OleDbDataAdapter dap = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
//dap.Fill(ds,"sheet1");
dbreader = cmd.ExecuteReader();
SqlBulkCopy bcpy = new SqlBulkCopy(constr);
bcpy.DestinationTableName = "Excel_Details";
bcpy.WriteToServer(dbreader);
//GridView1.DataSource = ds.Tables[0].DefaultView;
//GridView1.DataBind();
excelcon.Close();
}
}
It's better you should retrieve excel sheet name using code.
Code is shown below
OleDbConnection con = new OleDbConnection(ConnString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
I need to delete a row from gridview and database based on it's id which is unique identifier.
In my code below I get error msg " Unrecognized Guid format" .Any help to fix it?
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand("Delete", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#ID", SqlDbType.UniqueIdentifier);
command.Parameters["#ID"].Value = new System.Guid(id);
command.Connection.Open();
command.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
command.Connection.Close();
}
Try this:
Guid theGuid = new Guid();
System.Data.SqlTypes.SqlGuid.Parse(theGuid.ToString());
Adding a Guid Parameter into SqlCommand
Use Guid.TryParse on your id string, then you'll know what's going on.
I am using asp.net 2008 repeater control. I am trying to refresh the repeater control when a button is clicked, it shows me duplicated values, that means its appending the same values once again. Here is my code
public void LoadRepeater1()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnection"].ConnectionString);
String sql;
sql = "select * from FeeCollection Where AdminNo='"+lblstano.Text+"'";
cmd = new SqlCommand(sql, con);
da = new SqlDataAdapter(sql, con);
da.Fill(ds, "FeeCollection");
dt = ds.Tables["FeeCollection"];
Repeater4.DataSource = dt;
Repeater4.DataBind();
con.Close();
}
protected void btnMedical_Click(object sender, EventArgs e)
{
LoadRepeater1();
}
I want to remove the existing data and refresh the data instead of appending.
Not sure where ds (DataSet) in instantiated. Try to instantiate the DataSet/DataTable within the LoadRepeater1() method.
public void LoadRepeater1()
{
con = new SqlConnection(ConfigurationManager
.ConnectionStrings["SqlServerConnection"].ConnectionString);
sql = "select * from FeeCollection Where AdminNo=#AdminNo";
cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#AdminNo",System.Data.SqlDbType.VarChar,20).Value=lblstano.Text;
da = new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
Repeater4.DataSource = dt;
Repeater4.DataBind();
}