Hi,
I have a form, in which i have 2 dropdown boxes and textbox. When i select values from each of the dropdown box and give a value in the text box, i have a query formed in 2nd text box. I have a sujbmit button, by pressing which i need to execute the query. However, my query displays, when i select the values. I want to know how to execute the query on pressing submit button.
Please Help me!!!
Regards.
Loading
VJ RPPosted Jul 30, 2007, 5:47 PM
Munir ShaikhPosted Jul 30, 2007, 2:43 AM
VJRP
Here is an example i have written for you which will take care of your problem.
Here is "Aspx" page
<
form id="form1" runat="server"><div> Select Product:<asp:DropDownList ID="ddl1" runat="server">
<asp:ListItem Value="1">Windows XPasp:ListItem>
<asp:ListItem Value="2">Windows 2003 Serverasp:ListItem>
<asp:ListItem Value="3">Windows 2008asp:ListItem>
asp:DropDownList>
<br />
Quantity:<asp:TextBox ID="TextBox3" runat=server Width="44px" Text="50">asp:TextBox><br />Query Generated:<asp:TextBox ID="TextBox1" runat="server" Width="462px">asp:TextBox><br /><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Generate Query:" /><asp:Button ID="Button2" runat="server" Text="Fill Grid" OnClick="Button2_Click" /><br /><br />
<asp:GridView ID="GridView1" runat="server">asp:GridView>div>form>
string
ddown1, ddown2, txt;public void Page_Load(object sender, EventArgs e)
{
if(!(Page.IsPostBack))
{
getdata("SELECT * FROM Products");
}
} protected void Button1_Click(object sender, EventArgs e)
{
//ddown1 = ddl1.SelectedItem.Text;
ddown1 = ddl1.SelectedValue;
//ddown2 = ddl2.SelectedItem.Text;
txt = TextBox3.Text;
// if id is integer, don't need to place single quote
TextBox1.Text = "Select * from products where ID = " + ddown1;
} protected void Button2_Click(object sender, EventArgs e)
{
GridView1.DataSource = getdata(TextBox1.Text);
GridView1.DataBind();
} private static DataSet getdata(string sqlstr)
{
DataSet ds = new DataSet();
SqlConnection SqlCon = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand sqlComd = new SqlCommand(sqlstr, SqlCon);
try
{
SqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlComd);
da.Fill(ds);
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
}
finally
{
SqlCon.Close();
}
return ds;
}
Enjoy !! Easy coding
Regards,
>>Munnamax
VJ RPPosted Jul 29, 2007, 9:40 PM
Thanks for your reply. Infact, the results are not getting displayed in either a gridview or a text box. Please porvide with the peice of code, so that i can display the result in gridview.
Thanks in advance.
Sincerely.
Ravi sekhar KakumaniPosted Jul 27, 2007, 8:01 PM
public partial class _Default : System.Web.UI.Page
{
string ddown1, ddown2, txt;
public void Page_Load(object sender, EventArgs e)
{
getdata("select * from products")
}
protected void Button1_Click(object sender, EventArgs e)
{
ddown1 = ddl1.SelectedItem.Text;
ddown2 = ddl2.SelectedItem.Text;
txt = TextBox3.Text;
// if id is integer, don't need to place single quote
TextBox1.Text = "Select * from products where ProductID = '" + ddown1 + "' and Pname = '" + ddown2 + "' and Quantity = '" + txt + "'" ;
}
protected void Button2_Click(object sender, EventArgs e)
{
getdata(TextBox1.Text);
}
private static DataSet getdata(string sqlstr)
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\SQL Server 2000 Sample Databases\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=False");
SqlCommand sqlCommand = new SqlCommand(sqlstr,cn);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlCommand );
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
VJ RPPosted Jul 25, 2007, 8:03 PM
Hi,
Yes, It is a webform. Please find below my code. In my Button2_click, which is my submit, i have establised a connection string. I need the code to execute the query when button2 is clicked.
Please suggest me how to accomplish this!!
Thanks in advance.
VJRP.
public partial class _Default : System.Web.UI.Page
{
string ddown1, ddown2, txt;
public void Page_Load(object sender, EventArgs e)
{
SqlConnection cn =new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\SQL Server 2000 Sample Databases\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=False");
SqlDataAdapter da = new SqlDataAdapter("select * from products", cn);
DataSet ds = new DataSet();
}
protected void Button1_Click(object sender, EventArgs e)
{
ddown1 = ddl1.SelectedItem.Text;
ddown2 = ddl2.SelectedItem.Text;
txt = TextBox3.Text;
TextBox1.Text = "Select * from products where ProductID = " + ddown1 + " and Pname = " + ddown2 + " Quantity = " + txt ;
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\SQL Server 2000 Sample Databases\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=False");
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(TextBox1.Text, cn);
DataSet ds = new DataSet();
da.Fill(ds);
cn.Close();
}
}
Mike GoldPosted Jul 25, 2007, 7:19 PM