Solved

SQL and C# for a ddl to a gridview

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FTP file download using c# 3 25
how to double quote a string for an inline sql statement. 8 73
jquery + C#.net radiobuttonlist 3 28
C# Linq Select From List 3 5
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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