Solved

SQL and C# for a ddl to a gridview

Posted on 2010-09-14
2
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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