Solved

SQL and C# for a ddl to a gridview

Posted on 2010-09-14
2
236 Views
Last Modified: 2012-05-10
Hello all, this one may be easy for you but its kickin me in the touche!!!

I have a Sql table called Course_Detail.and in it the fileds are Course_Abb, Major, CourseNum, SubClass, CourseTitle, RQ, CourseDesc and CatalogYear.

I have a webpage that has the Course_Abb and CourseTitle in a single dropdownlist.
I want to add the value of that ddl to a gridview. I was trying to use a like statement but its not working so much. Is there anyway to only query the first 6 charters in the ddl?

 
sql Code-------------------\
__________________________________
select * from course_detail where Course_Abb like '%AC430 %'

C# code--------------------
________________________________________
System.Data.SqlClient.SqlConnection connectionstuff = new SqlConnection();
        connectionstuff.ConnectionString = ConfigurationManager.ConnectionStrings["strDBCourseStandard"].ConnectionString;
        connectionstuff.Open();

        string finditwoman = "Select * from Course_Detail where CatalogYear ='" + ddlSchoolYear.SelectedValue + "' and Major = '" + DDLMajor.SelectedValue + "'  ";
       SqlCommand objectionCommand = new SqlCommand(finditwoman, connectionstuff);
       SqlDataAdapter DataAdapter = new SqlDataAdapter("Select * from Course_Detail where CatalogYear ='" + ddlSchoolYear.SelectedValue + "' and Major = '" + DDLMajor.SelectedValue + "'", connectionstuff);
       System.Data.DataSet dss = new System.Data.DataSet();
       DataAdapter.Fill(dss);
       DataGrid1.DataSource = dss.Tables[0];
       DataGrid1.DataBind();

Open in new window

0
Comment
Question by:HawaiiDragon
2 Comments
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 33680423
Hello HawaiiDragon,

Can u explain with an example?

Are u binding Course_Abb and CourseTitle in a seperate dropdown or what?
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 33681624
Hi,

If you only want the six first characters you can do something like (let's assume the dropdown is called ddlCourse_Abb):

Dim strSql As String = "select * from course_detail where Course_Abb = @Course_Abb"
Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, connectionstuff)
da.SelectCommand.Parameters.AddWithValue("@Course_Abb", ddlCourse_Abb.SelectedValue.ToString().SubString(0, 6))
Dim dt As New DataTable()
da.Fill(dt)

etc, etc...

The trick is to use the "SubString()" method. Just specify where in string to start (i.e. 0 for the first character) and the length to include (i.e. 6).

/Carl.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now