Hi I'm really new to C#, more familiar with .ne but haven't code much in a while.

I am not sure how to populate a table with data after clicking a search button.. know how to do this in .net but eve after looking at examples I'm not sure how to do this in C#

this is what I have in .net but i need to find a way to do it in C# since the whole project will be in C#.
is there another way to use "Dim" ?

protected Sub seachMarks_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchMarks.Click
 
  Dim systemmgr As New SchoolSystem.Controller
        Dim myDS As DataSet
        Try
            myDS = systemmgr.GetMarkSubmissionList(courseid.Text, semester.Text)
            If myDS.Tables(0).Rows.Count = 0 Then
                FormMessage.Text = "No students in this course/semester."
            Else
                myDS.Tables(0).DefaultView.Sort = "Name ASC"
                MarkList.DataSource = myDS.Tables(0)
                MarkList.DataBind()
            End If
        Catch ex As Exception
            FormMessage.Text = ex.Message
        End Try
    End Sub

Dani AI

Generated

The general approach shown in the thread is fine: call your business/controller layer with the search parameters, examine the result, and then bind whatever tabular data you got back to the grid. 's C# conversion is helpful for syntax, but it contains two common runtime pitfalls worth calling out: the controller variable is declared but not instantiated (this will throw a NullReferenceException when you call its method), and the code assumes the returned dataset always contains a table. Always instantiate your controller and defensively verify the returned object is non-null and that Tables.Count > 0 before accessing Tables[0].

Practical hardening steps:

  • Validate and sanitize courseid and semester before sending them to the data layer.
  • Return a single DataTable or a typed list from your data layer if the query always returns one table — it simplifies checks and binding.
  • Prefer parameterized queries or stored procedures in the data layer to avoid SQL injection.
  • If you keep ADO.NET objects, check for null, check Tables.Count, and examine Rows.Count before binding.
  • Make sure the ASPX grid has columns defined or AutoGenerateColumns set, and that the search button is wired to the correct event (and included as a trigger if using an UpdatePanel).

If you want less plumbing, consider declarative data controls (SqlDataSource/ObjectDataSource) or using LINQ/Entity Framework to return IEnumerable<T>; they simplify binding and sorting. For reference on the ADO.NET model and ASP.NET data binding patterns see Microsoft’s docs on DataSet/DataView and Web Forms data binding:

Quick checklist for debugging: set breakpoints, inspect the returned object, confirm the controller is constructed, confirm the event is firing, and watch for exceptions in the output window.

Recommended Answers

All 4 Replies

A few things about your post
1) Use Code tags like this , if you didn't already know.
2) .NET is not a language, the language you are using is VB.
3) If you already know to program on .NET with VB its not too difficult to learn to do it in C#. Here is a starter.

If you know VB.net, then C# become simpler for you to learn. Some of the things you are used to doing in VB using the .Net frame work are still available in c# only the syntaxes are different. I have converted your code to C# for you so that you can compare and learn from it.

protected void seachMarks_Click(object sender , EventArgs e)
	   {

	          SchoolSystem.Controller systemmgr;
	   	 	
	   	DataSet myDS;
	   	
	   	try
	   	{
	   	myDS = systemmgr.GetMarkSubmissionList(courseid.Text, semester.Text);
	   		if(myDS.Tables[0].Rows.Count == 0)
	   		{
	   			FormMessage.Text = "No students in this course/semester.";
	   		}
	   		else
	   		{
	   			myDS.Tables[0].DefaultView.Sort = "Name ASC";
	   			MarkList.DataSource = myDS.Tables(0);
	   		    MarkList.DataBind();
	   		}
	   		
	   		 
	    }

    	     catch (Exception ex)
	   	{
	   		FormMessage.Text = ex.Message;
	   	}
	   }
    }

If you know VB.net, then C# become simpler for you to learn. Some of the things you are used to doing in VB using the .Net frame work are still available in c# only the syntaxes are different. I have converted your code to C# for you so that you can compare and learn from it.

protected void seachMarks_Click(object sender , EventArgs e)
	   {

	          SchoolSystem.Controller systemmgr;
	   	 	
	   	DataSet myDS;
	   	
	   	try
	   	{
	   	myDS = systemmgr.GetMarkSubmissionList(courseid.Text, semester.Text);
	   		if(myDS.Tables[0].Rows.Count == 0)
	   		{
	   			FormMessage.Text = "No students in this course/semester.";
	   		}
	   		else
	   		{
	   			myDS.Tables[0].DefaultView.Sort = "Name ASC";
	   			MarkList.DataSource = myDS.Tables(0);
	   		                MarkList.DataBind();
	   		}
	   		
	   		 
	     }

    	     catch (Exception ex)
	   	{
	   		FormMessage.Text = ex.Message;
	   	}
	   }
    }

you need to fire the select query.....when click search..at that time..

pass your search parameter and bind the datagrid...

i'm sure that will work...

hope it helps...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.