程序错误:无效的对象名System.Data.DataRowView [英] Programm Error: Invalid Objectname System.Data.DataRowView
问题描述
我有两个组合框和一个按钮。我想首先从每个组合框中选择并显示Form2中的记录(datagridview)
公开 部分 类 Form1:表格
{
private void button1_Click( object sender, EventArgs e)
{
// 连接字符串
string C = ConfigurationManager.ConnectionStrings [ DB ] .ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
// 撰写查询
Form2 f2 = new Form2();
string qry = ;
string qrynew = 选择 + cbNr.SelectedItem + from + cbTbl.SelectedItem + [DBN Zeichen] ='AB';
if (cbTbl.SelectedItem!= null && cbEIU。 SelectedItem!= null )
{
this .Hide();
f2.copyfun(qrynew);
f2.show();
}
else
{
MessageBox.Show( 错误首先选择一个tbl);
}
}
}
}
Form2:
public partial < span class =code-keyword> class Form2:表格
{
public Standardbericht()
{
InitializeComponent();
}
DataTable dt;
private void Standardbericht_Load(对象发件人,EventArgs e)
{
// lbTblname.Text = cbTbl .Text;
string C = ConfigurationManager.ConnectionStrings [ DB跨度>]的ConnectionString。
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
}
// 从上一页获取/检索记录
private void loadgrid( string qrynew)
{
string C = ConfigurationManager.ConnectionStrings [ < span class =code-string> DB]。ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText =(qrynew);
DataSet d = new DataSet();
// dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(d);
dgvSbericht.DataSource = d;
// dgvSbericht.DataSource = d.Tables [0];
}
public void copyfun( string qrynew)
{
loadgrid(qrynew);
}
}
}
我的查询
string qrynew = SELECT + cbNr .SelectedItem + from + cbTbl.SelectedItem + [DBN Zeichen] ='AB';
如果我在线调试它就是我得到的(cmd.CommandText =(qrynew):
Quote:从System.Data.DataRowView中选择16,其中[DBN Zeichen] =' AB'
cbTbl.SelectedItem是DataRowView,所以你需要得到如下的值
cbTbl.SelectedItem.Row [ ColumnName]。ToString()
以及cbNr.SelectedItem
给你的值为16
可能是你想要cbNr.SelectedItem.Text
或cbNr.SelectedItem.Value
取决于你如何完成数据绑定。
这种连接sql字符串的方式对于sql注入攻击都是开放的。你最好总是尝试使用参数化查询
有很多强度我终于解决了.Bravo
public partial class Form1:表格
{
private < span class =code-keyword> void button1_Click( object sender,EventArgs e)
{
// 连接字符串
字符串 C = ConfigurationManager。 ConnectionStrings [ DB]。ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText =( sp);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ table_name,cbTbl.SelectedValue);
// 撰写查询
Form2 f2 = new Form2();
string qry = ;
string qrynew = cmd.CommandText
if (cbTbl。 SelectedItem!= null && cbNr.SelectedItem!= null )
{
this .Hide();
f2.copyfun(qrynew);
f2.show();
}
else
{
MessageBox.Show( 错误首先选择一个tbl);
}
}
}
}
Hallo i've tried many posibilities but i ain't getting it:
I have two comboboxes and a button. i wanna first choose from each combobox and dispaly the records in Form2 (datagridview)
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
//Connection string
string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
//write a query
Form2 f2 = new Form2();
string qry = "";
string qrynew = " select " + cbNr.SelectedItem + " from " + cbTbl.SelectedItem + " where [DBN Zeichen] = 'AB' ";
if (cbTbl.SelectedItem != null && cbEIU.SelectedItem != null)
{
this.Hide();
f2.copyfun(qrynew);
f2.show();
}
else
{
MessageBox.Show ("ERROR First choose a tbl");
}
}
}
}
Form2:
public partial class Form2 : Form
{
public Standardbericht()
{
InitializeComponent();
}
DataTable dt;
private void Standardbericht_Load(object sender, EventArgs e)
{
//lbTblname.Text = cbTbl.Text;
string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
}
// Get/ retrieve records from the previous page
private void loadgrid(string qrynew)
{
string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = (qrynew);
DataSet d = new DataSet();
//dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(d);
dgvSbericht.DataSource = d;
//dgvSbericht.DataSource = d.Tables[0];
}
public void copyfun(string qrynew)
{
loadgrid(qrynew);
}
}
}
My Query
string qrynew = "SELECT " + cbNr.SelectedItem + " from " + cbTbl.SelectedItem + " where [DBN Zeichen] = 'AB'";
And that's what i get if i debug it on line (cmd.CommandText = (qrynew):
Quote:select 16 from System.Data.DataRowView where [DBN Zeichen] = 'AB'
cbTbl.SelectedItem is DataRowView, so you need to get the value as below
cbTbl.SelectedItem.Row["ColumnName"].ToString()
and alsocbNr.SelectedItem
giving you value as16
may be you wantcbNr.SelectedItem.Text
orcbNr.SelectedItem.Value
depending on how you done the data binding.
any way this way of concatenating sql string is open for sql injection attacks. you better always try to use parameterized query
With a lot of intensity i finally solved it. Bravo
public partial class Form1 : Form { private void button1_Click(object sender, EventArgs e) { //Connection string string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString; SqlConnection con = new SqlConnection(C); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = ("sp"); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@table_name", cbTbl.SelectedValue); //write a query Form2 f2 = new Form2(); string qry = ""; string qrynew = cmd.CommandText if (cbTbl.SelectedItem != null && cbNr.SelectedItem != null) { this.Hide(); f2.copyfun(qrynew); f2.show(); } else { MessageBox.Show ("ERROR First choose a tbl"); } } } }
这篇关于程序错误:无效的对象名System.Data.DataRowView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!