Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;

namespace testedit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }



        public void ShowData()
        {

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\MyPractice\testedit\testedit\Database1.mdf;Integrated Security=True");

            con.Open();

            SqlCommand comm = new SqlCommand("Select * from userdata", con);

            DataTable dt = new DataTable();

            SqlDataAdapter da = new SqlDataAdapter(comm);

            da.Fill(dt);

            mygd.ItemsSource = dt.DefaultView;

        }





        private void windwloaded(object sender, RoutedEventArgs e)
        {
            ShowData();
        }



        private void insert_btn_Click(object sender, RoutedEventArgs e)
        {
            int Id = Convert.ToInt32( txtid.Text);
            string Username = textBox1.Text;

            string password = textBox2.Text;

            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\MyPractice\testedit\testedit\Database1.mdf;Integrated Security=True");

            con.Open();

            SqlCommand comm = new SqlCommand("insert into userdata(Id,username,password) values(@Id,@username,@password)", con);
            comm.Parameters.AddWithValue("@Id", txtid.Text);

            comm.Parameters.AddWithValue("@username", textBox1.Text);

            comm.Parameters.AddWithValue("@password", textBox2.Text);

            comm.ExecuteNonQuery();

            con.Close();

            ShowData();
        }







        private void clrbtn_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = "";

            textBox2.Text = "";
            txtid.Text = "";
        }





        private void dltbtn_Click(object sender, RoutedEventArgs e)
        {
            if (   mygd.SelectedItems.Count > 0)
            {

                DataRowView drv = (DataRowView)mygd.SelectedItem;

                string id = drv.Row[0].ToString();

                SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\MyPractice\testedit\testedit\Database1.mdf;Integrated Security=True");

                con.Open();

                SqlCommand comm = new SqlCommand("delete from userdata where id=@id", con);

                comm.Parameters.AddWithValue("@id", id);

                comm.ExecuteNonQuery();

                ShowData();
            }
        }






        private void updtbtn_Click(object sender, RoutedEventArgs e)
        {
            if (mygd.SelectedItems.Count > 0)
            {

                DataRowView drv = (DataRowView)mygd.SelectedItem;

                string id = drv.Row[0].ToString();

                SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\MyPractice\testedit\testedit\Database1.mdf;Integrated Security=True");


                con.Open();

                SqlCommand comm = new SqlCommand("update userdata set username=@username,password=@password where id=@id", con);

                comm.Parameters.AddWithValue("@id", txtid.Text);

                comm.Parameters.AddWithValue("@username", textBox1.Text);

                comm.Parameters.AddWithValue("@password", textBox2.Text);

                comm.ExecuteNonQuery();

                con.Close();
                ShowData();
               
            }
        }









        
    }




<window x:class="testedit.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="693.283" Width="625" Loaded="windwloaded">
    <grid margin="0,0,-8,-1">
        <textbox margin="129,548,207,81" name="textBox1" datacontext="{Binding ElementName=listView1,Path=SelectedItem}" text="{Binding Path=Username}" />
        <textbox height="27" margin="129,0,207,25" name="textBox2" verticalalignment="Bottom" datacontext="{Binding ElementName=listView1,Path=SelectedItem}" text="{Binding<br mode=" hold=" />	 Path=Password}" />
        <Label Margin="20,544,525,81" Name="label1">Username :</Label>
        <Label Height="25" Margin="20,0,535,27" Name="label2" VerticalAlignment="Bottom" RenderTransformOrigin="0.504,-1.301">Password :</Label>

        <Button x:Name="insert_btn" Margin="454,93,43,535" Content="Insert" Click="insert_btn_Click"></Button>
        <Button x:Name="updtbtn" Margin="454,196,43,433" Content="Update" Click="updtbtn_Click"/>
        <Button x:Name="dltbtn" Margin="454,308,43,321" Content="Delete" Click="dltbtn_Click"/>
        <Button x:Name="clrbtn" Margin="454,409,43,220" RenderTransformOrigin="0.631,4.197" Content="Clear" Click="clrbtn_Click"/>
        <textbox margin="129,494,207,135" x:name="txtid" datacontext="{Binding SelectedItem, ElementName=listView1}" text="{Binding Username}" />
        <Label Margin="20,490,525,135" x:Name="label1_Copy" Content="id"/>
        <datagrid x:name="mygd" autogeneratecolumns="False" margin="40,93,223,201">
            <datagrid.columns>


                <datagridtextcolumn header="Id" binding="{Binding Id}" />
                <datagridtextcolumn header="username" binding="{Binding username}" />
                <datagridtextcolumn header="password" binding="{Binding password}" />
               
            </datagrid.columns>



        </datagrid>



    </grid>
</window>
Posted
Updated 13-May-15 0:46am
v2
Comments
Abhipal Singh 13-May-15 13:12pm    
Can you please explain the problem you are getting in a little more detail?
[no name] 14-May-15 1:33am    
here in this code,when I click delete dat will deleted .but when I clik update button nothing happen.i want to edit n update

1 solution

Your code looks fine. There is an issue with AddWithValue(). Check this link: http://stackoverflow.com/questions/9999751/difference-between-parameters-add-and-parameters-addwithvalue[^]

Try to use the below code and see if it works. (Assuming you have all the columns as VARCHAR in database)

C#
private void updtbtn_Click(object sender, RoutedEventArgs e)
        {
            if (mygd.SelectedItems.Count > 0)
            {

                DataRowView drv = (DataRowView)mygd.SelectedItem;

                string id = drv.Row[0].ToString();

                SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=temp;Integrated Security=True");


                con.Open();

                SqlCommand comm = new SqlCommand("update userdata set username=@username,password=@password where id=@id", con);

                comm.Parameters.Add("@id", SqlDbType.VarChar).Value = txtid.Text;

                comm.Parameters.Add("@username", SqlDbType.VarChar).Value = textBox1.Text;

                comm.Parameters.Add("password", SqlDbType.VarChar).Value = textBox2.Text;

                comm.ExecuteNonQuery();

                con.Close();
                ShowData();

            }
 
Share this answer
 
v2
Comments
[no name] 15-May-15 0:31am    
my primary key in int type and am using local dB.dis s also not working
Abhipal Singh 15-May-15 9:24am    
Well, the code looks fine as I tested it on my machine. I believe there is something else which is causing the issue.

I need to debug to figure it out. It would be great if you can share your entire code or if you could share your screen with me over Teamviewer.

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