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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Oct 8, 2012, 8:46 PM EDT
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
sree29 reputationPosted Oct 9, 2012, 8:47 PM EDT