Solved

SQL and C# for a ddl to a gridview

Posted on 2010-09-14
2
239 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 104
Simple Injector with Web Service 4 38
Cant save 3D 4 19
Need help how to find where my error is in UFD 6 25
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

11 Experts available now in Live!

Get 1:1 Help Now