المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : NullReferenceException object reference not set to an instance of an object..please h



C# Programming
05-08-2009, 04:30 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.AnalysisServices;
using Microsoft.DataWarehouse.Interfaces;
using System.Data.OleDb;
using System.Data.Common;



namespace DatabaseApplication
{
public partial class Form1 : Form
{
string dbname;
string connectionstring;
string databasename;
string datasourceviewname;
string datasourcename;
string tableName;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string connstring = "Data Source=localhost;Integrated Security=True";

SqlConnection connection = new SqlConnection(connstring);

string query = "select * from sys.databases";

SqlDataAdapter data = new SqlDataAdapter(query, connection);

DataTable dtable = new DataTable();

try
{
connection.Open();
int records = data.Fill(dtable);
if (records > 0)
{
foreach (DataRow dr in dtable.Rows)
{
//Console.WriteLine(dr[0]);
comboBox1.Items.Add(dr[0]);
}
}
}
catch (SqlException sqlexcp)
{
MessageBox.Show("connection error");
}


}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dbname = comboBox1.Text;
}

private void button1_Click(object sender, EventArgs e)
{
/*databasename = dbname;
datasourceviewname = "oor";
connectionstring = "Data Source=localhost;Integrated Security=True";

//create a datasource name
RelationalDataSource newDS = new RelationalDataSource(datasourceviewname, Utils.GetSyntacticallyValidID(datasourceviewname, typeof(RelationalDataSource)));

Database db = new Database("oor");

db.DataSources.Add(newDS);

newDS.ConnectionString = connectionstring;

newDS.Update();*/


Server server = new Server();

databasename = "cwh_temp";

server.Connect("localhost"); // connection to the MS Analysis service engine

Database db = new Database(databasename, Utils.GetSyntacticallyValidID(databasename, typeof(Database)));


if (server.Databases.ContainsName(databasename))
server.Databases.RemoveAt(server.Databases.IndexOfName(databasename));

server.Databases.Add(db);

db.Update();

connectionstring = "Provider=SQLNCLI.1;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=rmcwh_alevoor";
datasourcename = "ooru";

RelationalDataSource dsNew = new RelationalDataSource(datasourcename, Utils.GetSyntacticallyValidID(datasourcename, typeof(RelationalDataSource)));

db.DataSources.Add(dsNew);

dsNew.ConnectionString = connectionstring;

dsNew.Update();

datasourceviewname = "ooru";

DataSourceView dsv;

dsv = db.DataSourceViews.Add(datasourceviewname, Utils.GetSyntacticallyValidID(datasourceviewname, typeof(DataSourceView)));

dsv.DataSourceID = dsNew.ID;

OleDbConnection cn = new OleDbConnection(dsNew.ConnectionString);

tableName = "UdayPareekScale";

//dss.Tables[1].TableName = "Folder";
try
{

OleDbCommand cmd = new OleDbCommand("Select * from [" + tableName + "]", cn);

OleDbDataAdapter ad = new OleDbDataAdapter(cmd);

DataSet dss = new DataSet();

ad.FillSchema(dss, SchemaType.Source);

dss.Tables[0].TableName = tableName;

dsv.Schema.Tables.Add(dss.Tables[tableName].Clone());
}
catch (NullReferenceException nre)
{
MessageBox.Show(nre.ToString());
}
catch(Exception exp)
{
MessageBox.Show(exp.ToString());
}

//dsv.Schema.Tables.Add(dss.Tables["Folder"].Clone());

dsv.Update();
}
}
}