Hello all,
what im trying to do is to create a SQL View using SMO library. Here's an example code that im using:
Server srv = Session["Connection"] as Server;
Microsoft.SqlServer.Management.Smo.View NewView = new Microsoft.SqlServer.Management.Smo.View(srv.Databases[cmbDatabases.SelectedItem.Text],txtViewName.Text);
NewView.TextMode = false;
foreach (ListItem item in cblFields.Items)
{
if (item.Selected)
{
Column SelCol = srv.Databases[cmbDatabases.SelectedItem.Text].Tables[cmbTable.SelectedItem.Text].Columns[item.Text];
NewView.Columns.Add(SelCol);
}
}
try
{
NewView.Create();
lblErrorInfo.ForeColor = System.Drawing.Color.Green;
lblErrorInfo.Text = "View \'"+txtViewName.Text+"\' has successfuly been created!";
}
catch (Exception ex)
{
lblErrorInfo.ForeColor = System.Drawing.Color.Red;
lblErrorInfo.Text = ex.Message + " Error Info: "+ex.InnerException.ToString();
}
But when i try to run this code it throws an exception saying:'
Parent property of object [] does not match the collection's parent to which it is added.'
Does anyone have any idea on how to get over this error?
Thanks in advance,
Alexander