| |

VerySource

 Forgot password?
 Register
Search
View: 819|Reply: 8

Datagrid update data error, c#, vs, enclosed code~~ expert guidance!

[Copy link]

1

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-28 10:00:02
| Show all posts |Read mode
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication1
{
/// <summary>
/// Summary description of WebForm4.
/// </summary>
public class WebForm4: System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid Grid1;
string strConnection;
SqlConnection nwConn;
SqlDataAdapter shipperAdapter;
string queryStr;
DataSet shipperSet=new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
// Place user code here to initialize the page
ConnectData();
//string
queryStr="select * from shippers";
shipperAdapter=new SqlDataAdapter(queryStr,nwConn);
shipperAdapter.Fill(shipperSet, "shippers");
if(!Page.IsPostBack)
{
this.BindGrid();
To
}
To
}
private void ConnectData()
{
string strConnection=System.Configuration.ConfigurationSettings.AppSettings["connstr"];
SqlConnection nwConn=new SqlConnection(strConnection);
To
}
private void BindGrid()
{
To
this.Grid1.DataSource=shipperSet;
Grid1.DataBind();
}
To

#region Web Form Designer Generated Code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form designer.
//
InitializeComponent();
base.OnInit(e);
}
To
/// <summary>
/// The designer supports the required methods-do not use the code editor to modify
/// The content of this method.
/// </summary>
private void InitializeComponent()
{
this.Grid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Grid1_CancelCommand);
this.Grid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Grid1_EditCommand);
this.Grid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Grid1_UpdateCommand);
this.Grid1.Load += new System.EventHandler(this.Page_Load);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Grid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.Grid1.EditItemIndex=e.Item.ItemIndex;
this.BindGrid();
}

private void Grid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int rowCount=e.Item.ItemIndex;
TextBox tbox=(TextBox)e.Item.FindControl("CompanyNameBox1");
shipperSet.Tables["Shippers"].Rows[rowCount]["CompanyName"]=tbox.Text;
tbox=(TextBox)e.Item.FindControl("PhoneBox1");
shipperSet.Tables["Shippers"].Rows[rowCount]["Phone"]=tbox.Text;
this.ConnectData();
SqlCommandBuilder updateBuilder=new SqlCommandBuilder(shipperAdapter);
shipperAdapter.UpdateCommand=updateBuilder.GetUpdateCommand();
shipperAdapter.Update(shipperSet,"shippers");
shipperSet.Clear();
shipperAdapter.Fill(shipperSet,"shippers");
this.BindGrid();
}

private void Grid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.Grid1.EditItemIndex=-1;
this.BindGrid();
}
}
}


//Exception details during debugging: System.InvalidOperationException: Fill: The SelectCommand.Connection property has not been initialized
Reply

Use magic Report

0

Threads

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-9-28 12:30:01
| Show all posts
ConnectData();
//string
queryStr="select * from shippers";
shipperAdapter=new SqlDataAdapter(queryStr,nwConn);
shipperAdapter.Fill(shipperSet, "shippers");

Put these codes in


private void BindGrid()
{

                            Come here
To
this.Grid1.DataSource=shipperSet;
Grid1.DataBind();
}
Reply

Use magic Report

1

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-9-28 13:30:01
| Show all posts
Back to the big brother on the first floor, I tried it, but it still doesn't work, it still runs
Fill: The SelectCommand.Connection property has not been initialized.
Explanation: During the execution of the current web request, an unhandled exception occurred. Please check the stack trace for details about the error and the source of the error in the code.

Exception details: System.InvalidOperationException: Fill: The SelectCommand.Connection property has not been initialized.

Source error:


Line 34: queryStr="select * from shippers";
Line 35: shipperAdapter=new SqlDataAdapter(queryStr,nwConn);
Line 36: shipperAdapter.Fill(shipperSet, "shippers");
Line 37: if(!Page.IsPostBack)
Line 38: {


Source file: c:\inetpub\wwwroot\webapplication1\webform3.aspx.cs Line: 36

Stack trace:


[InvalidOperationException: Fill: The SelectCommand.Connection property has not been initialized. ]
   System.Data.Common.DbDataAdapter.GetConnection(IDbCommand command, String method)
   System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
   WebApplication1._WebForm3.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication1\webform3.aspx.cs:36
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()




-------------------------------------------------- ------------------------------
Version information: Microsoft .NET Framework version: 1.1.4322.573; ASP.NET version: 1.1.4322.573
Reply

Use magic Report

0

Threads

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-9-28 15:00:01
| Show all posts
You didn't do what I told you
Put

ConnectData();
//string
queryStr="select * from shippers";
shipperAdapter=new SqlDataAdapter(queryStr,nwConn);
shipperAdapter.Fill(shipperSet, "shippers");
Put it in that function
Reply

Use magic Report

0

Threads

60

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-9-28 15:30:01
| Show all posts
private void Grid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int rowCount=e.Item.ItemIndex;
TextBox tbox=(TextBox)e.Item.FindControl("CompanyNameBox1");
shipperSet.Tables["Shippers"].Rows[rowCount]["CompanyName"]=tbox.Text;
tbox=(TextBox)e.Item.FindControl("PhoneBox1");
shipperSet.Tables["Shippers"].Rows[rowCount]["Phone"]=tbox.Text;
this.ConnectData();
SqlCommandBuilder updateBuilder=new SqlCommandBuilder(shipperAdapter);
shipperAdapter.UpdateCommand=updateBuilder.GetUpdateCommand();
shipperAdapter.Update(shipperSet,"shippers");
shipperSet.Clear();
shipperAdapter.Fill(shipperSet,"shippers");
this.BindGrid();
}
This update method is not appropriate, generally take the primary key update instead of updating the DATASET
Reply

Use magic Report

1

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Canada

 Author| Post time: 2020-9-28 21:15:01
| Show all posts
private void BindGrid()
{ConnectData();
queryStr="select * from shippers";
shipperAdapter=new SqlDataAdapter(queryStr,nwConn);
shipperAdapter.Fill(shipperSet, "shippers");
this.Grid1.DataSource=shipperSet;
Grid1.DataBind();
}
//RE:3521565brother,
Put it like this?? Still not working~~~



Exception details: System.InvalidOperationException: Fill: The SelectCommand.Connection property has not been initialized.

Source error:


Line 49: queryStr="select * from shippers";
Line 50: shipperAdapter=new SqlDataAdapter(queryStr,nwConn);
Line 51: shipperAdapter.Fill(shipperSet, "shippers");
Line 52: this.Grid1.DataSource=shipperSet;
Line 53: Grid1.DataBind();
Reply

Use magic Report

0

Threads

12

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-9-28 21:30:02
| Show all posts
It's not good for you to update like this!
Generally take the primary key update, that is, execute SQL Update...
Reply

Use magic Report

0

Threads

12

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-9-28 22:00:01
| Show all posts
Exception details: System.InvalidOperationException: Fill: The SelectCommand.Connection property has not been initialized.

=========

I told you that the SelectCommand.Connection property has not been initialized yet
That means no value!

It is recommended to readjust the program structure after clarifying the logical relationship!
Reply

Use magic Report

1

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-9-28 23:45:01
| Show all posts
Sorry, I just learned,
    Is there any way to initialize the SelectCommand.Connection property>?
---It is better to write the code, thank you~!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list