Click here to Skip to main content
15,896,489 members
Please Sign up or sign in to vote.
2.20/5 (5 votes)
See more:
hi sir,my question is that if the user click on button(which is placed in default.aspx,for example) then the database table is created in database(database is placed in sql express 2005)how can do ?.I try this task by another method but the following errors are occurred:
1.'system.Web.UI.Page.Server' is a 'property' but is used like a 'type'.
2.The type or namespace name 'Database' could not be found(are you missing a using directive or an assembly reference?)
3.The name 'DataType' does not exist in the current context.
4.'System.Web.UI.WebControls.Table' does not contain a definition for 'columns' and no extension method 'columns' accepting a first argument of type 'System.Web.UI.WebControls.Table' could be found(are you missing a using directive or an assembly reference.
5.'System.Data.Index' is inaccessible due to its protection level.
6.'System.Data.Index' does not contain a constructor that takes '2' arguments.
7.'System.Data.Index' does not contain a definition for 'IndexKeyType' and no extension method 'IndexKeyType' accepting a first argument of type 'System.Data.Index' could be found(are you missing a using directive or an assembly reference?)
8.The name 'IndexKeyType' does not exist in the current context.
9.'System.Data.Index' does not contain a definition for'IndexedColumns' and no extension method 'IndexedColumns' accepting a first argument of type 'System.Data.Index' could be found(are you missing a using directive or assembly reference?)
10.The type or namespace name 'Indexedcolumn' could not be found(are you missing a using directive or an assembly reference?)
11.'System.Web.UI.WebControls.Table' does not contain a definition for 'Indexes' and no extension method 'Indexes' accepting a first argument of type 'System.Web.UI.Webcontrols.Table' could be found(are you missing a using directive or an assembly reference?)
13.'System.Web.UI.WebControls.Table' does not Contain a definition for 'Create' and no extension method 'Create' accepting a first argument of type 'Systen.Web.UI.WebControls.Table' could be found(are you missing a using directive or an assembly reference?)
The code written in c# behind the button is that:

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
//using System.Data.OleDb;
using System.Diagnostics;
using System.ComponentModel;
using System.Text;
using System.Data.SqlClient;
//using System.Data.Odbc;
using Microsoft.SqlServer.Management.Common;
//using ADOX;
//using ADODB;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Establish the database server 
        string connectionString = ConfigurationManager.ConnectionStrings["gameConnectionString"].ConnectionString;
        SqlConnection connection =
             new SqlConnection(connectionString);
        Server server =
             new Server(new ServerConnection(connection));
        // Create table in my personal database 
        Database db = server.Databases["game"];
        // Create new table, called TestTable 
        Table newTable = new Table(db, "TestTable");
        // Add "ID" Column, which will be PK 
        Column idColumn = new Column(newTable, "ID");
        idColumn.DataType = DataType.Int;
        idColumn.Nullable = false;
        idColumn.Identity = true;
        idColumn.IdentitySeed = 1;
        idColumn.IdentityIncrement = 1;
        // Add "Title" Column 
        Column titleColumn = new Column(newTable, "Title");
        titleColumn.DataType = DataType.VarChar(50);
        titleColumn.Nullable = false;
        // Add Columns to Table Object 
        newTable.Columns.Add(idColumn);
        newTable.Columns.Add(titleColumn);
        // Create a PK Index for the table 
        Index index = new Index(newTable, "PK_TestTable");
        index.IndexKeyType = IndexKeyType.DriPrimaryKey;
        // The PK index will consist of 1 column, "ID" 
        index.IndexedColumns.Add(new IndexedColumn(index, "ID"));
        // Add the new index to the table. 
        newTable.Indexes.Add(index);
        // Physically create the table in the database 
        newTable.Create();
    }
}

sir please solve these errors and also give the solution in detail through which i can easily understand.I am very confused in this task please help me.Thank sir

[edit]code wraped in pre tag[/edit]
Posted
Updated 23-Mar-11 18:29pm
v2
Comments
pankajupadhyay29 24-Mar-11 0:31am    
what errors you have given these are not due to any reference these are just due to the way you are writing the code check it yourself and you will resolve it surelly.Try it yourself first and then come with specific errors.

1 solution

You need to reference SMO (Sql Server Management) objects. You can download them here[^].

I think you need to reference the assemblies below :
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.SqlEnum


Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900