Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends as u know I am new to c# and has a very poor knowledge of c#

In my application I have three contact box.

I want to insert this 3 box value in my contact table but not column wisei want to insert each text box value in particular row

Please any one help me how is it possible.
Posted
Comments
[no name] 16-Jul-13 7:32am    
You connect to whatever database you are using, write an INSERT or UPDATE query and execute the query.
Er Bhargav Dudani 16-Jul-13 7:56am    
will u please explain me with demo example
[no name] 16-Jul-13 8:07am    
No. You have not provided nearly enough information to be able to do that. Besides that, you need to try yourself.
Naz_Firdouse 16-Jul-13 7:36am    
rowwise insertion in the sense....please elaborate
Er Bhargav Dudani 16-Jul-13 7:46am    
let me explain with demo

my table have only 2 columns 1 is id and 2 is value

in which id is auto incremented,

my form have n number of text box so when I press on insert button then my 1st text box value will be inserted with id 1,second text box value inserted with id=2and so on

C#
try
    {
//your database connection string.
        string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Customers.mdf;Integrated Security=True;User Instance=True";

        using (SqlConnection connection = new SqlConnection(connectionString))
        using (SqlCommand command = connection.CreateCommand())
        {
            command.CommandText = "INSERT INTO TableName(ColumnName1, ColumnName2, ColumnName3) VALUES (@value1, @value2, @value3)";

            command.Parameters.AddWithValue("@value1", ContactBox1.Text);
            command.Parameters.AddWithValue("@value2", ContactBox2.Text);
            command.Parameters.AddWithValue("@value3", ContactBox3.Text);
           

            connection.Open();
            command.ExecuteNonQuery();
        }
    }
    catch (SqlException ex)
    {
        Console.WriteLine(ex.Message);
    }
 
Share this answer
 
v2
Comments
[no name] 16-Jul-13 7:39am    
Now what makes you think he is using SQLExpress? What if he is using Access? Or MySQL?
1. Connect to Database
2. Give sql command like
C#
string sql= "insert into tablename (Contact) values (?p1) where contactid=values";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddWithValue("?p1",TextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
 
Share this answer
 
v2
Comments
Er Bhargav Dudani 16-Jul-13 8:16am    
Sorry but it gives me following error

Incorrect syntax near '?'
SrinivasTiru 16-Jul-13 9:07am    
try like this
string sql= "insert into tablename (Contact) values (?p1) where contactid=values;";

As you mentioned in the comment, if there are N number of TextBox, then you may have to go through loop for inserting into the table

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