Hai friends,
Im storing the list box selected items to database .how can i select the item in list box when item is present in database.
thanks and regards
sree
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
sreePosted Oct 10, 2012, 12:47 AM
Satyapriya NayakPosted Oct 9, 2012, 12:46 AM
Try this...
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.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
OleDbDataAdapter oledbda;
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string str;
OleDbCommand com;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Raj");
listBox1.Items.Add("Raju");
listBox1.Items.Add("Rahul");
listBox1.Items.Add("Harry");
listBox1.Items.Add("Ben");
listBox1.Items.Add("Ian");
}
private void btn_insert_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select count(*)from student where sname='" + listBox1.SelectedItem + "'";
com = new OleDbCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
if (count > 0)
{
lblMessage.Text = "Sorry! you can't take this student name";
}
else
{
lblMessage.Text = "You can take this student name";
con.Open();
str = "insert into student(sname) values('" + listBox1.SelectedItem + "')";
com = new OleDbCommand(str, con);
com.ExecuteNonQuery();
con.Close();
}
}
}
}
Thanks
If this post helps you mark it as answer