Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL and C# for a ddl to a gridview

Posted on 2010-09-14
2
Medium Priority
?
247 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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