Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hello sir, how are you,sir my question is that if the user click on button( which is place in default.aspx,for example) then the database table is created in database(that is SQL express),how can do that.Please sir help me in that problem.I try that thing but error is occurred during debugging,errors are:

1.the best overloaded method match for 'system.data.odbc.odbc command.odbc command(string,system.data.odbc.odbc connection)'has some invalid arguments.
2.Argument'2':cannot convert from 'system.data.sqlclient.sqlconnection' to 'system.data.odbc.odbc connection'.

The code written in c# behind the button( which is placed in default.aspx,for example) is that
using System;
using System.Collections;
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 PractiseWeb.DataSet1TableAdapters;
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 ADOX;
using ADODB;
public partial class _Default : System.Web.UI.Page 
{
      SqlConnection conn;
      OdbcCommand cmd;
    string connectionString = ConfigurationManager.ConnectionStrings["gameConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try 
{
   conn = new SqlConnection(connectionString);
  if (!(conn.State == ConnectionState.Open)) 
    { 
        conn.Open(); 
    } 
    string sql = "CREATE TABLE mySchoolRecord(StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY,"
    + "Name CHAR(50)," + "Address CHAR(255)," + "Contact INTEGER));"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (1, 'Mr. Manish', " + " 'Sector-12,Noida', 2447658  );";
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (2, 'Mr. Ravi', " + " 'New Delhi', 2584076521   );"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery();
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (3, 'Mr. Peter', " + " 'United States', 25684124  );"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 

    if (conn.State == ConnectionState.Open) 
    { 
        conn.Close(); 
    } 
 
} 
catch (OdbcException ex) 
{ 
   Console.WriteLine(ex); 
} 
    }
}
plz sir email me to that email:[REMOVED]@yahoo.com,if any solution you know or link.plz sir solution or link in detial through which i can easily understand.THANKS SIR
Posted
Updated 20-Mar-11 10:58am
v3
Comments
Wendelius 20-Mar-11 14:26pm    
Pre-tags added. Unless you want a whole lot of spam in your mailbox, don't include your email address in the post. You can edit the question in order to remove the email.

You have to use either SQL Server client library or ODBC. You cannot mix both.

So if you want to use for example SQL Server client, replace the OdbcCommand with SqlCommand etc.
 
Share this answer
 
Comments
fjdiewornncalwe 20-Mar-11 17:39pm    
+5.
Wendelius 21-Mar-11 6:05am    
Thanks :)
khattak1 21-Mar-11 3:22am    
i can do that odbccommand replace by sqlcommand,errors are removed ,there is no error in the code after debugging but the table is not created in the database when the button is clicked.please sir solve my problem?
Wendelius 21-Mar-11 3:26am    
Most likely you have one extra parenthesis in the end. Try replacing:

+ "Contact INTEGER));";
with
+ "Contact INTEGER)";

If that doesn't solve the problem, please include the error message in the post.
hi,

First Create Procedure
Create Procedure M_TestProcedure
(
@Check varchar(10)
)

as

if(@Check='Yes')
begin
CREATE TABLE mySchoolRecord
(StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY,
Name CHAR(50),
[Address] CHAR(255),
Contact int)
INSERT INTO mySchoolRecord (StudentId, Name,[Address],Contact) VALUES (1, 'Mr. Manish', 'Sector-12,Noida', 2447)
INSERT INTO mySchoolRecord (StudentId, Name,[Address],Contact) VALUES (2, 'Mr. Ravi','New Delhi', 2584)
INSERT INTO mySchoolRecord (StudentId, Name,[Address],Contact) VALUES (3, 'Mr. Peter','United States', 256 )
end



then in the button click event write--

C#
string Check="Yes";
            try
            {
                Con = new SqlConnection("Data Source=10.20.30.101;Initial Catalog=train;User ID=Train;Password=pass123!@#");
                Con.Open();
                Cmd = new SqlCommand("M_TestProcedure", Con);
                Cmd.Parameters.AddWithValue("@Check", Check);
                Cmd.CommandType = CommandType.StoredProcedure;
                int k = Cmd.ExecuteNonQuery();
                Console.WriteLine(k);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                Con.Close();
            }
 
Share this answer
 
Comments
khattak1 24-Mar-11 2:00am    
sir please write the above code in c# from "first create procedure" to "end".sir also mention that that code write in which area(button-click event or pageload()).

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