Question Update String using my sql

msdhh

New member
Joined
Oct 30, 2012
Messages
2
Programming Experience
Beginner
Hi There,

This is my First time to post a thread, and I am a beginner to C# programming I have an issue with the Update String.
Right Now I'm developing a program for Business Card whenever I receive a business Card I can save it into the program so it helps me to retrieve it back whenever I need it.
I have Four Buttons (Save, Delete, Update and Clear) also I have on combo box list all the Ids so once a certain ID is clicked it shows me all the information of that particular Business Card. All the buttons are working smoothly except the Update it doesn't give me an error though but it doesn't update the information that I want to apply to a certain text field. Here is the entire source code of the application. I would be greatly appreciate it if you can help me out with this issue.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Business_Card
{
public partial class Form1 : Form
{
SqlConnection conn;
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
void FillCombo()
{
String sql = "select ID from BusinessCard ";
SqlDataReader dr;
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = sql;
dr = cmd.ExecuteReader();
cmb_Id.Items.Clear();
while (dr.Read())
{
cmb_Id.Items.Add(dr[0].ToString());
}
if (conn.State == ConnectionState.Open)
{
conn.Close();
}

}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
}

private void Form1_Load(object sender, EventArgs e)
{
String connStr = "server=.;database=msdhh;uid=sa;pwd=sasa";
try
{
conn = new SqlConnection(connStr);
cmd = new SqlCommand();
cmd.Connection = conn;
FillCombo();
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}

}

private void cmd_Save_Click(object sender, EventArgs e)
{
String sql = "insert into BusinessCard values("+txt_ID.Text+",'"+ txt_Name.Text+"','"+txt_Rank.Text +"','"+txt_mobilePhone.Text+"','"+txt_phone.Text+"','"+txt_Fax.Text+"','"+ txt_email.Text+"','"+txt_plcOfWrk.Text+"','"+txt_POBox.Text+"','"+txt_address.Text+"')";

try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

if (conn.State == ConnectionState.Open)
{
conn.Close();
}
FillCombo();
MessageBox.Show("Saved..");
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);

}

}

private void cmd_Delete_Click(object sender, EventArgs e)
{
String sql = "Delete From BusinessCard Where ID = " + txt_ID.Text;
try
{

if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

if (conn.State == ConnectionState.Open)
{
conn.Close();
}
FillCombo();
MessageBox.Show("Deleted..");
ClearF();
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
}

private void cmd_Update_Click(object sender, EventArgs e)
{


String sql = "update BusinessCard set EmployeeName = '" + txt_Name.Text + "', Rank= '" + txt_Rank.Text + "',MobilePhone= '" + txt_mobilePhone.Text + "',Telephone='" + txt_phone.Text + "',Fax ='" + txt_Fax.Text + "',Email='" + txt_email.Text + "',PlaceOfWork='" + txt_plcOfWrk.Text + "',POBox='" + txt_POBox.Text + "',Address='" + txt_address.Text + "'where ID = " + txt_ID.Text;
try
{

if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = sql;
cmd.ExecuteNonQuery();

if (conn.State == ConnectionState.Open)
{
conn.Close();
}
MessageBox.Show("Updated..");
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
}
private void cmb_Id_SelectedIndexChanged(object sender, EventArgs e)
{
String sql = "select * from BusinessCard where ID= " + cmb_Id.Text;
SqlDataReader dr; //Read the Content from database
try
{
//If Connection is Closed then Open to save the Record
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = sql;
dr = cmd.ExecuteReader();

//Filling Combobox with Sid
if (dr.Read())
{
txt_ID.Text = dr[0].ToString();
txt_Name.Text = dr[1].ToString();
txt_Rank.Text = dr[2].ToString();
txt_mobilePhone.Text = dr[3].ToString();
txt_phone.Text =dr[4].ToString();
txt_Fax.Text = dr[5].ToString();
txt_email.Text = dr[9].ToString();
txt_plcOfWrk.Text = dr[7].ToString();
txt_POBox.Text = dr[8].ToString();
txt_address.Text = dr[6].ToString();

}

//After Saving Closing the Connection
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
catch (Exception exe)
{
MessageBox.Show(exe.Message);
}
}





private void cmd_Clear_Click(object sender, EventArgs e)
{
ClearF();
}
void ClearF()
{

txt_ID.Text= "";
txt_Name.Text="";
txt_Rank.Text ="";
txt_mobilePhone.Text = "";
txt_phone.Text ="";
txt_Fax.Text =" ";
txt_email.Text = "";
txt_plcOfWrk.Text="";
txt_POBox.Text="";
txt_address.Text = "";
}

}
}
 
I got this problem solved, but there is an issue with the primary key, I set the Column name (ID) as a primary key but the thing is that I can but the same primary key for different data entry
for example if i put 123 as a primary key for one data entry, and the same ID123 for anothe different data enrty it doesnt show me an invalid ID number that it has been taken for anthor data entry.
So how can i solve this problem please.
 
Back
Top Bottom