Click here to Skip to main content
15,925,661 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,


I am develop on web application using c#and asp.net and using sqlserver2008
these web application has one textbox one command button and one gridview
sql server2008 table also one table i.e. svf the svf has the coloumns sno
rdate and amount .i am entered the date in textbox the gridview display
rdate data so please give the code and i writed some code

SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
con.Open();
string strQuery = "select * from stv where rdate =@Textbox6.Text";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
cmd.Parameters.AddWithValue("@rdate", TextBox6.Text);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
gvuser.DataSource = dt;
gvuser.DataBind();
con.Close();
but i got error while on web run at command buttom click event

Fill: SelectCommand.Connection property has not been initialized.
Posted
Updated 25-Oct-13 0:09am
v2
Comments
Thanks7872 25-Oct-13 5:13am    
please give the code

No one will. Its your responsibility to implement. We are here to help you if you face any problem with that code.
Raghavendra Guptha 25-Oct-13 6:00am    
SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
con.Open();
string strQuery = "select * from stv where rdate =@Textbox6.Text";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
cmd.Parameters.AddWithValue("@rdate", TextBox6.Text);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
gvuser.DataSource = dt;
gvuser.DataBind();
con.Close();


but i got error on web application run error is "Fill: SelectCommand.Connection property has not been initialized."
Thanks7872 25-Oct-13 6:07am    
See the updated answer.
Tom Marvolo Riddle 25-Oct-13 5:26am    
Frequent Question here.Search it and you will get it.

Your code should be like this:
C#
//connections
string strQuery = "select * from stv where rdate =@something";

SqlCommand cmd = new SqlCommand(strQuery,con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@something", TextBox6.Text);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();
sda.Fill(dt);
gvuser.DataSource = dt;
gvuser.DataBind();
con.Close();

Regards..
 
Share this answer
 
v3
Comments
Raghavendra Guptha 25-Oct-13 6:33am    
but not showing gridview what is problem here is given the asp page code

<asp:GridView ID="gvuser" runat="server" AutoGenerateColumns="False" PageSize="10"
Width="1017px" Caption="Settlement vouchers Feeding Details" AllowPaging="true"
>
<columns>
<asp:TemplateField HeaderText="S.No">
<itemtemplate>
<asp:Label ID="label1" Visible="true" runat="server" Text='<%# Eval("sno") %>'>


<asp:TemplateField HeaderText="RDATE">
<itemtemplate>
<asp:Label ID="label2" Visible="true" runat="server" Text='<%# Eval("rdate") %>'>


<asp:TemplateField HeaderText="ACNO">
<itemtemplate>
<asp:Label ID="label3" Visible="true" runat="server" Text='<%# Eval("acno") %>'>


<asp:TemplateField HeaderText="NAME">
<itemtemplate>
<asp:Label ID="label4" Visible="true" runat="server" Text='<%# Eval("name") %>'>


<asp:TemplateField HeaderText="VNO">
<itemtemplate>
<asp:Label ID="label5" Visible="true" runat="server" Text='<%# Eval("vno") %>'>


<asp:TemplateField HeaderText="AMOUNT">
<itemtemplate>
<asp:Label ID="label6" Visible="true" runat="server" Text='<%# Eval("amt") %>'>


<asp:TemplateField HeaderText="EDATE">
<itemtemplate>
<asp:Label ID="label7" Visible="true" runat="server" Text='<%# Eval("edate") %>'>


<asp:TemplateField HeaderText="CHNO">
<itemtemplate>
<asp:Label ID="label8" Visible="true" runat="server" Text='<%# Eval("chno") %>'>



Use below mentioned code snippet

SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
string strQuery = "select * from stv where rdate="+"'"+Textbox6.Text+"'";
SqlCommand cmd = new SqlCommand(strQuery);
SqlDataAdapter sda = new SqlDataAdapter(cmd ,con);
DataSet ds = new DataSet();
ds.clear();
sda.Fill(ds);
gvuser.DataSource = ds;
gvuser.DataBind();


For your information when using data adapter no need to provide con.open or close it will manage itself.

Hope This Helps!!!
 
Share this answer
 
Comments
Member 11762610 16-Jun-15 5:03am    
How to search data between two datetimepicker in C#

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