473,287 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

2 DATA GRID Questions

Hi,

I need to display columns in a data grid based on 7 different queries. Now I
have 32 questions:

1. Is it possble to have 1 single data adapter with 7 queries & 1 data set
or do I need to have a separate data adapter & a separate data set for each
select query?
If thats possible then how?

2. I have the following code which allows the user to enter an order number
& click the view button which should show all information for that
order...But the data gris is not showing the infomation, it is showing the
only the column headings... I am using only 1 data grid for all the
queries... Everytime a different view link (related to a different criteria)
is clicked , I change the DataSource , DataKeyField & DataMember for the grid
which points to a separate data set.

Code ::

private void LinkButton1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsAllAO1);
DataGrid1.DataBind();
}
private void lnkByOrder_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@o"].Value=txtOrder.Text;

// Different data set
sqlDataAdapter2.Fill(dsByOrder1); DataGrid1.DataSource=
dsByOrder1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByName_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;

// Different data set
sqlDataAdapter2.Fill(dsByName1); DataGrid1.DataSource=
dsByName1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

Is smthg wrong with this code? Will the data grid not work if I bound it to
a different data set on different link button clicks?

Thanks

--
pmud
Jul 21 '05 #1
5 2458
pmud wrote:
Hi,

I need to display columns in a data grid based on 7 different queries. Now I
have 32 questions:

1. Is it possble to have 1 single data adapter with 7 queries & 1 data set
or do I need to have a separate data adapter & a separate data set for each
select query?
If thats possible then how?
Yes, you can have 1 single adapter with 7 different queries and 1 data
set. It all depends on how your stored procedure is written. If there
are 7 select statements or unions or whatever, you will get back 7
tables in one stored procedure call that you can then populate into a
data set using the data adapter.

2. I have the following code which allows the user to enter an order number
& click the view button which should show all information for that
order...But the data gris is not showing the infomation, it is showing the
only the column headings... I am using only 1 data grid for all the
queries... Everytime a different view link (related to a different criteria)
is clicked , I change the DataSource , DataKeyField & DataMember for the grid
which points to a separate data set.

Code ::

private void LinkButton1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsAllAO1);
DataGrid1.DataBind();
}
private void lnkByOrder_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@o"].Value=txtOrder.Text;

// Different data set
sqlDataAdapter2.Fill(dsByOrder1); DataGrid1.DataSource=
dsByOrder1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByName_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;

// Different data set
sqlDataAdapter2.Fill(dsByName1); DataGrid1.DataSource=
dsByName1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

Is smthg wrong with this code? Will the data grid not work if I bound it to
a different data set on different link button clicks?
Your code above should run fine, but here is something a little leaner
and easier to debug:

<code>
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, connection);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
sda.SelectCommand.Paramaters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}
</code>

Then, elsewhere in your code, all you would have to do to populate your
datagrid is to just call

private void lnkByName_Click(object sender, System.EventArgs e)
{
// sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;
// // Different data set
// sqlDataAdapter2.Fill(dsByName1);
// DataGrid1.DataSource= dsByName1;
// DataGrid1.DataMember="INVOICES";
// DataGrid1.DataKeyField= "ORDER_NO";
// DataGrid1.AutoGenerateColumns=true;
// DataGrid1.DataBind();

//Replace above with:
BindData("<stored proc name>", "@n", txtName.Text);
}

Thanks


hth,
~d
Jul 21 '05 #2
Hi D.W. Warlock

I have never written stored procedures. but just now I did a tutorial on
them. So I should write a stored procedure with 7 select statements . Then I
use this stored procedure with only one data adapter & 1 data set. & when
the user clicks the view button , the data grid will show the result? Is that
right?

"D.W. Warlock" wrote:
pmud wrote:
Hi,

I need to display columns in a data grid based on 7 different queries. Now I
have 32 questions:

1. Is it possble to have 1 single data adapter with 7 queries & 1 data set
or do I need to have a separate data adapter & a separate data set for each
select query?
If thats possible then how?


Yes, you can have 1 single adapter with 7 different queries and 1 data
set. It all depends on how your stored procedure is written. If there
are 7 select statements or unions or whatever, you will get back 7
tables in one stored procedure call that you can then populate into a
data set using the data adapter.

2. I have the following code which allows the user to enter an order number
& click the view button which should show all information for that
order...But the data gris is not showing the infomation, it is showing the
only the column headings... I am using only 1 data grid for all the
queries... Everytime a different view link (related to a different criteria)
is clicked , I change the DataSource , DataKeyField & DataMember for the grid
which points to a separate data set.

Code ::

private void LinkButton1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsAllAO1);
DataGrid1.DataBind();
}
private void lnkByOrder_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@o"].Value=txtOrder.Text;

