I have a three textbox in my form and a button
one to table name ( Example .Emp details)
two to get field name(Example ID)
three to get field value(Example 10)
i need a code to pass values in run time and all should be stored in SQL
i dont know code but plz Help me.. im getting Error..
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;User Id=sa;Password=sa");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM "+ TextBox1.Text +"",con);
SqlCommand CMD = new SqlCommand("Create table "+ TextBox1.Text +"("+ TextBox2.Text +" )values( '"+ TextBox3.Text +"')",con);
DataSet ds = new DataSet();
da.Fill(ds,"+ TextBox1.Text +");
DataGrid1.DataSource=ds;
DataGrid1.DataMember="+ TextBox1.Text +";
DataGrid1.DataBind();
con.Open();
CMD.ExecuteNonQuery();
con.Close();
}
Please dear friend Help me soon
If any doubt mail me at [email protected]
Thank u
senthil kumar.
Loading
Gautam BaruahPosted Nov 15, 2009, 2:51 PM
protected void btnSubmit_Click(object sender, EventArgs e)
{
string sql = "CREATE TABLE [dbo].["+textbox1.Text+"](["+textbox2.Text+"] [nvarchar](25) NOT NULL)";
SqlConnection con=new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;User Id=sa;Password=sa");
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader;
try
{
con.Open();
cmd.ExecuteNonQuery();
sql = "insert into "+textbox1.Text+"("+textbox2.Text+") values("+textbox3.Text+")";
cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
sql = "select * from " + textbox1.Text;
cmd = new SqlCommand(sql, con);
reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
DataGrid1.DataSource = dt;
DataGrid1.DataBind();
}
catch
{
throw;
}
finally
{
con.Close();
}
}
Please let me know if this solve your problem.
Gautam BaruahPosted Nov 16, 2009, 3:05 PM
senthilPosted Nov 16, 2009, 12:24 PM
THANKS A LOT FOR ALL REPLYS.. KEEP SMILING....
HAPPY CODING
SENTHIL KUMAR
Gautam BaruahPosted Nov 16, 2009, 12:38 AM
senthilPosted Nov 15, 2009, 8:57 PM
contact at [email protected]
senthilPosted Nov 15, 2009, 8:54 PM
Example
Table name(label) - Textbox(for getting value for table at runtime such as Employee details )
column name(label) - textbox (for getting a colum name for table such as ID)
Column value(label) -textbox( for getting a colum value for the colum name such as 10)
Button
if i click a button at run time
the first textbox value should create a table
the second text box should create a column for table
the third to insert column name for table
example in sql we create (This will be creted in sql)
CREATE TABLE Employeedetails(ID Int)
INSERT INTO TABLE(ID)
VALUES(10)
I needed this to create the above in run time ....
I will very thankful if i finish this concept... Keep smiling Thanks for replys....
senthilPosted Nov 15, 2009, 8:53 PM
Example
Table name(label) - Textbox(for getting value for table at runtime such as Employee details )
column name(label) - textbox (for getting a colum name for table such as ID)
Column value(label) -textbox( for getting a colum value for the colum name such as 10)
Button
if i click a button at run time
the first textbox value should create a table
the second text box should create a column for table
the third to insert column name for table
example in sql we create (This will be creted in sql)
CREATE TABLE Employeedetails(ID Int)
INSERT INTO TABLE(ID)
VALUES(10)
I needed this to create the above in run time ....
I will very thankful if i finish this concept... Keep smiling Thanks for replys....
Sam HobbsPosted Nov 15, 2009, 4:03 PM
As Aleksandar indicated, it is not clear whether you need to create tables or create rows in tables. I will assume you need to create rows in tables, but that does not make sense since you have only one field in your form so the new rows will have just one field.
First I suggest you open the connection only once for the form, so add the following member to the form class:
Then in the form's Load event do something such as:
And in the form's Closed event:
Then in the button click you can do something such as:
But note that as I said, that will create a new row with just one field, and I doubt that is what you need.
Can you clarify what you need to do?
Aleksandar IlioskiPosted Nov 15, 2009, 2:29 PM