Trang 1 trên 1

Sửa lỗi C#

Đã gửi: T.Hai 13/11/2017 11:25 pm
gửi bởi vbvn
namespace dbf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void LENH(string chuoi, string sql)
{
try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
MyConnection = new System.Data.OleDb.OleDbConnection(chuoi);
MyConnection.Open();
myCommand.Connection = MyConnection;
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();

}
catch
{

}
}

private void btnOpenFileexcel_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
String ChuoiKetNoi = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source = " + openFileDialog1.FileName + ";" +
"Extended Properties = Excel 8.0;";
OleDbConnection DoiTuongKetNoi = new OleDbConnection(ChuoiKetNoi);
DoiTuongKetNoi.Open();
OleDbCommand CauLanhSql = new OleDbCommand("SELECT SoThua, Dientich, DT_GCN, Hoten, '' as Lrd, Diachi, LoaiDat FROM [mau$] ", DoiTuongKetNoi);
OleDbDataAdapter BoChuyenDuLieu = new OleDbDataAdapter();
BoChuyenDuLieu.SelectCommand = CauLanhSql;
System.Data.DataTable Bang1 = new System.Data.DataTable();
BoChuyenDuLieu.Fill(Bang1);
dataGridView2.DataSource = Bang1;
DoiTuongKetNoi.Close();
DoiTuongKetNoi.Dispose();
}
catch
{

}
}
}

private void btnConvert_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.Filter = "DBF Files(*.dbf)|*.dbf|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
string fn = fdlg.FileName;
string chuoidbf = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.IO.Path.GetFullPath(fn).Replace(System.IO.Path.GetFileName(fn), "") + ";Extended Properties=dBASE IV;User ID=Admin;Password=;";
OleDbConnection conn = new OleDbConnection(chuoidbf);
conn.Open();

string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(fn) + "] ";
OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
for (int i2 = 1; i2 < dataGridView2.RowCount; i2++)
{
for (int i1 = 1; i1 < dataGridView1.RowCount; i1++)
{
string @SHThua1 = dataGridView1.Rows[i1].Cells["PARCELID"].Value.ToString();
string @SHThua2 = dataGridView2.Rows[i2].Cells["SoThua"].Value.ToString();
string @Hoten = dataGridView2.Rows[i1].Cells["Hoten"].Value.ToString();
string @Diachi = dataGridView2.Rows[i1].Cells["Diachi"].Value.ToString();

string sql = "UPDATE [" + System.IO.Path.GetFileName(fn) + "] set USERNAME = '" + @Hoten + "', ADDRESS = '" + @Diachi + "'WHERE @SHThua1 = @SHThua2 ";
LENH(chuoidbf, sql);
}
}
conn.Close();
}
}

}
}
lỗi xảy ra
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at dbf.Form1.btnConvert_Click(Object sender, EventArgs e) in C:\Users\ADMIN\Documents\Visual Studio 2010\Projects\dbf\dbf\Form1.cs:line 96
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
dbf
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/ADMIN/Documents/Visual%20Studio%202010/Projects/dbf/dbf/bin/Debug/dbf.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Transactions
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.