使用DataSet时遇到的问题

发布于 2024-11-08 14:49:45 字数 1688 浏览 0 评论 0原文

我首先在项目名称 recvd.xsd 中手动创建了一个具有相应 recvd.xssDataSet。在我的按钮单击事件中,我完成了以下编码。

try 
{
    DataSet recvd_REPORT = new DataSet();
    DataTable REPORT = new DataTable();
    String dd_webCofig = ConfigurationManager.ConnectionStrings["server2"].ConnectionString;
    OdbcConnection ddlistconn = new OdbcConnection(dd_webCofig);
    ddlistconn.Open();

    REPORT = recvd_REPORT.Tables["REPORT"];
    DataColumn myDataColumn = new DataColumn();
    myDataColumn.DataType = typeof(System.Int32);
    myDataColumn.ColumnName = "RECEIVED";
    myDataColumn.ReadOnly = false;
    myDataColumn.Unique = false;
    // Add the Column to the DataColumnCollection.
    REPORT.Columns.Add(myDataColumn);

    string query = "SELECT case_no as \"RECEIVED\" from dcpanaji.Civil_t where dt_regis > '" + txtStartDate.Text + "' AND dt_regis < '" + txtEndDate.Text + "' AND court_no = " + DropDownList1.SelectedItem + "";
    Response.Write(query);
    OdbcCommand cmd = new OdbcCommand(query, ddlistconn);
    OdbcDataReader loginMyReader = cmd.ExecuteReader();

    OdbcDataAdapter adptr = new OdbcDataAdapter(query, ddlistconn);
    adptr.Fill(REPORT);

    ddlistconn.Close();   
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

我收到的错误是

未将对象引用设置为对象的实例。

如果我从 SQL 查询中删除 as \"RECEIVED\" 并简单地执行 SQL 查询,则查询结果如下(根据用户输入而变化)

200200000452011 ……,n个12位数字。

请帮助我删除关于为什么我无法绑定到数据表的错误。

我在执行 Response.Write(query); 之前收到错误,为什么会出现这个问题?

我删除了 try catch 块,现在收到错误

在此处输入图像描述

I have first manually created a DataSet in my project name recvd.xsd having corresponding recvd.xss. On my button click event i have done the following coding.

try 
{
    DataSet recvd_REPORT = new DataSet();
    DataTable REPORT = new DataTable();
    String dd_webCofig = ConfigurationManager.ConnectionStrings["server2"].ConnectionString;
    OdbcConnection ddlistconn = new OdbcConnection(dd_webCofig);
    ddlistconn.Open();

    REPORT = recvd_REPORT.Tables["REPORT"];
    DataColumn myDataColumn = new DataColumn();
    myDataColumn.DataType = typeof(System.Int32);
    myDataColumn.ColumnName = "RECEIVED";
    myDataColumn.ReadOnly = false;
    myDataColumn.Unique = false;
    // Add the Column to the DataColumnCollection.
    REPORT.Columns.Add(myDataColumn);

    string query = "SELECT case_no as \"RECEIVED\" from dcpanaji.Civil_t where dt_regis > '" + txtStartDate.Text + "' AND dt_regis < '" + txtEndDate.Text + "' AND court_no = " + DropDownList1.SelectedItem + "";
    Response.Write(query);
    OdbcCommand cmd = new OdbcCommand(query, ddlistconn);
    OdbcDataReader loginMyReader = cmd.ExecuteReader();

    OdbcDataAdapter adptr = new OdbcDataAdapter(query, ddlistconn);
    adptr.Fill(REPORT);

    ddlistconn.Close();   
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}

I am getting the error as

Object reference not set to an instance of an object.

If i remove as \"RECEIVED\" from my SQL query and simply execute my SQL query than the result of my query is as follows (varies depending on user input)

200200000452011 ......, n numbers of 12 digit number.

Please help me to remove the error as to why am i not able to bind to DataTable.

I am getting the error before Response.Write(query); is executed, why is that problem?

I removed the try catch block and now i get the error as

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箹锭⒈辈孓 2024-11-15 14:49:45

尝试

SELECT case_no as `RECEIVED` ...

或者只是

SELECT case_no as RECEIVED ...

无论如何,正确的数据集填充代码应该如下所示:

DataSet ds = new DataSet();
using (OdbcConnection connection = new OdbcConnection(connectionString))
using (OdbcCommand command = new OdbcCommand(sqlQuery, connection)
using (OdbcDataAdapter adapter = new OdbcDataAdapter(command)
{
    adapter.Fill(ds);
}

try

SELECT case_no as `RECEIVED` ...

or just

SELECT case_no as RECEIVED ...

Anyway the correct DataSet filling code should look like this:

DataSet ds = new DataSet();
using (OdbcConnection connection = new OdbcConnection(connectionString))
using (OdbcCommand command = new OdbcCommand(sqlQuery, connection)
using (OdbcDataAdapter adapter = new OdbcDataAdapter(command)
{
    adapter.Fill(ds);
}
ˉ厌 2024-11-15 14:49:45

在你的 SQL 中,而不是 \"RECIEVED\" 你是否尝试过只 RECIEVED,没有引号?

In your SQL instead of \"RECIEVED\" have you tried just RECIEVED, no quotes?

烟燃烟灭 2024-11-15 14:49:45

您不需要在查询中使用“RECEIVED”,您可以像下面这样使用。

query = "SELECT case_no as [RECIEVED] from dcpanaji.Civil_t where dt_regis > '" + txtStartDate.Text + "' AND dt_regis < '" + txtEndDate.Text + "' AND court_no = " + DropDownList1.SelectedItem + "";

You need not to use \"RECEIVED\" in the query, you can use like below.

query = "SELECT case_no as [RECIEVED] from dcpanaji.Civil_t where dt_regis > '" + txtStartDate.Text + "' AND dt_regis < '" + txtEndDate.Text + "' AND court_no = " + DropDownList1.SelectedItem + "";
好倦 2024-11-15 14:49:45

你可以像检查一样编写查询

query = "SELECT case_no as RECIEVED from dcpanaji.Civil_t where dt_regis > '" + txtStartDate.Text + "' AND dt_regis < '" + txtEndDate.Text + "' AND court_no = " + DropDownList1.SelectedItem + "";

......

you can write query as like

query = "SELECT case_no as RECIEVED from dcpanaji.Civil_t where dt_regis > '" + txtStartDate.Text + "' AND dt_regis < '" + txtEndDate.Text + "' AND court_no = " + DropDownList1.SelectedItem + "";

check like that....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文