Close

Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Help c#.net beginners help

    I'm hoping someone might be able to help me here or point me in the right direction.

    I've just started out learning a bit of c# using visual studio as i'm supposed to be writing a few web applications in the coming months for work.
    I have managed to create a web form that has a drop down list, and a blank datagrid. I have managed to connect the drop down list to one of our databases and used a stored procedure to populate the drop down list with the names of all the people who work for our company.
    My question is, for now.....how can i make it so that when a user chooses a member of staff from the drop down list, the datagrid displays a row that contains that particular member of staff, along with all the other fields in the database (eg. job title, date of birth etc).

    If anyone can even help me slightly or point me in the right direction i'd be grateful. Cheers!
    LEEDS AND PROUD

  2. #2
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    I'm rusty at this but here goes...

    Make sure the drop down list is populated with not only the member of staff's name but that the value field has a unique ID for them.

    Write an OnSelect method so when a drop down item is chosen it calls another stored procedure and passes the Unique ID.

    The stored procedure returns all the details for that particular member of staff and you bind that info to the datagrid.

    Let us know how you get on.

  3. #3
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    cheers for the reply but im still a bit stuck. let me see if i can describe it a bit better.


    i now have a dropdown box that displays a big list of names that it has got from a stored procedure. i now have a button next to it. when i click that button, i want it to display a datagrid showing only the name of the person selected in the dropdown PLUS all of the that persons other fields (eg. address, job title, phone number)

    i just need to know what to stick in this part:


    private void Button1_Click(object sender, System.EventArgs e)
    {

    myComponent.FillDataSet(empDataSet1);
    DataGrid2.DataBind();



    }


    (all that this does at the moment is fill the datagrid with the entire set of data for every name in the list. i want it to only return one row, with the details of the person selected in the drop down box. my dropdownbox is called DropDownList1)


    Sorry if this is badly explained, but HELP!
    LEEDS AND PROUD

  4. #4
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: c#.net beginners help

    www.asp.net

    Download the 'webmatrix' tool. Makes it very very easy.
    We all make mistakes sometimes

  5. #5
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    Quote Originally Posted by Goldberg
    www.asp.net

    Download the 'webmatrix' tool. Makes it very very easy.
    Cheers, just reading up on it now, sounds like it makes it much easier! I'll have a dabble with it, test it out and see what the boss thinks.
    LEEDS AND PROUD

  6. #6
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: c#.net beginners help

    I use it for my aspx pages, both vb.net and c#

    It doesn't have tab strips and tree views as standard though, you have to d/l them and insert manually (a bit tricky but does work in the end).
    We all make mistakes sometimes

  7. #7
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    Wish I could provide code examples but I'm really rusty so can only remember how to do it in theory.

    Get your stored procedure to return the names and a unique ID of everyone in the db i.e. 'SELECT ID, firstName, LastName FROM Staff'

    Bind the firstName and lastName fields to the Value field and the ID to the ID field of the dropdown.

    In the method you posted you need some code to retrieve the ID of the selected name from the drop down list. Pass this ID into a NEW stored procedsure which would look somethig like:
    'SELECT firstName, lastName, address, title, phone FROM Staff WHERE ID = @ID'

    That stored procedure would only return the one record of the member of staff you selected from the drop down. You can then bind that to the datagrid.

    There will be a way of just going to the database once and just working with a dataset instead but I'm even more rusty at that

  8. #8
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    BTW Webmatrix is just a very basic alternative to Visual Studio. Anything you can do in it you can also do in VS. Some people do prefer it though.

    If you havn't sussed it all by tonight I'll try coding it myself and will pass on the code.

  9. #9
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    Cheers tricellet and goldberg

    I have managed to do a couple of basic web apps in webmatrix such as getting data from a database and displaying it in a datagrid, im still struggling with dropdown boxes and my problem that i stated earlier however. Ill keep having a go at it though, any code anyone can give me would be good though, or any tutorials for what im looking for.
    LEEDS AND PROUD

  10. #10
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    PM your code to me.

  11. #11
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: c#.net beginners help

    Quote Originally Posted by copey123
    Cheers tricellet and goldberg

    I have managed to do a couple of basic web apps in webmatrix such as getting data from a database and displaying it in a datagrid, im still struggling with dropdown boxes and my problem that i stated earlier however. Ill keep having a go at it though, any code anyone can give me would be good though, or any tutorials for what im looking for.
    I started off by using a 'Sams teach yourself in 24 hours' book many moons ago mate. I got this because it focuses on WebMatrix. I find webmatrix easier to use than VS as it has wizards etc... for connecting to the database for datagrids etc....

    For your drop down list, make sure you set the Value field and text field as the relevant name from the database in the design part, then create a 'select' function in the code part. Call the code in the Page_Load or where ever else you need to call the info then simply use:

    Code:
    ddlDropDownListName.Datasource = CallSelectFunctionHere(any variables needed here)
    ddlDropDownListName.Databind()
    That should do it.
    Last edited by Goldberg; 4th August 2005 at 01:50 PM.
    We all make mistakes sometimes

  12. #12
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    Right then lads, i'm slowly getting there. I have now managed to create a web app that displays a drop down box with every employee ID (which it gets from a database), and when u click the button next to it, a datagrid appears with all the details of that certain employee.
    The only problem is that i am want the drop down to display the NAME of the employee, not their employee number. The way the code is written makes it difficult to hae anything but the employee number in the dropdown box, because it needs that number from the box to use in the fucntion wher eit brings back all the employees details.

    anyway, here's the code, i hope someone can shed some light on it.

    Function Emps(ByVal employeeID As Integer) As System.Data.DataSet
    Dim connectionString As String = "server='(local)'; trusted_connection=true; database='JumpData'"
    Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

    Dim queryString As String = "SELECT [Employees].[EmployeeID], [Employees].[First Name], [Employees].[Last Name"& _
    "], [Employees].[Birthdate], [Employees].[Branch] FROM [Employees] WHERE ([Employ"& _
    "ees].[EmployeeID] = @EmployeeID)"
    Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    Dim dbParam_employeeID As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
    dbParam_employeeID.ParameterName = "@EmployeeID"
    dbParam_employeeID.Value = employeeID
    dbParam_employeeID.DbType = System.Data.DbType.Int32
    dbCommand.Parameters.Add(dbParam_employeeID)

    Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
    dataAdapter.SelectCommand = dbCommand
    Dim dataSet As System.Data.DataSet = New System.Data.DataSet
    dataAdapter.Fill(dataSet)

    Return dataSet
    End Function

    Function EmpNum() As System.Data.DataSet
    Dim connectionString As String = "server='(local)'; trusted_connection=true; database='JumpData'"
    Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

    Dim queryString As String = "SELECT [Employees].[EmployeeID] FROM [Employees]"
    Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
    dataAdapter.SelectCommand = dbCommand
    Dim dataSet As System.Data.DataSet = New System.Data.DataSet
    dataAdapter.Fill(dataSet)

    Return dataSet
    End Function

    Sub Page_Load(Sender As Object, E As EventArgs)
    If Not Page.IsPostBack Then
    DropDownList1.DataTextField = "EmployeeID"
    DropDownList1.DataSource = EmpNum()
    DropDownList1.DataBind()
    End If
    End Sub

    Sub Button1_Click(sender As Object, e As EventArgs)
    DataGrid1.DataSource = _
    Emps(DropDownList1.Items(DropDownList1.SelectedIndex).Text)
    DataGrid1.DataBind()
    End Sub
    ps. i might have to invest in one of those sams books!
    LEEDS AND PROUD

  13. #13
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    Change the stored procedure that gets just the ID to also get the first and last name i.e. SELECT employeeid, firstname, lastname from etc.

    Then...
    Sub Page_Load(Sender As Object, E As EventArgs)
    If Not Page.IsPostBack Then
    DropDownList1.DataTextField = "EmployeeID"
    DropDownList1.DataSource = EmpNum()
    DropDownList1.DataBind()
    End If
    End Sub
    Change this to...
    Sub Page_Load(Sender As Object, E As EventArgs)
    If Not Page.IsPostBack Then
    DropDownList1.DataTextField = "First Name" + "Last Name" (syntax may be wrong here)
    DropDownList1.DataValueField = "EmployeeID"

    DropDownList1.DataSource = EmpNum()
    DropDownList1.DataBind()
    End If
    End Sub
    Then change the line that grabs the ID...
    Emps(DropDownList1.Items(DropDownList1.SelectedInd ex).Text)
    To...
    Emps(DropDownList1.Items(DropDownList1.SelectedInd ex).Value)
    Any luck?

  14. #14
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: c#.net beginners help

    Right, the best way to do this is as follows:

    For the Drop Down List in Design mode set the Text Field to 'Name' (or whatever the field in the DB is) and the Value to 'EmployeeID'

    Then when you call the Select procedure request the record as you have done based on the EmployeeID field. This is done easily by declaring a variable first (string or integer depending on the format of the Employee ID's).

    So at this stage you would have the Text field of the DDL showing the name and the value behind will be the EmployeeID for that Employee.

    Now the variable that you have declared, simply do a 'VariableName = DropDownListName.SelectedValue

    Then when you call the select method to show the info for an employee, pass this variable through to bring out the employee's details

    Hope that made sense.
    We all make mistakes sometimes

  15. #15
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1153

    Default Re: c#.net beginners help

    Tricellet beat me to it but same thing, only he had the time to change the code lol.

    As for the sams books, I have the 'Teach yourself ASP.Net in 24 hours' by Scott Mitchell and then I have the Sams ASP.Net bible (I think thats the title i'll check when I get in).

    I always find however that what I want to do either doesnt exist or is a bastard to do lol. So a lot of my work is 'the long' way around, but the books help me get there.
    Last edited by Goldberg; 4th August 2005 at 03:28 PM.
    We all make mistakes sometimes

  16. #16
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    tricellet you beauty!! That worked a treat cheers. The only thing it didn't like was the "First Name" + "Last Name" , and i'm not entirely sure what the syntax is either but i'm sure i'll be able to find it.
    Thanks again! No doubt i'll be back over the next few days with more probs as i'm learning.
    LEEDS AND PROUD

  17. #17
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    Cheers to goldberg too, you can always rely on DF members when yer stuck I'll have a look for that book too, i'll bloody need it these next few weeks!
    LEEDS AND PROUD

  18. #18
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    If it isn't + then it'll be &

  19. #19
    DF VIP Member copey123's Avatar
    Join Date
    Nov 2001
    Location
    West Yorkshire
    Posts
    973
    Thanks
    6
    Thanked:        2
    Karma Level
    322

    Default Re: c#.net beginners help

    Quote Originally Posted by tricellet
    If it isn't + then it'll be &
    nope, that dont work either :nowords:


    DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name First NameLast Name.
    LEEDS AND PROUD

  20. #20
    DF VIP Member tric's Avatar
    Join Date
    Apr 2002
    Location
    uk
    Posts
    314
    Thanks
    4
    Thanked:        12
    Karma Level
    297

    Default Re: c#.net beginners help

    Yeh was thinking about this and it wouldn't work as it's expecting the name of a field and we're trying to give it two. Tried looking on Google but couldn't find any examples of how it could be done. All I can suggest at the minute is having another field in the db which contains their full name.

Page 1 of 2 12 LastLast

Similar Threads

  1. Cheap Golf clubs for beginners.....
    By webslinger2k in forum Cheapskates Corner
    Replies: 3
    Last Post: 6th February 2006, 12:54 AM
  2. Anatomy For Beginners
    By incognito™ in forum TV Talk
    Replies: 21
    Last Post: 28th January 2005, 05:40 PM
  3. Beginners guide
    By T24 in forum Digital Satellite TV
    Replies: 9
    Last Post: 4th April 2004, 08:10 PM
  4. Beginners Guides.....
    By neocortex in forum Programming
    Replies: 20
    Last Post: 7th October 2002, 07:22 PM
  5. Absolute Beginners
    By Knight-templar in forum Programming
    Replies: 9
    Last Post: 10th September 2002, 09:10 AM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •