469,926 Members | 1,767 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

How Insert into database and display in the form

2
Need Help Here.......
I'm using visual C#:
How to insert the value that user have entered combobox and click button.The value will sent into database.This is in form1.

In form2,if click button it will display in listbox.There also got another button to to delete the value according the index have chose using cursour.

I'm using sql server 2005,the were connection problem.How to know the server name for my computer. Below is the coding I have done but cant work......
Database name is testing.mdf,table name display,field is value.
////////////////////
form1
///////////////////
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data.SqlClient;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace test1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         DataTable datatable;
  15.  
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             string connectionString = "server=P-III;database=testing;uid=sa;pwd=;";
  25.             SqlConnection conn = new SqlConnection(connectionString);
  26.  
  27.             ?????? for inserting I don't know much.
  28.         }   
  29.      private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  30.         {
  31.  
  32.         }
  33.     }
  34. }
///////////////////////////////////
form2
//////////////////////////////////
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data.SqlClient;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace test
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         DataTable datatable;
  15.         int currRec = 0;
  16.         int totalRec = 0;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             string connectionString = "server=P-III;database=testing;uid=sa;pwd=;";
  26.             SqlConnection conn = new SqlConnection(connectionString);
  27.  
  28.             string commandString = "SELECT value from display";
  29.  
  30.             SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, conn);
  31.             DataSet ds = new DataSet();
  32.             dataAdapter.Fill(ds, "prog");
  33.  
  34.             datatable = ds.Tables["prog"];
  35.             currRec = 0;
  36.             totalRec = datatable.Rows.Count;
  37.  
  38.             FillControls();
  39.         }
  40.  
  41.         private void FillControls()
  42.         {
  43.             listBox1.Text = datatable.Rows[currRec]["display"].ToString();
  44.  
  45.         }
  46.  
  47.         private void button2_Click(object sender, EventArgs e)
  48.         {
  49.             ????? to delete the value in the list one by one .
  50.         }
  51.  
  52.  
  53.     }
  54. }
Tq in advance. If get the solution of sample coding is appreaciated.
Apr 8 '07 #1
2 1328
There is lots of way to accomplish this...

U can use the following code...

Expand|Select|Wrap|Line Numbers
  1.   Dim conn As SqlConnection = New SqlConnection(_connectionString) 'Connection Parameters
  2.         Dim cmd As SqlCommand = New SqlCommand("INSERT INTO Customer " & _
  3.                                             "  (CustomerId, CustomerName, ContactName,CustomerRank, Occupation,Address1,Address2,City,Telephone,Mobile,Fax,Email,UserCode) " & _
  4.                                             "  Values(@id, @name, @contact, @Rank, @occupation,@add1,@add2,@city,@tel,@mob,@fax,@mail,@uid)", conn)
  5.  
  6.         cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = Your control Reference
  7.         cmd.Parameters.Add("@name", SqlDbType.VarChar, 200).Value = Your control Reference
  8.  'Setting Parameters
  9.     // Put value in that way for all parameters
  10.         Try
  11.             conn.Open()
  12.  
  13.             cmd.ExecuteNonQuery()
  14.  
  15.  
  16.         Catch e As Data.SqlClient.SqlException
  17.             ' Handle exception.
  18.             Throw New ArgumentException(e.Message)
  19.         Finally
  20.             conn.Close()
  21.         End Try
  22.  
  23.         Return 1
Apr 8 '07 #2
krajah
2
Tq very much.It's help me
Apr 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by EricRobineau | last post: by
reply views Thread by Eustice Scrubb | last post: by
5 posts views Thread by Brad Baker | last post: by
5 posts views Thread by =?Utf-8?B?bXBhaW5l?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.