Help with DataSets

Hi
can anyone help with this problem, I'm new to using Visual Studio 2003 and I'm trying to get the details from a stored procedure and enter them into numerous text boxes, I tried a few examples from the EE website, but none seem to work.

can anyone give me step by step instructions on how to do this.

Regards
Caz
BeginningWebDesignAsked:
Who is Participating?
 
b1xml2Connect With a Mentor Commented:
looking at your stored proc, it is wrong.

SqlCommand cmd = new SqlCommand("GetEmp",cn);
cmd.CommandType = CommandType.StoredProcedure;
/// first parameter of SqlCommand of the type StoredProcedure
cmd.Parameters.Add("@RETURN_VALUE",SqlDbType.Int,4)
cmd.Parameters[0].Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add("@EmpID",SqlDbType.Int).Value = int.Parse(textBox1.Text);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
textBox2.Text = dr.GetString(0);
textBox3.Text = dr.GetString(1);
dr.Close();
cn.Close();
cmd.Dispose();
0
 
Mohammed NasmanConnect With a Mentor Software DeveloperCommented:
Here's an example

// the store procedure
Create PROCEDURE GetEmp
      (
            @EmpID Int            
      )
AS
      Select FirstName, LastName from Employees
      where EmployeeID = @EmpID


and here's the code to run the StoredProcedure and return the results in textboxes


            {
                  SqlCommand cmd = new SqlCommand("GetEmp",cn);
                  
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.Parameters.Add("@EmpID",SqlDbType.Int).Value = Int32.Parse(textBox1.Text);
                  cn.Open();
                  SqlDataReader dr = cmd.ExecuteReader();
                  dr.Read();
                  textBox2.Text = dr.GetString(0);
                  textBox3.Text = dr.GetString(1);
                  dr.Close();
                  cn.Close();            
            }

Regards,
Mohammed
0
 
b1xml2Commented:
My advice is simply this:

If this is important to you, then it would be wise to invest both your time and money buying at least a book on ASP.NET and ADO.NET.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
BeginningWebDesignAuthor Commented:
Hi b1xml2

I have books on asp.net, but when i try the code it does  not work.

Caz
0
 
b1xml2Commented:
do you have Visual Studio.NET 2002/2003 ??? or any IDE that will help you in writing ASP.NET ??
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hi Caz
Did you try my code?
and which error you got?
0
 
BeginningWebDesignAuthor Commented:
Hi Mohammed
I tried your code and got about 7 errors.
I'm using Visual Studio 2003 and this is what I do.
Drag a DataAdapter onto the page
Drag a dataSet onto the page
Create a repeater region
Bind the dataSet to the repeater
                                                sqlDataAdapter1.Fill(dataSet1);
                  Repeater1.DataSource = dataSet1;
                  Repeater1.DataBind();

<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.strCompany")%>
</ItemTemplate>
</asp:Repeater>

This is following the book to the letter, it throws no errors, but does not display any data either and I'm using a stored procedure.

As above I've even tried just connecting to just a repeater region

I have tried 5 different ways as each book has a different method and still no joy, it proberbly somthing simple but I cannot find it.

Caz
0
 
BeginningWebDesignAuthor Commented:
I have written an insert page which returns the @@Identity from the sp and that works OK, thats why I cannot understand why this will not work
0
 
b1xml2Commented:
well i c you are doing this in c#

so thus,
private void Page_Load(object sender,EventArgs e)
{
  if (! IsPostBack)
  {
    BindRepeaterData();
  }
}

private void BindRepeaterData()
{
    sqlDataAdapter.Fill(dataSet1);
    Repeater1.DataSource = dataSet1;
    Repeater1.DataBind();
}
0
 
BeginningWebDesignAuthor Commented:
Hib1xml2

Still displays no data

caz
0
 
b1xml2Commented:
Where's the DataMember ??

Repeater1.DataSource = dataSet1.Tables[0].DefaultView;
Repeater1.DataBind();


or

Repeater1.DataSource = dataSet1;
Repeater1.DataMember = dataSet1.Tables[0].TableName;
Repeater1.DataBind();


assuming the first table is the datatable you are targetting


0
 
BeginningWebDesignAuthor Commented:
Its a stored prrocedure i'm trying to get the details from
None of the books I have mention a Datamember

Caz
0
 
b1xml2Commented:
what;'s in the DataSet, just point to the first datatable...
0
 
BeginningWebDesignAuthor Commented:
Still does not work, but if I try this it works OK
private void Page_Load(object sender, System.EventArgs e)
            {
                  sqlDataAdapter1.Fill(dataSet1);
                        DataGrid1.DataSource = dataSet1;
                  DataGrid1.DataBind();
                  // Put user code to initialize the page here
            }

But this is data from the table not the sp

Caz
0
 
BeginningWebDesignAuthor Commented:
I've just tried bind to the data straight from the table and bind to the repeater and datagrid and it works OK.

So why does it not work when I try to connect to the stored procedure and the method seems identical???

caz
0
 
BeginningWebDesignAuthor Commented:
I split points as mnasman started the ball rolling and b1xml2 help finish it off
Caz
0
All Courses

From novice to tech pro — start learning today.