// Different data set
sqlDataAdapter2.Fill(dsByOrder1); DataGrid1.DataSource=
dsByOrder1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByName_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;

// Different data set
sqlDataAdapter2.Fill(dsByName1); DataGrid1.DataSource=
dsByName1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

Is smthg wrong with this code? Will the data grid not work if I bound it to
a different data set on different link button clicks?


Your code above should run fine, but here is something a little leaner
and easier to debug:

<code>
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, connection);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
sda.SelectCommand.Paramaters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}
</code>

Then, elsewhere in your code, all you would have to do to populate your
datagrid is to just call

private void lnkByName_Click(object sender, System.EventArgs e)
{
// sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;
// // Different data set
// sqlDataAdapter2.Fill(dsByName1);
// DataGrid1.DataSource= dsByName1;
// DataGrid1.DataMember="INVOICES";
// DataGrid1.DataKeyField= "ORDER_NO";
// DataGrid1.AutoGenerateColumns=true;
// DataGrid1.DataBind();

//Replace above with:
BindData("<stored proc name>", "@n", txtName.Text);
}

Thanks


hth,
~d

Jul 21 '05 #3
Hi D.W. Warlock,

I created a stored procedure which works fine when i run it from the server
explorer.

Stored Procedure Code::
CREATE PROCEDURE dbo.usp_Order (@OrderNo int) AS
select * from IRUS_INVOICES where ORDER_NO = @OrderNo
GO

& then I used the followng code...
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, sqlConnection1);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
//sda.SelectCommand.Paramaters.Add(paramName, paramValue);
sda.SelectCommand.Parameters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByOrder_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_Order", "@OrderNo",
int.Parse(txtOrder.Text));

}

But when I click the view link , the data grid doesnt show up the records
.... What is wrong with the code?

"D.W. Warlock" wrote:
pmud wrote:
Hi,

I need to display columns in a data grid based on 7 different queries. Now I
have 32 questions:

1. Is it possble to have 1 single data adapter with 7 queries & 1 data set
or do I need to have a separate data adapter & a separate data set for each
select query?
If thats possible then how?


Yes, you can have 1 single adapter with 7 different queries and 1 data
set. It all depends on how your stored procedure is written. If there
are 7 select statements or unions or whatever, you will get back 7
tables in one stored procedure call that you can then populate into a
data set using the data adapter.

2. I have the following code which allows the user to enter an order number
& click the view button which should show all information for that
order...But the data gris is not showing the infomation, it is showing the
only the column headings... I am using only 1 data grid for all the
queries... Everytime a different view link (related to a different criteria)
is clicked , I change the DataSource , DataKeyField & DataMember for the grid
which points to a separate data set.

Code ::

private void LinkButton1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsAllAO1);
DataGrid1.DataBind();
}
private void lnkByOrder_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@o"].Value=txtOrder.Text;

// Different data set
sqlDataAdapter2.Fill(dsByOrder1); DataGrid1.DataSource=
dsByOrder1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByName_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;

// Different data set
sqlDataAdapter2.Fill(dsByName1); DataGrid1.DataSource=
dsByName1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

Is smthg wrong with this code? Will the data grid not work if I bound it to
a different data set on different link button clicks?


Your code above should run fine, but here is something a little leaner
and easier to debug:

<code>
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, connection);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
sda.SelectCommand.Paramaters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}
</code>

Then, elsewhere in your code, all you would have to do to populate your
datagrid is to just call

private void lnkByName_Click(object sender, System.EventArgs e)
{
// sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;
// // Different data set
// sqlDataAdapter2.Fill(dsByName1);
// DataGrid1.DataSource= dsByName1;
// DataGrid1.DataMember="INVOICES";
// DataGrid1.DataKeyField= "ORDER_NO";
// DataGrid1.AutoGenerateColumns=true;
// DataGrid1.DataBind();

//Replace above with:
BindData("<stored proc name>", "@n", txtName.Text);
}

Thanks


hth,
~d

Jul 21 '05 #4
Hi pmud,

Sorry about the delay in getting back to you, I was out sick.

Anyhow, when you call "BindData("dob.usp_Order", "@OrderNo",
int.Parse(txtOrder.Text));" drop the dbo. in the stored procedure name.
For some reason that I have yet to find out, SqlServer/MSDE has issues
when using owner.spname conventions.

HTH,
~d

pmud wrote:
Hi D.W. Warlock,

I created a stored procedure which works fine when i run it from the server
explorer.

Stored Procedure Code::
CREATE PROCEDURE dbo.usp_Order (@OrderNo int) AS
select * from IRUS_INVOICES where ORDER_NO = @OrderNo
GO

