insert using checkedlistbox
i need to insert multiple items using checkedlistbox in c# and sql 2005.please give code and insert sp
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.
Dipankar BiswasPosted Aug 12, 2014, 5:17 AM
Sqlconnection con=new Sqlconnection();
con.open();
con.Connectionstring="Data Source=PHANNY-PC\PHANNY; Initial Catalog=db_stuRegisterPay; Integrated Security=SSPI";
string query="insert into Persons_info(perID, latinName, gender, dob, pob, phone, passport, curAdd, status) values('" + txtID.Text + "','" + txtLatinName.Text + "','" + cbGender.Text + "'" + dTPdob.Text + txtPob.Text + "','" + txtPhone.Text + "','" + txtPassport.Text + "'" + txtCurAdd.Text + "'" + cbStatus.Text + " )";
SqlCommand cmd=new SqlCommand(query,con);
cmd.ExecuteNonQuery();
MessageBox.show("Saving is done!");
Now your sp in sql..example.
C# Code.........example..
try
{
sqlConnection = new SqlConnection(dbConnectionString);
SqlCommand command = new SqlCommand("sp_Test", sqlConnection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@Id", SqlDbType.VarChar).Value = txtId.Text;
command.Parameters.Add("@Name", SqlDbType.DateTime).Value = txtName.Text;
sqlConnection.Open();
return command.ExecuteNonQuery();
sqlConnection.Close();
}
catch (SqlException ex)
{
Console.WriteLine("SQL Error" + ex.Message.ToString());
return 0;
}
you are use checkbox.... so only change...
command.Parameters.Add("@Id", SqlDbType.VarChar).Value = checkbox1.Text;
Ankur JainPosted Aug 12, 2014, 5:13 AM
code for get selected item from checkbox list and save using store procedure...
now you can pass rightSelectedItems as parameter in your procedure...
create procedure sp_insertCheckboxItem
@Item nvarchar(250)=null
as
begin transcation
insert into tablename (columnname) values(@item)
commit;
end
please don't forget to mark it as answer if it's helpful