1
Answer

SQL Code/C# Code Problems

jen

jen

17y
1.9k
1

Hi!  Sorry if this is a stupid newbie question; I'm new to using Visual Studio 2005 with sql server/asp.net/c# .

I'm currently creating a website and i need functionality to search for a particular reference in my database and have it displayed on the page. For example, the way you can search for a book on amazon.com.

I know what needs to be done, but I'm having issues with where to insert the sql commands? I have code behind my page(c#) if (dropdownlist1.value) = whatever... then I need to have select * from [table] where textbox1.text = value to be search for.

Can I insert sql commands in the middle of my c# code?? I'm pretty confused! Or does it need to be in the asp.net code? If so, how do I link the c# and asp code??

Thanks for your help I'm so confused!!

JAK

Answers (1)
0
Niradhip Chakraborty

Niradhip Chakraborty

NA 6.5k 527k 17y

//U can go to the event of selectedIndex changed of your dropdown list
//and apply the following code.
SqlConnection con2 = null;
string str;
con2 = new SqlConnection(ConfigurationManager.AppSettings["con"].ToString());
//I have declared the connection string in con (web.config)

if (dropdownlist1.SelectedItem.ToString() == "whatever")
    {
     con2.Open();

    str = "select * from [table] where textbox1.text = value  ";
    SqlCommand sqlComm1 = new SqlCommand(str, con2);
    sqlComm1.ExecuteNonQuery();
    sqlComm1 = null;
     con2.close();
     }
Now u get the desire result in string str.
U can use str for your purpose.

make sure u make the autopostback property
of your dropdown list is true.

Dnt forget to use following namespace.

using System.Data.SqlClient;
using System.Configuration;