& then I used the followng code...
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, sqlConnection1);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
//sda.SelectCommand.Paramaters.Add(paramName, paramValue);
sda.SelectCommand.Parameters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByOrder_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_Order", "@OrderNo",
int.Parse(txtOrder.Text));

}

But when I click the view link , the data grid doesnt show up the records
... What is wrong with the code?

"D.W. Warlock" wrote:

pmud wrote:
Hi,

I need to display columns in a data grid based on 7 different queries. Now I
have 32 questions:

1. Is it possble to have 1 single data adapter with 7 queries & 1 data set
or do I need to have a separate data adapter & a separate data set for each
select query?
If thats possible then how?


Yes, you can have 1 single adapter with 7 different queries and 1 data
set. It all depends on how your stored procedure is written. If there
are 7 select statements or unions or whatever, you will get back 7
tables in one stored procedure call that you can then populate into a
data set using the data adapter.

2. I have the following code which allows the user to enter an order number
& click the view button which should show all information for that
order...But the data gris is not showing the infomation, it is showing the
only the column headings... I am using only 1 data grid for all the
queries... Everytime a different view link (related to a different criteria)
is clicked , I change the DataSource , DataKeyField & DataMember for the grid
which points to a separate data set.

Code ::

private void LinkButton1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsAllAO1);
DataGrid1.DataBind();
}
private void lnkByOrder_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@o"].Value=txtOrder.Text;

// Different data set
sqlDataAdapter2.Fill(dsByOrder1); DataGrid1.DataSource=
dsByOrder1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByName_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;

// Different data set
sqlDataAdapter2.Fill(dsByName1); DataGrid1.DataSource=
dsByName1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

Is smthg wrong with this code? Will the data grid not work if I bound it to
a different data set on different link button clicks?


Your code above should run fine, but here is something a little leaner
and easier to debug:

<code>
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, connection);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
sda.SelectCommand.Paramaters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}
</code>

Then, elsewhere in your code, all you would have to do to populate your
datagrid is to just call

private void lnkByName_Click(object sender, System.EventArgs e)
{
// sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;
// // Different data set
// sqlDataAdapter2.Fill(dsByName1);
// DataGrid1.DataSource= dsByName1;
// DataGrid1.DataMember="INVOICES";
// DataGrid1.DataKeyField= "ORDER_NO";
// DataGrid1.AutoGenerateColumns=true;
// DataGrid1.DataBind();

//Replace above with:
BindData("<stored proc name>", "@n", txtName.Text);
}

Thanks


hth,
~d

Jul 21 '05 #5
Hi ,

I hope you are well now.Thanks for the answer ... Is there a way to sort the
data grid using the following code...coz here i have not used a data set but
a data table for binding to the data grid...I searched many articles but all
were for binding to the dat set using a data vierw only & more over I am
BINDING THE GRID on DIFFERENT LINK BUTTON CLICKS to ONE DATA TABLE WHICH
CONTAINS DIFFERENT VALUES BASED ON DIFFERENT STORED PROCEDURES....

MY code :

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, sqlConnection1);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{

sda.SelectCommand.Parameters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}
private void BindData(string sqlQuery, string paramName, object
paramValue,string paramName1, object paramValue1)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, sqlConnection1);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null && paramName1 != null &&
paramValue1 != null)
{

sda.SelectCommand.Parameters.Add(paramName, paramValue);
sda.SelectCommand.Parameters.Add(paramName1,paramV alue1);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void LinkButton1_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_MasterBrowser_ViewAll",null,null );
}

private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEvent Args e)
{

}

private void lnkByOrder_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_Order", "@OrderNo", int.Parse(txtOrder.Text));

}

private void lnkByDate_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_MasterBrowser_Date","@StartDate" ,
System.DateTime.Parse(txtStartDate.Text),"@EndDate ",System.DateTime.Parse(txtEndDate.Text));
}

private void lnkByStatus_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_MasterBrowser_Status","@Status", ddlStatus.SelectedValue);
}
How can sorting be done in this grid? Is it even possible?

"D0tN3t C0d3r" wrote:
Hi pmud,

Sorry about the delay in getting back to you, I was out sick.

Anyhow, when you call "BindData("dob.usp_Order", "@OrderNo",
int.Parse(txtOrder.Text));" drop the dbo. in the stored procedure name.
For some reason that I have yet to find out, SqlServer/MSDE has issues
when using owner.spname conventions.

HTH,
~d

pmud wrote:
Hi D.W. Warlock,

I created a stored procedure which works fine when i run it from the server
explorer.

Stored Procedure Code::
CREATE PROCEDURE dbo.usp_Order (@OrderNo int) AS
select * from IRUS_INVOICES where ORDER_NO = @OrderNo
GO

& then I used the followng code...
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, sqlConnection1);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
//sda.SelectCommand.Paramaters.Add(paramName, paramValue);
sda.SelectCommand.Parameters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByOrder_Click(object sender, System.EventArgs e)
{
BindData("dbo.usp_Order", "@OrderNo",
int.Parse(txtOrder.Text));

}

But when I click the view link , the data grid doesnt show up the records
... What is wrong with the code?

"D.W. Warlock" wrote:

pmud wrote:

Hi,

I need to display columns in a data grid based on 7 different queries. Now I
have 32 questions:

1. Is it possble to have 1 single data adapter with 7 queries & 1 data set
or do I need to have a separate data adapter & a separate data set for each
select query?
If thats possible then how?

Yes, you can have 1 single adapter with 7 different queries and 1 data
set. It all depends on how your stored procedure is written. If there
are 7 select statements or unions or whatever, you will get back 7
tables in one stored procedure call that you can then populate into a
data set using the data adapter.
2. I have the following code which allows the user to enter an order number
& click the view button which should show all information for that
order...But the data gris is not showing the infomation, it is showing the
only the column headings... I am using only 1 data grid for all the
queries... Everytime a different view link (related to a different criteria)
is clicked , I change the DataSource , DataKeyField & DataMember for the grid
which points to a separate data set.

Code ::

private void LinkButton1_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsAllAO1);
DataGrid1.DataBind();
}
private void lnkByOrder_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@o"].Value=txtOrder.Text;

// Different data set
sqlDataAdapter2.Fill(dsByOrder1); DataGrid1.DataSource=
dsByOrder1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

private void lnkByName_Click(object sender, System.EventArgs e)
{
sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;

// Different data set
sqlDataAdapter2.Fill(dsByName1); DataGrid1.DataSource=
dsByName1;
DataGrid1.DataMember="INVOICES";
DataGrid1.DataKeyField= "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}

Is smthg wrong with this code? Will the data grid not work if I bound it to
a different data set on different link button clicks?

Your code above should run fine, but here is something a little leaner
and easier to debug:

<code>
private void BindData(string sqlQuery, string paramName, object paramValue)
{
DataTable queryTable = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, connection);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
if(paramName != null && paramValue != null)
{
sda.SelectCommand.Paramaters.Add(paramName, paramValue);
}
sda.Fill(queryTable);
DataGrid1.DataSource = queryTable;
DataGrid1.DataKeyField = "ORDER_NO";
DataGrid1.AutoGenerateColumns=true;
DataGrid1.DataBind();
}
</code>

Then, elsewhere in your code, all you would have to do to populate your
datagrid is to just call

private void lnkByName_Click(object sender, System.EventArgs e)
{
// sqlSelectCommand2.Parameters["@n"].Value=txtName.Text;
// // Different data set
// sqlDataAdapter2.Fill(dsByName1);
// DataGrid1.DataSource= dsByName1;
// DataGrid1.DataMember="INVOICES";
// DataGrid1.DataKeyField= "ORDER_NO";
// DataGrid1.AutoGenerateColumns=true;
// DataGrid1.DataBind();

//Replace above with:
BindData("<stored proc name>", "@n", txtName.Text);
}
Thanks
hth,
~d

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jordan O'Hare | last post by:
Hello Everyone, I am after some help with the following: I have a windows application that contains a list box and two data grids. All three controls are binded to a dataset that contains...
1
by: Eagle | last post by:
I have a form that has one record with 22 fields. A grid is not the answer, either several small tables or combinations of textboxes and labels, so the user can edit all the editable fields then...
1
by: Jim | last post by:
I have a datagrid on a windows form that is filled using a data adapter and dataset created in code. I have mapped columns to the various fields. The user enters new infromation into the grid and...
5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
6
by: Mike Wilson | last post by:
Dear Group, I have a heirarchical set of database tables, say - "order" and "order_type" and want to display a series of orders in a grid control, and in place of the order_type foreign key...
3
by: Scottie_do | last post by:
I have a 20meg in-memory stack-based array and I'd like to normalise in the client's memory... then add foriegn keys, and display results on a datagrid. I discovered that converting the stack to a...
5
by: Kent Boogaart | last post by:
Hi, I have some hierarchical data (FAQs) that I would like to bind to. The basic structure is: FAQ Category + Categories + FAQs So an FAQ category has any number of sub-categories and any...
5
by: Brian P. Hammer | last post by:
I have data from multiple SQL tables, some of the tables will only have one row, while others multiple rows. I load a bunch of data from the various tables and add them to a third party grid. With...
6
by: =?Utf-8?B?bXIgcGVhbnV0?= | last post by:
How do I turn on the visual grid in the web (asp) design environment. I know this is stupid but it's counterintuitive to me.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.